/** * 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; }
private static Performable instrumentedCopyOf(Performable task, Class taskClass) { Performable instrumentedTask = null; try { instrumentedTask = (Performable) Instrumented.instanceOf(taskClass).newInstance(); } catch(IllegalArgumentException missingDefaultConstructor) { throw new TaskInstantiationException("Could not instantiate " + taskClass + ". If you are not instrumenting a Task class explicitly you need to give the class a default constructor." + "A task class cannot be instrumented if it is final (so if you are writing in Kotlin, make sure the task class is 'open'."); } Cloner cloner = new Cloner(); cloner.copyPropertiesOfInheritedClass(task, instrumentedTask); return instrumentedTask; }