diff --git a/src/java/org/apache/commons/lang/reflect/MethodUtils.java b/src/java/org/apache/commons/lang/reflect/MethodUtils.java index 44b5fcc00..6c523de52 100644 --- a/src/java/org/apache/commons/lang/reflect/MethodUtils.java +++ b/src/java/org/apache/commons/lang/reflect/MethodUtils.java @@ -82,7 +82,7 @@ import org.apache.commons.lang.StringUtils; * @author Gregor Raýman * @author Jan Sorensen * @author Robert Burrell Donkin - * @version $Id: MethodUtils.java,v 1.2 2002/11/14 18:51:57 rdonkin Exp $ + * @version $Id: MethodUtils.java,v 1.3 2002/11/18 22:18:44 rdonkin Exp $ */ public class MethodUtils { @@ -192,11 +192,6 @@ public class MethodUtils { /** *

Invoke a named method whose parameter type matches the object type.

* - *

The behaviour of this method is less deterministic - * than {@link #invokeExactMethod}. - * It loops through all methods with names that match - * and then executes the first it finds with compatable parameters.

- * *

This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a Boolean class * would match a boolean primitive.

@@ -241,11 +236,6 @@ public class MethodUtils { /** *

Invoke a named method whose parameter type matches the object type.

* - *

The behaviour of this method is less deterministic - * than {@link #invokeExactMethod(Object object,String methodName,Object [] args)}. - * It loops through all methods with names that match - * and then executes the first it finds with compatable parameters.

- * *

This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a Boolean class * would match a boolean primitive.

@@ -298,12 +288,6 @@ public class MethodUtils { /** *

Invoke a named method whose parameter type matches the object type.

* - *

The behaviour of this method is less deterministic - * than {@link - * #invokeExactMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}. - * It loops through all methods with names that match - * and then executes the first it finds with compatable parameters.

- * *

This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a Boolean class * would match a boolean primitive.

@@ -337,7 +321,6 @@ public class MethodUtils { args = ArrayUtils.EMPTY_OBJECT_ARRAY; } -//return null; Method method = getMatchingAccessibleMethod( object.getClass(), methodName, @@ -348,126 +331,6 @@ public class MethodUtils { return method.invoke(object, args); } - - /** - *

Invoke a method whose parameter type matches exactly the object - * type.

- * - *

This is a convenient wrapper for - * {@link #invokeExactMethod(Object object,String methodName,Object [] args)}. - *

- * - * @param object invoke method on this object - * @param methodName get method with this name - * @param arg use this argument - * - * @throws NoSuchMethodException if there is no such accessible method - * @throws InvocationTargetException wraps an exception thrown by the - * method invoked - * @throws IllegalAccessException if the requested method is not accessible - * via reflection - */ - public static Object invokeExactMethod( - Object object, - String methodName, - Object arg) - throws - NoSuchMethodException, - IllegalAccessException, - InvocationTargetException { - - Object[] args = {arg}; - return invokeExactMethod(object, methodName, args); - - } - - - /** - *

Invoke a method whose parameter types match exactly the object - * types.

- * - *

This uses reflection to invoke the method obtained from a call to - * {@link #getAccessibleMethod}.

- * - * @param object invoke method on this object - * @param methodName get method with this name - * @param args use these arguments - treat null as empty array - * - * @throws NoSuchMethodException if there is no such accessible method - * @throws InvocationTargetException wraps an exception thrown by the - * method invoked - * @throws IllegalAccessException if the requested method is not accessible - * via reflection - */ - public static Object invokeExactMethod( - Object object, - String methodName, - Object[] args) - throws - NoSuchMethodException, - IllegalAccessException, - InvocationTargetException { - if (args == null) { - args = ArrayUtils.EMPTY_OBJECT_ARRAY; - } - int arguments = args.length; - Class parameterTypes [] = new Class[arguments]; - for (int i = 0; i < arguments; i++) { - parameterTypes[i] = args[i].getClass(); - } - return invokeExactMethod(object, methodName, args, parameterTypes); - - } - - - /** - *

Invoke a method whose parameter types match exactly the parameter - * types given.

- * - *

This uses reflection to invoke the method obtained from a call to - * {@link #getAccessibleMethod}.

- * - * @param object invoke method on this object - * @param methodName get method with this name - * @param args use these arguments - 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 InvocationTargetException wraps an exception thrown by the - * method invoked - * @throws IllegalAccessException if the requested method is not accessible - * via reflection - */ - public static Object invokeExactMethod( - Object object, - String methodName, - Object[] args, - Class[] parameterTypes) - throws - NoSuchMethodException, - IllegalAccessException, - InvocationTargetException { - - if (args == null) { - args = ArrayUtils.EMPTY_OBJECT_ARRAY; - } - - if (parameterTypes == null) { - parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY; - } - - Method method = getAccessibleMethod( - object.getClass(), - methodName, - parameterTypes); - if (method == null) - throw new NoSuchMethodException("No such accessible method: " + - methodName + "() on object: " + object.getClass().getName()); - return method.invoke(object, args); - - } - - /** *

Return an accessible method (that is, one that can be invoked via * reflection) with given name and a single parameter. If no such method diff --git a/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java b/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java index 078b7bc2d..d512252eb 100644 --- a/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java +++ b/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java @@ -171,16 +171,16 @@ public class MethodUtilsTestCase extends TestCase { /** - *

Test invokeExactMethod. + *

Test invokeMethod. */ - public void testInvokeExactMethod() { - // test MethodUtils.invokeExactMethod + public void testinvokeMethod2() { + // test MethodUtils.invokeMethod // easy bit first - invoke a public method // METHOD ONE try { TestBean bean = new TestBean(); - Object ret = MethodUtils.invokeExactMethod(bean, "setStringProperty", "TEST"); + Object ret = MethodUtils.invokeMethod(bean, "setStringProperty", "TEST"); // check that the return's right and that the properties been set assertNull(ret); assertEquals("Method ONE was invoked", "TEST", bean.getStringProperty()); @@ -194,7 +194,7 @@ public class MethodUtilsTestCase extends TestCase { // METHOD TWO FAILURE try { - Object ret = MethodUtils.invokeExactMethod( + Object ret = MethodUtils.invokeMethod( privateBeanFactory.create(), "methodBar", "ANOTHER TEST"); @@ -212,7 +212,7 @@ public class MethodUtilsTestCase extends TestCase { // METHOD THREE try { - Object ret = MethodUtils.invokeExactMethod( + Object ret = MethodUtils.invokeMethod( privateBeanFactory.createSubclass(), "methodBaz", "YET ANOTHER TEST"); @@ -301,8 +301,8 @@ public class MethodUtilsTestCase extends TestCase { MethodUtils.invokeMethod(parent, "getName", null); MethodUtils.invokeMethod(parent, "getName", null, null); - MethodUtils.invokeExactMethod(parent, "getName", null); - MethodUtils.invokeExactMethod(parent, "getName", null, null); + MethodUtils.invokeMethod(parent, "getName", null); + MethodUtils.invokeMethod(parent, "getName", null, null); } @@ -407,7 +407,7 @@ public class MethodUtilsTestCase extends TestCase { /** - * Simple tests for accessing static methods via invokeExactMethod(). + * Simple tests for accessing static methods via invokeMethod(). */ public void testSimpleStatic2() { @@ -418,7 +418,7 @@ public class MethodUtilsTestCase extends TestCase { try { // Return initial value of the counter - value = MethodUtils.invokeExactMethod + value = MethodUtils.invokeMethod (bean, "currentCounter", new Object[0], new Class[0]); assertNotNull("currentCounter exists", value); assertTrue("currentCounter type", @@ -428,12 +428,12 @@ public class MethodUtilsTestCase extends TestCase { ((Integer) value).intValue()); // Increment via no-arguments version - MethodUtils.invokeExactMethod + MethodUtils.invokeMethod (bean, "incrementCounter", new Object[0], new Class[0]); // Validate updated value current++; - value = MethodUtils.invokeExactMethod + value = MethodUtils.invokeMethod (bean, "currentCounter", new Object[0], new Class[0]); assertNotNull("currentCounter exists", value); assertTrue("currentCounter type", @@ -443,14 +443,14 @@ public class MethodUtilsTestCase extends TestCase { ((Integer) value).intValue()); // Increment via specified-argument version - MethodUtils.invokeExactMethod + MethodUtils.invokeMethod (bean, "incrementCounter", new Object[] { new Integer(5) }, new Class[] { Integer.TYPE }); // Validate updated value current += 5; - value = MethodUtils.invokeExactMethod + value = MethodUtils.invokeMethod (bean, "currentCounter", new Object[0], new Class[0]); assertNotNull("currentCounter exists", value); assertTrue("currentCounter type",