@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
final Class<?> vClass = value.getClass(); if (vClass.isArray()) { value = (T) this.<Object> doCopyArray((Object[]) value, baseInterface); } else if (value instanceof Collection) { value = (T) this.<Object> doCopyCollection((Collection<Object>) value, baseInterface); } else if (value instanceof Map) { value = (T) this.<Object, Object> doCopyMap((Map<Object, Object>) value, baseInterface); } else if (isBasicType(vClass)) { if (value instanceof CopyFrom) { final CopyFrom source = (CopyFrom) value; CopyFrom target = createInstance(source.getInterface()); if (target == null) { target = (CopyFrom) value.getClass().newInstance();
private <S, T> Map<S, T> doCopyMap(final Map<S, T> map, final Class<?> baseInterface) throws Exception { final Map<S, T> newMap = new HashMap<S, T>(); for (final Entry<S, T> entry : map.entrySet()) { // TODO mustn't the key be copied too? final T copiedValue = doCopy(entry.getValue(), baseInterface); newMap.put(entry.getKey(), copiedValue); } return newMap; }
final Class<?> vClass = value.getClass(); if (vClass.isArray()) { value = (T) this.<Object> doCopyArray((Object[]) value, baseInterface); } else if (value instanceof Collection) { value = (T) this.<Object> doCopyCollection((Collection<Object>) value, baseInterface); } else if (value instanceof Map) { value = (T) this.<Object, Object> doCopyMap((Map<Object, Object>) value, baseInterface); } else if (isBasicType(vClass)) { if (value instanceof CopyFrom) { final CopyFrom source = (CopyFrom) value; CopyFrom target = createInstance(source.getInterface()); if (target == null) { target = (CopyFrom) value.getClass().newInstance();
private <T> Collection<T> doCopyCollection(final Collection<T> collection, final Class<?> baseInterface) throws Exception { // expecting a set or a list final Collection<T> newCollection; if (collection instanceof Set) { newCollection = new LinkedHashSet<T>(); } else { newCollection = new ArrayList<T>(); } for (final T item : collection) { final T copied = doCopy(item, baseInterface); newCollection.add(copied); } return newCollection; }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
private <T> T[] doCopyArray(final T[] array, final Class<?> baseInterface) 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++) { final Object element = doCopy(Array.get(array, i), baseInterface); Array.set(newArray, i, element); } return newArray; }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
private <T> Collection<T> doCopyCollection(final Collection<T> collection, final Class<?> baseInterface) throws Exception { // expecting a set or a list final Collection<T> newCollection; if (collection instanceof Set) { newCollection = new LinkedHashSet<T>(); } else { newCollection = new ArrayList<T>(); } for (final T item : collection) { final T copied = doCopy(item, baseInterface); newCollection.add(copied); } return newCollection; }
@Override public final void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
private <T> T[] doCopyArray(final T[] array, final Class<?> baseInterface) 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++) { final Object element = doCopy(Array.get(array, i), baseInterface); Array.set(newArray, i, element); } return newArray; }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
private <S, T> Map<S, T> doCopyMap(final Map<S, T> map, final Class<?> baseInterface) throws Exception { final Map<S, T> newMap = new HashMap<S, T>(); for (final Entry<S, T> entry : map.entrySet()) { // TODO mustn't the key be copied too? final T copiedValue = doCopy(entry.getValue(), baseInterface); newMap.put(entry.getKey(), copiedValue); } return newMap; }
@Override public void copyFrom(final CopyFrom syndImage) { COPY_FROM_HELPER.copy(this, syndImage); }
public void copy(final Object target, final Object source) { try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGettersAndSetters(beanInterfaceClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); if (baseInterfaceMap.containsKey(propertyName)) { final Method getter = propertyDescriptor.getReadMethod(); // only copies properties defined as copyFrom-able Object value = getter.invoke(source, NO_PARAMS); if (value != null) { final Method setter = propertyDescriptor.getWriteMethod(); final Class<?> baseInterface = baseInterfaceMap.get(propertyName); value = doCopy(value, baseInterface); setter.invoke(target, new Object[] { value }); } } } } catch (final Exception e) { LOG.error("Error while copying object", e); throw new RuntimeException("Could not do a copyFrom " + e, e); } }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
public void copy(final Object target, final Object source) { try { final List<PropertyDescriptor> propertyDescriptors = BeanIntrospector.getPropertyDescriptorsWithGettersAndSetters(beanInterfaceClass); for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) { final String propertyName = propertyDescriptor.getName(); if (baseInterfaceMap.containsKey(propertyName)) { final Method getter = propertyDescriptor.getReadMethod(); // only copies properties defined as copyFrom-able Object value = getter.invoke(source, NO_PARAMS); if (value != null) { final Method setter = propertyDescriptor.getWriteMethod(); final Class<?> baseInterface = baseInterfaceMap.get(propertyName); value = doCopy(value, baseInterface); setter.invoke(target, new Object[] { value }); } } } } catch (final Exception e) { LOG.error("Error while copying object", e); throw new RuntimeException("Could not do a copyFrom " + e, e); } }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }
@Override public void copyFrom(final CopyFrom obj) { COPY_FROM_HELPER.copy(this, obj); }