(Title sounds a bit too fancy but I couldn't find a match so we'll hope it's descriptive.)
I have a working piece of code as follows:
@SuppressWarnings("unchecked")
public static Copyable[] copyOf(Copyable[] objects, Class type) {
Copyable[] copies = Arrays.copyOf(objects, objects.length, type);
for( int i=0; i < copies.length; ++i ) {
if( objects[i] == null )
throw new IllegalArgumentException("Copyable[] contains null reference", new NullPointerException());
copies[i] = objects[i].copy();
}
return copies;
}
Not as pretty as I would like, since I have to pass in the array class, but it works for what I want it to do: allocate a new array of a Copyable implementor and fill it using the implemented method.
The problem I'm having is that this gets compiled by GWT, whose JRE Emulation library barfs on correct Java that it hasn't implemented. I need to do this without calling Arrays.copyOf(), and hopefully without reflection.
Note: I'm working on a clone() version but don't really want to rely on clone() either. I feel like there must be a cleaner solution.
How about this:
public static Copyable[] copyOf(Copyable[] objects) {
Copyable[] copies = new Copyable[objects.length];
for( int i=0; i < copies.length; ++i ) {
if( objects[i] == null )
throw new IllegalArgumentException("Copyable[] contains null reference", new NullPointerException());
copies[i] = objects[i].copy();
}
return copies;
}
There's no need to actually copy the objects
array into copies
before making copies of the elements themselves.
EDIT
If your Copyable objects are serializable, you can just serialize and then deserialize the array to create a copy. There's a utility function DeepCopy.java in gwt-test-utils that may do exactly what you need.