Generic cloning / copying
<p>I'm currently trying to write a custom clone-method for a class <code>GameMap</code> of mine.<br>
Somewhat like this:</p>

<pre><code>@Override
public GameMap clone()
{
    GameMap cloned = new GameMap(width, height, base.clone());
    cloned.setCash(cash);
    //the following are ArrayLists, obviously
    cloned.setPaths(cloneList(paths));
    cloned.setTowers(cloneList(towers));
    cloned.setEnemies(cloneList(enemies));
    cloned.setWaves(cloneList(waves));
    return cloned;
}
</code></pre>

<p>Problem is, this class contains several lists.
Now what I tried to do was to set each list of the new instance to a cloned version of itself, using a custom <code>cloneList</code> method, using generics.<br>
What I hoped would work:</p>

<pre><code>private &lt;T&gt; List&lt;T&gt; cloneList(List&lt;T&gt; list)
{
    List&lt;T&gt; newList = new ArrayList&lt;T&gt;(list.size());
    for (T element : list)
    {
        newList.add(element.clone());
    }
    return newList;
}
</code></pre>

<p>Unfortunately, <code>clone()</code> in <code>Object</code> is <code>protected</code>, which means that this code throws compile errors. (Note that I override the clone method for all classes that are elements in any of the 4 lists, so if <code>clone()</code> wasn't protected, this should work)</p>

<p>Does anybody have any idea how to fix this problem? I've thought about copy constructors, but I don't really want to write my own List implementation - If I did that, I might as well write 4 non-generic <code>cloneList</code> methods. All about clean code :D</p>

<p>Another way how I <em>could</em> theoretically solve the problem is by making all four classes that are elements in any of the lists subclasses of some superclass, so I'd then use the following code:</p>

<pre><code>newList.add( (T) ((Superclass) element).clone() );
</code></pre>

<p>Unfortunately, my <code>Enemy</code> class IS already a subclass of another class, so that possibility won't work either, since java doesn't support multiple inheritance.<br>
//As I typed the superclass thing, I realized that making the 4 classes implement an interface would work as well, so I guess that will be my way to go. I'm still interested if I could solve the problem another way, though.</p>

<p>Thanks in advance!</p>

<p>PS: I'd rather avoid external libraries, if possible.</p>