NIFI-43: Do not throw InvocationTargetException if it is wrapping a RuntimeException; instead just throw the RuntimeException

This commit is contained in:
Mark Payne 2014-12-15 13:14:42 -05:00
parent 73cc6cbe28
commit e04a55d3a5
1 changed files with 40 additions and 32 deletions

View File

@ -42,43 +42,51 @@ public class ReflectionUtils {
* @throws IllegalAccessException * @throws IllegalAccessException
*/ */
public static void invokeMethodsWithAnnotation(final Class<? extends Annotation> annotation, final Object instance, final Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { public static void invokeMethodsWithAnnotation(final Class<? extends Annotation> annotation, final Object instance, final Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for (final Method method : instance.getClass().getMethods()) { try {
if (method.isAnnotationPresent(annotation)) { for (final Method method : instance.getClass().getMethods()) {
final boolean isAccessible = method.isAccessible(); if (method.isAnnotationPresent(annotation)) {
method.setAccessible(true); final boolean isAccessible = method.isAccessible();
method.setAccessible(true);
try {
final Class<?>[] argumentTypes = method.getParameterTypes(); try {
if (argumentTypes.length > args.length) { final Class<?>[] argumentTypes = method.getParameterTypes();
throw new IllegalArgumentException(String.format("Unable to invoke method %1$s on %2$s because method expects %3$s parameters but only %4$s were given", if (argumentTypes.length > args.length) {
method.getName(), instance, argumentTypes.length, args.length)); throw new IllegalArgumentException(String.format("Unable to invoke method %1$s on %2$s because method expects %3$s parameters but only %4$s were given",
} method.getName(), instance, argumentTypes.length, args.length));
for (int i = 0; i < argumentTypes.length; i++) {
final Class<?> argType = argumentTypes[i];
if (!argType.isAssignableFrom(args[i].getClass())) {
throw new IllegalArgumentException(String.format(
"Unable to invoke method %1$s on %2$s because method parameter %3$s is expected to be of type %4$s but argument passed was of type %5$s",
method.getName(), instance, i, argType, args[i].getClass()));
} }
}
for (int i = 0; i < argumentTypes.length; i++) {
if (argumentTypes.length == args.length) { final Class<?> argType = argumentTypes[i];
method.invoke(instance, args); if (!argType.isAssignableFrom(args[i].getClass())) {
} else { throw new IllegalArgumentException(String.format(
final Object[] argsToPass = new Object[argumentTypes.length]; "Unable to invoke method %1$s on %2$s because method parameter %3$s is expected to be of type %4$s but argument passed was of type %5$s",
for (int i = 0; i < argsToPass.length; i++) { method.getName(), instance, i, argType, args[i].getClass()));
argsToPass[i] = args[i]; }
}
if (argumentTypes.length == args.length) {
method.invoke(instance, args);
} else {
final Object[] argsToPass = new Object[argumentTypes.length];
for (int i = 0; i < argsToPass.length; i++) {
argsToPass[i] = args[i];
}
method.invoke(instance, argsToPass);
}
} finally {
if (!isAccessible) {
method.setAccessible(false);
} }
method.invoke(instance, argsToPass);
}
} finally {
if (!isAccessible) {
method.setAccessible(false);
} }
} }
} }
} catch (final InvocationTargetException ite) {
if ( ite.getCause() instanceof RuntimeException ) {
throw (RuntimeException) ite.getCause();
} else {
throw ite;
}
} }
} }