Search in sources :

Example 1 with Method

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

the class EntityBinding method bindAttrOrRelation.

/**
 * Bind an attribute or relationship.
 *
 * @param fieldOrMethod Field or method to bind
 * @param isHidden Whether this field is hidden from API
 */
private void bindAttrOrRelation(AccessibleObject fieldOrMethod, boolean isHidden) {
    boolean isRelation = RELATIONSHIP_TYPES.stream().anyMatch(fieldOrMethod::isAnnotationPresent);
    String fieldName = getFieldName(fieldOrMethod);
    Type<?> fieldType = getFieldType(entityClass, fieldOrMethod);
    if (fieldName == null || REGULAR_ID_NAME.equals(fieldName) || "class".equals(fieldName) || OBJ_METHODS.contains(fieldOrMethod)) {
        // Reserved
        return;
    }
    if (fieldOrMethod instanceof Method) {
        Method method = (Method) fieldOrMethod;
        requestScopeableMethods.put(method, isRequestScopeableMethod(method));
    }
    if (isRelation) {
        bindRelation(fieldOrMethod, fieldName, fieldType, isHidden);
    } else {
        bindAttr(fieldOrMethod, fieldName, fieldType, isHidden);
    }
}
Also used : Method(com.yahoo.elide.core.type.Method)

Example 2 with Method

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

the class EntityBinding method getFieldName.

/**
 * Returns name of field whether public member or method.
 *
 * @param fieldOrMethod field or method
 * @return field or method name
 */
public static String getFieldName(AccessibleObject fieldOrMethod) {
    if (fieldOrMethod instanceof Field) {
        return ((Field) fieldOrMethod).getName();
    }
    Method method = (Method) fieldOrMethod;
    String name = method.getName();
    boolean hasValidParameterCount = method.getParameterCount() == 0 || isRequestScopeableMethod(method);
    if (name.startsWith("get") && hasValidParameterCount) {
        return StringUtils.uncapitalize(name.substring("get".length()));
    }
    if (name.startsWith("is") && hasValidParameterCount) {
        return StringUtils.uncapitalize(name.substring("is".length()));
    }
    return null;
}
Also used : Field(com.yahoo.elide.core.type.Field) Method(com.yahoo.elide.core.type.Method)

Example 3 with Method

use of com.yahoo.elide.core.type.Method 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 4 with Method

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

the class EntityDictionary method findMethod.

/**
 * Find an arbitrary method.
 *
 * @param entityClass the entity class
 * @param name        the name
 * @param paramClass  the param class
 * @return method method
 * @throws NoSuchMethodException the no such method exception
 */
public static Method findMethod(Type<?> entityClass, String name, Type<?>... paramClass) throws NoSuchMethodException {
    Method m = entityClass.getMethod(name, paramClass);
    int modifiers = m.getModifiers();
    if (Modifier.isAbstract(modifiers) || m.isAnnotationPresent(Transient.class) && !m.isAnnotationPresent(ComputedAttribute.class) && !m.isAnnotationPresent(ComputedRelationship.class)) {
        throw new NoSuchMethodException(name);
    }
    return m;
}
Also used : ComputedRelationship(com.yahoo.elide.annotation.ComputedRelationship) Method(com.yahoo.elide.core.type.Method) Transient(javax.persistence.Transient)

Example 5 with Method

use of com.yahoo.elide.core.type.Method 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)

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