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);
}
}
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;
}
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);
}
}
}
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;
}
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;
}
}
Aggregations