/** * Sample: if o is an instance of Product and c is OrderedProduct.class then this returns * and instance of OrderedProduct.class which has equal field values to those of the instance of Product. * In other words, the returned instance of OrderedProduct.class is the Product instance from the perspective * of an OrderedProduct * * View an object o from the perspective of class c. (view o as an instance of c). c must be instanceof o.getClass() * * @param <T> the object * @param <E> this will be the returned type and it must be instanceof T. All properties of o will be copied to this instance. * @param c the class of E. This is used to generate new instances of c * @param o the object that must be viewed from a different perspective * @return the E perspective of o */ public <T, E extends T> E viewAs(final Class<E> c, final T o) { if (o == null) return null; if (o instanceof Collection<?>) throw new IllegalArgumentException("for collections please use viewCollectionAs() method. Invalid object " + o); final E newInstance = cloner.fastCloneOrNewInstance(c); cloner.copyPropertiesOfInheritedClass(o, newInstance); return newInstance; }
/** * Sample: if o is an instance of Product and c is OrderedProduct.class then this returns * and instance of OrderedProduct.class which has equal field values to those of the instance of Product. * In other words, the returned instance of OrderedProduct.class is the Product instance from the perspective * of an OrderedProduct * * View an object o from the perspective of class c. (view o as an instance of c). c must be instanceof o.getClass() * * @param <T> the object * @param <E> this will be the returned type and it must be instanceof T. All properties of o will be copied to this instance. * @param c the class of E. This is used to generate new instances of c * @param o the object that must be viewed from a different perspective * @return the E perspective of o */ public <T, E extends T> E viewAs(final Class<E> c, final T o) { if (o == null) return null; if (o instanceof Collection<?>) throw new IllegalArgumentException("for collections please use viewCollectionAs() method. Invalid object " + o); final E newInstance = cloner.fastCloneOrNewInstance(c); cloner.copyPropertiesOfInheritedClass(o, newInstance); return newInstance; }