public <T> T deepClone(T o, Map<Object, Object> clones) { try { return cloneInternal(o, clones); } catch (IllegalAccessException e) { // just rethrow unchecked throw new IllegalStateException(e); } } };
public <T> T deepClone(T o, Map<Object, Object> clones) { try { return cloneInternal(o, clones); } catch (IllegalAccessException e) { // just rethrow unchecked throw new IllegalStateException(e); } } };
@Override @SuppressWarnings("unchecked") public <T> T cloneInternal(T o, Map<Object, Object> clones) throws IllegalAccessException { if (o instanceof Logger) { return (T) LoggerFactory.getLogger(o.getClass()); } if (o instanceof ClassLoader) { // There is no need to clone ClassLoader return null; } return super.cloneInternal(o, clones); } }
@Override @SuppressWarnings("unchecked") public <T> T cloneInternal(T o, Map<Object, Object> clones) throws IllegalAccessException { if (o instanceof Logger) { return (T) LoggerFactory.getLogger(o.getClass()); } if (o instanceof ClassLoader) { // There is no need to clone ClassLoader return null; } return super.cloneInternal(o, clones); } }
private Object applyCloningStrategy(Map<Object, Object> clones, Object o, Object fieldObject, Field field) throws IllegalAccessException { for (ICloningStrategy strategy : cloningStrategies) { ICloningStrategy.Strategy s = strategy.strategyFor(o, field); if (s == ICloningStrategy.Strategy.NULL_INSTEAD_OF_CLONE) return null; if (s == ICloningStrategy.Strategy.SAME_INSTANCE_INSTEAD_OF_CLONE) return fieldObject; } return cloneInternal(fieldObject, clones); }
private Object applyCloningStrategy(Map<Object, Object> clones, Object o, Object fieldObject, Field field) throws IllegalAccessException { for (ICloningStrategy strategy : cloningStrategies) { ICloningStrategy.Strategy s = strategy.strategyFor(o, field); if (s == ICloningStrategy.Strategy.NULL_INSTEAD_OF_CLONE) return null; if (s == ICloningStrategy.Strategy.SAME_INSTANCE_INSTEAD_OF_CLONE) return fieldObject; } return cloneInternal(fieldObject, clones); }
@SuppressWarnings("unchecked") private <T> T cloneArray(T o, Map<Object, Object> clones) throws IllegalAccessException { final Class<T> clz = (Class<T>) o.getClass(); final int length = Array.getLength(o); final T newInstance = (T) Array.newInstance(clz.getComponentType(), length); if (clones != null) { clones.put(o, newInstance); } if (clz.getComponentType().isPrimitive() || isImmutable(clz.getComponentType())) { System.arraycopy(o, 0, newInstance, 0, length); } else { for (int i = 0; i < length; i++) { final Object v = Array.get(o, i); final Object clone = clones != null ? cloneInternal(v, clones) : v; Array.set(newInstance, i, clone); } } return newInstance; }
/** * shallow clones "o". This means that if c=shallowClone(o) then * c!=o. Any change to c won't affect o. * * @param <T> the type of o * @param o the object to be shallow-cloned * @return a shallow clone of "o" */ public <T> T shallowClone(final T o) { if (o == null) return null; if (!cloningEnabled) return o; try { return cloneInternal(o, null); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }
/** * shallow clones "o". This means that if c=shallowClone(o) then * c!=o. Any change to c won't affect o. * * @param <T> the type of o * @param o the object to be shallow-cloned * @return a shallow clone of "o" */ public <T> T shallowClone(final T o) { if (o == null) return null; if (!cloningEnabled) return o; try { return cloneInternal(o, null); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }
@SuppressWarnings("unchecked") private <T> T cloneArray(T o, Map<Object, Object> clones) throws IllegalAccessException { final Class<T> clz = (Class<T>) o.getClass(); final int length = Array.getLength(o); final T newInstance = (T) Array.newInstance(clz.getComponentType(), length); if (clones != null) { clones.put(o, newInstance); } if (clz.getComponentType().isPrimitive() || isImmutable(clz.getComponentType())) { System.arraycopy(o, 0, newInstance, 0, length); } else { for (int i = 0; i < length; i++) { final Object v = Array.get(o, i); final Object clone = clones != null ? cloneInternal(v, clones) : v; Array.set(newInstance, i, clone); } } return newInstance; }
public <T> T deepCloneDontCloneInstances(final T o, final Object... dontCloneThese) { if (o == null) return null; if (!cloningEnabled) return o; if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } final Map<Object, Object> clones = new IdentityHashMap<Object, Object>(16); for (final Object dc : dontCloneThese) { clones.put(dc, dc); } try { return cloneInternal(o, clones); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }
public <T> T deepCloneDontCloneInstances(final T o, final Object... dontCloneThese) { if (o == null) return null; if (!cloningEnabled) return o; if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } final Map<Object, Object> clones = new IdentityHashMap<Object, Object>(16); for (final Object dc : dontCloneThese) { clones.put(dc, dc); } try { return cloneInternal(o, clones); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }
/** * deep clones "o". * * @param <T> the type of "o" * @param o the object to be deep-cloned * @return a deep-clone of "o". */ public <T> T deepClone(final T o) { if (o == null) return null; if (!cloningEnabled) return o; if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } final Map<Object, Object> clones = new IdentityHashMap<Object, Object>(16); try { return cloneInternal(o, clones); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }
/** * deep clones "o". * * @param <T> the type of "o" * @param o the object to be deep-cloned * @return a deep-clone of "o". */ public <T> T deepClone(final T o) { if (o == null) return null; if (!cloningEnabled) return o; if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } final Map<Object, Object> clones = new IdentityHashMap<Object, Object>(16); try { return cloneInternal(o, clones); } catch (final IllegalAccessException e) { throw new CloningException("error during cloning of " + o, e); } }