/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public final Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, CONVENIENCE_PROPERTIES); }
private static <T> Collection<T> cloneCollection(final Collection<T> collection) throws Exception { @SuppressWarnings("unchecked") final Collection<T> newCollection = newCollection(collection.getClass()); for (final T item : collection) { newCollection.add(doClone(item)); } return newCollection; }
@SuppressWarnings("unchecked") private static <T> T doClone(T value) throws Exception { if (value != null) { final Class<?> vClass = value.getClass(); if (vClass.isArray()) { value = cloneArray(value); } else if (value instanceof Collection) { value = (T) cloneCollection((Collection<Object>) value); } else if (value instanceof Map) { value = (T) cloneMap((Map<Object, Object>) value); } else if (isBasicType(vClass)) { // NOTHING SPECIAL TO DO HERE, THEY ARE INMUTABLE } else if (value instanceof Cloneable) { final Method cloneMethod = vClass.getMethod("clone", NO_PARAMS_DEF); if (Modifier.isPublic(cloneMethod.getModifiers())) { value = (T) cloneMethod.invoke(value, NO_PARAMS); } else { throw new CloneNotSupportedException("Cannot clone a " + value.getClass() + " object, clone() is not public"); } } else { throw new CloneNotSupportedException("Cannot clone a " + vClass.getName() + " object"); } } return value; }
private static <K, V> Map<K, V> cloneMap(final Map<K, V> map) throws Exception { @SuppressWarnings("unchecked") final Map<K, V> newMap = newMap(map.getClass()); for (final Entry<K, V> entry : map.entrySet()) { final K clonedKey = doClone(entry.getKey()); final V clonedValue = doClone(entry.getValue()); newMap.put(clonedKey, clonedValue); } return newMap; }
private static <T> T cloneArray(final T array) throws Exception { final Class<?> elementClass = array.getClass().getComponentType(); final int length = Array.getLength(array); @SuppressWarnings("unchecked") final T newArray = (T) Array.newInstance(elementClass, length); for (int i = 0; i < length; i++) { Array.set(newArray, i, doClone(Array.get(array, i))); } return newArray; }
@SuppressWarnings("unchecked") private static <T> T doClone(T value) throws Exception { if (value != null) { final Class<?> vClass = value.getClass(); if (vClass.isArray()) { value = cloneArray(value); } else if (value instanceof Collection) { value = (T) cloneCollection((Collection<Object>) value); } else if (value instanceof Map) { value = (T) cloneMap((Map<Object, Object>) value); } else if (isBasicType(vClass)) { // NOTHING SPECIAL TO DO HERE, THEY ARE INMUTABLE } else if (value instanceof Cloneable) { final Method cloneMethod = vClass.getMethod("clone", NO_PARAMS_DEF); if (Modifier.isPublic(cloneMethod.getModifiers())) { value = (T) cloneMethod.invoke(value, NO_PARAMS); } else { throw new CloneNotSupportedException("Cannot clone a " + value.getClass() + " object, clone() is not public"); } } else { throw new CloneNotSupportedException("Cannot clone a " + vClass.getName() + " object"); } } return value; }
private static <K, V> Map<K, V> cloneMap(final Map<K, V> map) throws Exception { @SuppressWarnings("unchecked") final Map<K, V> newMap = newMap(map.getClass()); for (final Entry<K, V> entry : map.entrySet()) { final K clonedKey = doClone(entry.getKey()); final V clonedValue = doClone(entry.getValue()); newMap.put(clonedKey, clonedValue); } return newMap; }
private static <T> T cloneArray(final T array) throws Exception { final Class<?> elementClass = array.getClass().getComponentType(); final int length = Array.getLength(array); @SuppressWarnings("unchecked") final T newArray = (T) Array.newInstance(elementClass, length); for (int i = 0; i < length; i++) { Array.set(newArray, i, doClone(Array.get(array, i))); } return newArray; }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, convenienceProperties); }
private static <T> Collection<T> cloneCollection(final Collection<T> collection) throws Exception { @SuppressWarnings("unchecked") final Collection<T> newCollection = newCollection(collection.getClass()); for (final T item : collection) { newCollection.add(doClone(item)); } return newCollection; }
value = doClone(value); setter.invoke(clonedBean, new Object[] { value });
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public final Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, CONVENIENCE_PROPERTIES); }
value = doClone(value); setter.invoke(clonedBean, new Object[] { value });
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, convenienceProperties); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, convenienceProperties); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, convenienceProperties); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, Collections.<String>emptySet()); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, Collections.<String>emptySet()); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, Collections.<String>emptySet()); }
/** * Creates a deep 'bean' clone of the object. * <p> * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * */ @Override public Object clone() throws CloneNotSupportedException { return CloneableBean.beanClone(this, Collections.<String>emptySet()); }