Search in sources :

Example 1 with AccessibleObject

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

the class EntityDictionary method setValue.

/**
 * Invoke the set[fieldName] method on the target object OR set the field with the corresponding name.
 * @param target The object which owns the field to set
 * @param fieldName the field name to set or invoke equivalent set method
 * @param value the value to set
 */
public void setValue(Object target, String fieldName, Object value) {
    Type<?> targetClass = getType(target);
    String targetType = getJsonAliasFor(targetClass);
    String fieldAlias = fieldName;
    try {
        Type<?> fieldClass = getType(targetClass, fieldName);
        String realName = getNameFromAlias(target, fieldName);
        fieldAlias = (realName != null) ? realName : fieldName;
        String setMethod = "set" + StringUtils.capitalize(fieldAlias);
        Method method = EntityDictionary.findMethod(targetClass, setMethod, fieldClass);
        method.invoke(target, coerce(target, value, fieldAlias, fieldClass));
    } catch (IllegalAccessException e) {
        throw new InvalidAttributeException(fieldAlias, targetType, e);
    } catch (InvocationTargetException e) {
        throw handleInvocationTargetException(e);
    } catch (IllegalArgumentException | NoSuchMethodException noMethod) {
        AccessibleObject accessor = getAccessibleObject(target, fieldAlias);
        if (accessor != null && accessor instanceof Field) {
            Field field = (Field) accessor;
            try {
                field.set(target, coerce(target, value, fieldAlias, field.getType()));
            } catch (IllegalAccessException noField) {
                throw new InvalidAttributeException(fieldAlias, targetType, noField);
            }
        } else {
            throw new InvalidAttributeException(fieldAlias, targetType);
        }
    }
}
Also used : Field(com.yahoo.elide.core.type.Field) InvalidAttributeException(com.yahoo.elide.core.exceptions.InvalidAttributeException) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Method(com.yahoo.elide.core.type.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with AccessibleObject

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

the class EntityDictionary method getId.

/**
 * Gets id.
 *
 * @param value the value
 * @return the id
 */
public String getId(Object value) {
    if (value == null) {
        return null;
    }
    try {
        AccessibleObject idField = null;
        Type<?> valueClass = getType(value);
        for (; idField == null && valueClass != null; valueClass = valueClass.getSuperclass()) {
            try {
                idField = getEntityBinding(valueClass).getIdField();
            } catch (NullPointerException e) {
                log.warn("Class: {} ID Field: {}", valueClass.getSimpleName(), idField);
            }
        }
        Type<?> idClass;
        Object idValue;
        if (idField instanceof Field) {
            idValue = ((Field) idField).get(value);
            idClass = ((Field) idField).getType();
        } else if (idField instanceof Method) {
            idValue = ((Method) idField).invoke(value, (Object[]) null);
            idClass = ((Method) idField).getReturnType();
        } else {
            return null;
        }
        Serde serde = serdeLookup.apply(((ClassType) idClass).getCls());
        if (serde != null) {
            return String.valueOf(serde.serialize(idValue));
        }
        return String.valueOf(idValue);
    } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
        return null;
    }
}
Also used : Serde(com.yahoo.elide.core.utils.coerce.converters.Serde) Field(com.yahoo.elide.core.type.Field) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Method(com.yahoo.elide.core.type.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with AccessibleObject

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

the class EntityBindingTest method testIdField.

@Test
public void testIdField() throws Exception {
    AccessibleObject idField = entityBinding.getIdField();
    assertEquals(idField, ClassType.of(ParentClass.class).getDeclaredField("parentField"));
}
Also used : AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) Test(org.junit.jupiter.api.Test)

Example 4 with AccessibleObject

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

the class EntityDictionary method getParameterizedType.

/**
 * Retrieve the parameterized type for the given field.
 *
 * @param entityClass the entity class
 * @param identifier  the identifier/field name
 * @param paramIndex  the index of the parameterization
 * @return Entity type for field otherwise null.
 */
public Type<?> getParameterizedType(Type<?> entityClass, String identifier, int paramIndex) {
    ConcurrentHashMap<String, AccessibleObject> fieldOrMethods = getEntityBinding(entityClass).fieldsToValues;
    if (fieldOrMethods == null) {
        return null;
    }
    AccessibleObject fieldOrMethod = fieldOrMethods.get(identifier);
    if (fieldOrMethod == null) {
        return null;
    }
    return EntityBinding.getFieldType(entityClass, fieldOrMethod, Optional.of(paramIndex));
}
Also used : AccessibleObject(com.yahoo.elide.core.type.AccessibleObject)

Aggregations

AccessibleObject (com.yahoo.elide.core.type.AccessibleObject)4 Field (com.yahoo.elide.core.type.Field)2 Method (com.yahoo.elide.core.type.Method)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InvalidAttributeException (com.yahoo.elide.core.exceptions.InvalidAttributeException)1 Serde (com.yahoo.elide.core.utils.coerce.converters.Serde)1 Test (org.junit.jupiter.api.Test)1