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