Converted invokeMethod to use ReflectionExceptions

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Burrell Donkin 2002-11-21 18:53:32 +00:00
parent f9ad966250
commit 4f24da86c7
2 changed files with 27 additions and 28 deletions

View File

@ -82,7 +82,7 @@ import org.apache.commons.lang.StringUtils;
* @author Gregor Raýman * @author Gregor Raýman
* @author Jan Sorensen * @author Jan Sorensen
* @author Robert Burrell Donkin * @author Robert Burrell Donkin
* @version $Id: MethodUtils.java,v 1.6 2002/11/20 22:31:40 rdonkin Exp $ * @version $Id: MethodUtils.java,v 1.7 2002/11/21 18:53:32 rdonkin Exp $
*/ */
public class MethodUtils { public class MethodUtils {
@ -204,11 +204,7 @@ public class MethodUtils {
* @param methodName get method with this name, must not be null * @param methodName get method with this name, must not be null
* @param arg use this argument, must not be null * @param arg use this argument, must not be null
* *
* @throws NoSuchMethodException if there is no such accessible method * @throws ReflectionException if an error occurs during reflection
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
* @throws IllegalArgumentException if any parameter is null * @throws IllegalArgumentException if any parameter is null
*/ */
public static Object invokeMethod( public static Object invokeMethod(
@ -216,9 +212,7 @@ public class MethodUtils {
String methodName, String methodName,
Object arg) Object arg)
throws throws
NoSuchMethodException, ReflectionException {
IllegalAccessException,
InvocationTargetException {
if (objectToInvoke == null) { if (objectToInvoke == null) {
throw new IllegalArgumentException("The object to invoke must not be null"); throw new IllegalArgumentException("The object to invoke must not be null");
@ -248,11 +242,7 @@ public class MethodUtils {
* @param methodName get method with this name, must not be null * @param methodName get method with this name, must not be null
* @param args use these arguments - treat null as empty array * @param args use these arguments - treat null as empty array
* *
* @throws NoSuchMethodException if there is no such accessible method * @throws ReflectionException if an error occurs during reflection
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
* @throws IllegalArgumentException if the objectToInvoke, methodName or any argument is null * @throws IllegalArgumentException if the objectToInvoke, methodName or any argument is null
*/ */
public static Object invokeMethod( public static Object invokeMethod(
@ -260,9 +250,7 @@ public class MethodUtils {
String methodName, String methodName,
Object[] args) Object[] args)
throws throws
NoSuchMethodException, ReflectionException {
IllegalAccessException,
InvocationTargetException {
if (objectToInvoke == null) { if (objectToInvoke == null) {
throw new IllegalArgumentException("The object to invoke must not be null"); throw new IllegalArgumentException("The object to invoke must not be null");
@ -298,11 +286,7 @@ public class MethodUtils {
* @param args use these arguments - treat null as empty array * @param args use these arguments - treat null as empty array
* @param parameterTypes match these parameters - treat null as empty array * @param parameterTypes match these parameters - treat null as empty array
* *
* @throws NoSuchMethodException if there is no such accessible method * @throws ReflectionException if an error occurs during reflection
* @throws InvocationTargetException wraps an exception thrown by the
* method invoked
* @throws IllegalAccessException if the requested method is not accessible
* via reflection
*/ */
public static Object invokeMethod( public static Object invokeMethod(
Object object, Object object,
@ -310,9 +294,7 @@ public class MethodUtils {
Object[] args, Object[] args,
Class[] parameterTypes) Class[] parameterTypes)
throws throws
NoSuchMethodException, ReflectionException {
IllegalAccessException,
InvocationTargetException {
if (parameterTypes == null) { if (parameterTypes == null) {
parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY; parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
@ -326,9 +308,26 @@ public class MethodUtils {
methodName, methodName,
parameterTypes); parameterTypes);
if (method == null) if (method == null)
throw new NoSuchMethodException("No such accessible method: " + throw new ReflectionException("No such accessible method: " +
methodName + "() on object: " + object.getClass().getName()); methodName + "() on object: " + object.getClass().getName());
return method.invoke(object, args);
try {
return method.invoke(object, args);
} catch (IllegalAccessException ex) {
throw new ReflectionException(
ReflectionUtils.getThrowableText(
ex, "invoking method", object.getClass().getName(), parameterTypes, methodName)
, ex);
} catch (InvocationTargetException ex) {
throw new ReflectionException(
ReflectionUtils.getThrowableText(
ex, "invoking method", object.getClass().getName(), parameterTypes, methodName)
, ex);
}
} }
/** /**

View File

@ -295,7 +295,7 @@ public class MethodUtilsTestCase extends TestCase {
// should get here! // should get here!
fail("No exception thrown when no appropriate method exists"); fail("No exception thrown when no appropriate method exists");
} catch (NoSuchMethodException e) { } catch (ReflectionException e) {
// this is what we're expecting! // this is what we're expecting!
} }