Search in sources :

Example 1 with AllowDirectAccess

use of org.openmrs.annotation.AllowDirectAccess in project openmrs-core by openmrs.

the class RequiredDataAdvice method getChildCollection.

/**
 * This method gets a child attribute off of an OpenmrsObject. It usually uses the getter for
 * the attribute, but can use the direct field (even if its private) if told to by the
 * {@link AllowDirectAccess} annotation.
 *
 * @param openmrsObject the object to get the collection off of
 * @param field the name of the field that is the collection
 * @return the actual collection of objects that is on the given <code>openmrsObject</code>
 * @should get value of given child collection on given field
 * @should should be able to get annotated private fields
 * @should throw APIException if getter method not found
 */
@SuppressWarnings("unchecked")
protected static Collection<OpenmrsObject> getChildCollection(OpenmrsObject openmrsObject, Field field) {
    String fieldName = field.getName();
    String getterName = "get" + StringUtils.capitalize(fieldName);
    try {
        // checks if direct access is allowed
        if (field.isAnnotationPresent(AllowDirectAccess.class)) {
            boolean previousFieldAccessibility = field.isAccessible();
            field.setAccessible(true);
            Collection<OpenmrsObject> childCollection = (Collection<OpenmrsObject>) field.get(openmrsObject);
            field.setAccessible(previousFieldAccessibility);
            return childCollection;
        } else {
            // access the field via its getter method
            Class<? extends OpenmrsObject> openmrsObjectClass = openmrsObject.getClass();
            Method getterMethod = openmrsObjectClass.getMethod(getterName, (Class[]) null);
            return (Collection<OpenmrsObject>) getterMethod.invoke(openmrsObject, new Object[] {});
        }
    } catch (IllegalAccessException e) {
        if (field.isAnnotationPresent(AllowDirectAccess.class)) {
            throw new APIException("unable.get.field", new Object[] { fieldName, openmrsObject.getClass() });
        } else {
            throw new APIException("unable.getter.method", new Object[] { "use", getterName, fieldName, openmrsObject.getClass() });
        }
    } catch (InvocationTargetException e) {
        throw new APIException("unable.getter.method", new Object[] { "run", getterName, fieldName, openmrsObject.getClass() });
    } catch (NoSuchMethodException e) {
        throw new APIException("unable.getter.method", new Object[] { "find", getterName, fieldName, openmrsObject.getClass() });
    }
}
Also used : AllowDirectAccess(org.openmrs.annotation.AllowDirectAccess) OpenmrsObject(org.openmrs.OpenmrsObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) APIException(org.openmrs.api.APIException) Collection(java.util.Collection) OpenmrsObject(org.openmrs.OpenmrsObject)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 OpenmrsObject (org.openmrs.OpenmrsObject)1 AllowDirectAccess (org.openmrs.annotation.AllowDirectAccess)1 APIException (org.openmrs.api.APIException)1