/** * Get the value of a field or getter method from an object allowing an annotation to override<br/> * Getting fields supports simple, nested, indexed, and mapped values:<br/> * <b>Simple:</b> Get/set a field in a bean (or map), Example: "title", "id"<br/> * <b>Nested:</b> Get/set a field in a bean which is contained in another bean, Example: "someBean.title", "someBean.id"<br/> * <b>Indexed:</b> Get/set a list/array item by index in a bean, Example: "myList[1]", "anArray[2]"<br/> * <b>Mapped:</b> Get/set a map entry by key in a bean, Example: "myMap(key)", "someMap(thing)"<br/> * * @param object any object * @param fieldName the name of the field (property) to get the value of or the getter method without the "get" and lowercase first char * @param annotationClass if the annotation class is set then we will attempt to get the value from the annotated field or getter method first * @return the value of the field OR null if the value is null * @throws FieldnameNotFoundException if this fieldName does not exist on the object and no annotation was found * @throws IllegalArgumentException if a failure occurs while getting the field value */ public Object getFieldValue(Object object, String fieldName, Class<? extends Annotation> annotationClass) { Object value; Class<?> elementClass = object.getClass(); if (annotationClass != null) { // try to find annotation first String annotatedField = getFieldNameWithAnnotation(elementClass, annotationClass); if (annotatedField != null) { fieldName = annotatedField; } } value = getFieldValue(object, fieldName); return value; }
/** * Get the value of a field or getter method from an object allowing an annotation to override<br/> * Getting fields supports simple, nested, indexed, and mapped values:<br/> * <b>Simple:</b> Get/set a field in a bean (or map), Example: "title", "id"<br/> * <b>Nested:</b> Get/set a field in a bean which is contained in another bean, Example: "someBean.title", "someBean.id"<br/> * <b>Indexed:</b> Get/set a list/array item by index in a bean, Example: "myList[1]", "anArray[2]"<br/> * <b>Mapped:</b> Get/set a map entry by key in a bean, Example: "myMap(key)", "someMap(thing)"<br/> * * @param object any object * @param fieldName the name of the field (property) to get the value of or the getter method without the "get" and lowercase first char * @param annotationClass if the annotation class is set then we will attempt to get the value from the annotated field or getter method first * @return the value of the field OR null if the value is null * @throws FieldnameNotFoundException if this fieldName does not exist on the object and no annotation was found * @throws IllegalArgumentException if a failure occurs while getting the field value */ public Object getFieldValue(Object object, String fieldName, Class<? extends Annotation> annotationClass) { Object value; Class<?> elementClass = object.getClass(); if (annotationClass != null) { // try to find annotation first String annotatedField = getFieldNameWithAnnotation(elementClass, annotationClass); if (annotatedField != null) { fieldName = annotatedField; } } value = getFieldValue(object, fieldName); return value; }
/** * This will determine the id property correctly (but fairly inefficiently) so this should be cached, * use {@link #getIdProperty(Class)} to get the id property for a class * @param type a persistent type * @return */ protected String findIdProperty(Class<?> type) { String idProp = null; DataMapper dm = getDataMapper(type); if (dm != null) { idProp = dm.getIdPropertyName(); } if (classes.contains(type)) { if (idProp == null) { // look for the annotation idProp = ReflectUtils.getInstance().getFieldNameWithAnnotation(type, PersistentId.class); } if (idProp == null) { idProp = "id"; } } return idProp; }
/** * Gets the fieldname of the identifier field for an entity class type * @param type any entity class type * @return the name of the identifier field for this entity OR null if it cannot be determined */ public static String getEntityIdField(Class<?> type) { String entityIdField = ReflectUtils.getInstance().getFieldNameWithAnnotation(type, EntityId.class); if (entityIdField == null) { try { ReflectUtils.getInstance().getFieldType(type, "id"); entityIdField = "id"; } catch (FieldnameNotFoundException e) { entityIdField = null; } } return entityIdField; }