|
|
|
@ -266,54 +266,6 @@ public final class Utils {
|
|
|
|
|
//
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p>Deep clone an {@code Object} using serialization.</p> |
|
|
|
|
* <p/> |
|
|
|
|
* <p>This is many times slower than writing clone methods by hand |
|
|
|
|
* on all objects in your object graph. However, for complex object |
|
|
|
|
* graphs, or for those that don't support deep cloning this can |
|
|
|
|
* be a simple alternative implementation. Of course all the objects |
|
|
|
|
* must be {@code Serializable}.</p> |
|
|
|
|
* |
|
|
|
|
* @param <T> the type of the object involved |
|
|
|
|
* @param object the {@code Serializable} object to clone |
|
|
|
|
* @return the cloned object |
|
|
|
|
*/ |
|
|
|
|
public static <T extends Serializable> T clone(T object) { |
|
|
|
|
if (object == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
byte[] objectData = serialize(object); |
|
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(objectData); |
|
|
|
|
|
|
|
|
|
ClassLoaderAwareObjectInputStream in = null; |
|
|
|
|
try { |
|
|
|
|
// stream closed in the finally
|
|
|
|
|
in = new ClassLoaderAwareObjectInputStream(bais, object.getClass().getClassLoader()); |
|
|
|
|
/* |
|
|
|
|
* when we serialize and deserialize an object, |
|
|
|
|
* it is reasonable to assume the deserialized object |
|
|
|
|
* is of the same type as the original serialized object |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("unchecked") // see above
|
|
|
|
|
T readObject = (T) in.readObject(); |
|
|
|
|
return readObject; |
|
|
|
|
|
|
|
|
|
} catch (ClassNotFoundException ex) { |
|
|
|
|
throw new RuntimeException("ClassNotFoundException while reading cloned object data", ex); |
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
throw new RuntimeException("IOException while reading cloned object data", ex); |
|
|
|
|
} finally { |
|
|
|
|
try { |
|
|
|
|
if (in != null) { |
|
|
|
|
in.close(); |
|
|
|
|
} |
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
throw new RuntimeException("IOException on closing cloned object data InputStream.", ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p>Serializes an {@code Object} to the specified stream.</p> |
|
|
|
|