in empire-db/src/main/java/org/apache/empire/commons/ClassUtils.java [442:499]
public static synchronized Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean includePrivateMethods)
{
// check arguments
if (object==null)
throw new InvalidArgumentException("object", object);
if (clazz==null || !clazz.isInstance(object))
throw new InvalidArgumentException("clazz", clazz);
if (StringUtils.isEmpty(methodName))
throw new InvalidArgumentException("methodName", methodName);
// begin
boolean accessible = true;
Method method = null;
try
{ // find and invoke
method = (includePrivateMethods ? clazz.getDeclaredMethod(methodName) : clazz.getMethod(methodName));
accessible = method.isAccessible();
if (includePrivateMethods && accessible==false)
method.setAccessible(true);
// invoke
return method.invoke(object);
}
catch (NoSuchMethodException e)
{ // No such Method
if (includePrivateMethods)
{ // try superclass
clazz = clazz.getSuperclass();
if (clazz!=null && !clazz.equals(java.lang.Object.class))
return invokeSimpleMethod(clazz, object, methodName, true);
}
// not found
return null;
}
catch (SecurityException e)
{ // Invalid Method definition
throw new NotSupportedException(object, methodName, e);
}
catch (IllegalAccessException e)
{ // Invalid Method definition
throw new NotSupportedException(object, methodName, e);
}
catch (IllegalArgumentException e)
{ // Invalid Method definition
throw new NotSupportedException(object, methodName, e);
}
catch (InvocationTargetException e)
{ // Error inside Method
Throwable cause = e.getCause();
if (cause instanceof EmpireException)
throw (EmpireException)cause;
// wrap
throw new InternalException(cause);
}
finally {
// restore accessible
if (method!=null && accessible==false)
method.setAccessible(false);
}
}