/** * Finds the enum where the getId() value matches the value of the field for this particular * object. * * @param field The field to get the value of. * @param value The value of the field. * @return The value, null if the field value is null, also null on error. */ public static Object findEnumById(final Field field, final Object value) { if (!IntrospectionUtils.isIdentifiableEnum(field)) { throw new IllegalArgumentException( "Field " + field.getName() + " is not an Identifiable enum"); } if (value == null) { return null; } final Object[] elements = field.getType().getEnumConstants(); for (final Object element : elements) { if (((Identifiable<?>) element).getId().equals(value)) { if (log.isLoggable(Level.FINEST)) { log.finest("Matched: " + element.toString()); } return element; } } if (log.isLoggable(Level.FINEST)) { log.finest("No Match for " + value); } return null; }
/** * Returns the value of a property descriptor, or null if an error occurs. * * @param object The object instance to invoke the property descriptor on * @param field The field of the class of the object that corresponds with the property * descriptor. * @param propertyDescriptor The property descriptor to invoke on the object. * @return The value of a property descriptor, or null if an error occurs. */ public static Object propGetSafeAuto( final Object object, final Field field, final PropertyDescriptor propertyDescriptor) { if (IntrospectionUtils.isString(field)) { // It's a string return ReflectionUtils.propGetSafe(object, propertyDescriptor); } else if (IntrospectionUtils.isDate(field)) { // It's a date return DateUtils.safeToLong(ReflectionUtils.propGetDateSafe(object, propertyDescriptor)); } else if (IntrospectionUtils.isToStringable(field)) { // It's toStringable return StringUtils.safeToString(ReflectionUtils.propGetSafe(object, propertyDescriptor)); } else if (IntrospectionUtils.isIdentifiable(field)) { // It's identifiable final Identifiable<?> identifiable = ((Identifiable<?>) ReflectionUtils.propGetSafe(object, propertyDescriptor)); if (identifiable == null) { return null; } return StringUtils.safeToString(identifiable.getId()); } else if (IntrospectionUtils.isInt(field)) { // It's an int return ReflectionUtils.propGetSafe(object, propertyDescriptor); } else if (IntrospectionUtils.isLong(field)) { // It's a long return ReflectionUtils.propGetSafe(object, propertyDescriptor); } else if (IntrospectionUtils.isBoolean(field)) { // It's a boolean return ReflectionUtils.propGetSafe(object, propertyDescriptor); } else { log.warning( "Can't handle property getting of type " + field.getType() + " for field " + field.getName()); return ReflectionUtils.propGetSafe(object, propertyDescriptor); } }