import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
/**
* Invokes a method, masking with a runtime exception all the exceptions.
*
* @param obj The object from which a method will be called.
* @param method The method to call.
* @param args The arguments of the method.
* @return The object returned, if the method is not "void".
* @since 2.0.7
*/
public static Object invokeMethod(Object obj, Method method, Object... args) {
try {
return method.invoke(obj, args);
} catch (IllegalArgumentException e) {
throw new RuntimeException("The arguments for '"
+ method.getName() + "' in class '"
+ obj.getClass().getName() + "' are not valid", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot access '"
+ method.getName() + "' in class '"
+ obj.getClass().getName() + "'", e);
} catch (InvocationTargetException e) {
throw new RuntimeException(
"An exception has been thrown inside '" + method.getName()
+ "' in class '" + obj.getClass().getName() + "'", e);
}
}
}
|