Search in sources :

Example 6 with Method

use of com.yahoo.elide.core.type.Method in project elide by yahoo.

the class MultiplexWriteTransaction method cloneObject.

/**
 *  Clone contents of object for possible reverse transaction.
 */
private Object cloneObject(Object object) {
    if (object == null) {
        return null;
    }
    Type<?> cls = multiplexManager.getDictionary().lookupBoundClass(EntityDictionary.getType(object));
    try {
        Object clone = cls.newInstance();
        for (Field field : cls.getFields()) {
            field.set(clone, field.get(object));
        }
        for (Method method : cls.getMethods()) {
            if (method.getName().startsWith("set")) {
                try {
                    Method getMethod = cls.getMethod("get" + method.getName().substring(3));
                    method.invoke(clone, getMethod.invoke(object));
                } catch (IllegalStateException | IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
                    return null;
                }
            }
        }
        return clone;
    } catch (InstantiationException | IllegalAccessException e) {
    // ignore
    }
    return null;
}
Also used : Field(com.yahoo.elide.core.type.Field) Method(com.yahoo.elide.core.type.Method)

Aggregations

Method (com.yahoo.elide.core.type.Method)6 Field (com.yahoo.elide.core.type.Field)4 AccessibleObject (com.yahoo.elide.core.type.AccessibleObject)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ComputedRelationship (com.yahoo.elide.annotation.ComputedRelationship)1 InvalidAttributeException (com.yahoo.elide.core.exceptions.InvalidAttributeException)1 Serde (com.yahoo.elide.core.utils.coerce.converters.Serde)1 Transient (javax.persistence.Transient)1