Search in sources :

Example 1 with SnakeToCamel

use of org.ehrbase.serialisation.util.SnakeToCamel in project openEHR_SDK by ehrbase.

the class CanonicalUtil method getAttributeValue.

/**
 * resolves an attribute of a RMObject
 * NB. borrowed from CAttribute.java in validation
 *
 * @param obj
 * @param attributePath
 * @return
 */
private static Object getAttributeValue(Object obj, String attributePath) {
    if (obj == null)
        return null;
    Class rmClass = obj.getClass();
    Object value;
    Method getter;
    String getterName;
    if (attributePath.startsWith("is_")) {
        // conventionally, a getter for a boolean uses 'is' as a prefix
        getterName = "is" + new SnakeToCamel(attributePath.substring(3)).convert();
    } else
        getterName = "get" + new SnakeToCamel(attributePath).convert();
    try {
        getter = rmClass.getMethod(getterName);
        value = getter.invoke(obj, null);
    } catch (Exception e) {
        throw new IllegalStateException("unresolved attribute:" + attributePath + " for class:" + rmClass);
    }
    return value;
}
Also used : RMObject(com.nedap.archie.rm.RMObject) Method(java.lang.reflect.Method) SnakeToCamel(org.ehrbase.serialisation.util.SnakeToCamel)

Aggregations

RMObject (com.nedap.archie.rm.RMObject)1 Method (java.lang.reflect.Method)1 SnakeToCamel (org.ehrbase.serialisation.util.SnakeToCamel)1