* @param root The object from which we want to extract the property
* @param key the key for the property.
* @param def the model definition.
*/
public static Object getValue(Object root, PropertyReference key, ModelDef def) throws QuickFixException {
LoggingService loggingService = Aura.getLoggingService();
loggingService.stopTimer(LoggingService.TIMER_AURA);
loggingService.startTimer("java");
Object ret = null;
try {
String part = key.getRoot();
PropertyReference stem = key.getStem();
if (root == null) {
return null;
} else if (root instanceof Map) {
ret = ((Map<?, ?>) root).get(part);
} else if (root instanceof List) {
List<?> l = ((List<?>) root);
// special case for length property
if ("length".equals(part)) {
ret = l.size();
} else {
int i;
try {
i = Integer.parseInt(part); // NumberFormatException will be caught below
} catch (NumberFormatException nfe) {
throw makeException(nfe.getMessage(), nfe, def);
}
ret = ((List<?>) root).get(i);
}
} else {
Method meth = null;
try {
meth = root.getClass().getMethod("get" + AuraTextUtil.initCap(part));
} catch (NoSuchMethodException e) {
try {
meth = root.getClass().getMethod("is" + AuraTextUtil.initCap(part));
} catch (NoSuchMethodException nme) {
throw makeException("no such property: " + part, e, def);
}
}
try {
ret = meth.invoke(root);
} catch (IllegalAccessException iae) {
throw makeException("no such property: " + part, iae, def);
} catch (InvocationTargetException ite) {
throw makeException(ite.getCause().getMessage(), ite.getCause(), def);
}
loggingService.incrementNum("JavaCallCount");
}
ValueProvider vp;
if (def != null) {
TypeDef typeDef = def.getType(part);
vp = (ret instanceof ValueProvider) ? (ValueProvider) ret : (ValueProvider) typeDef.wrap(ret);
} else {
vp = (ret instanceof ValueProvider) ? (ValueProvider) ret : new JavaValueProvider(ret);
}
if (stem != null) {
ret = vp.getValue(stem);
}
} catch (AuraRuntimeException lre) {
throw lre;
} catch (Exception e) {
throw makeException(e.getMessage(), e, def);
} finally {
loggingService.stopTimer("java");
loggingService.startTimer(LoggingService.TIMER_AURA);
}
return ret;
}