Remove invokeExactMethod. Going to consolidate on one inokeMethod that considers superclasses and doesn't break scope rules.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137136 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Burrell Donkin 2002-11-18 22:18:44 +00:00
parent 97883eec49
commit dd87113102
2 changed files with 15 additions and 152 deletions

View File

@ -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 {
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>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.</p>
*
* <p>This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a <code>Boolean</code> class
* would match a <code>boolean</code> primitive.</p>
@ -241,11 +236,6 @@ public class MethodUtils {
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>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.</p>
*
* <p>This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a <code>Boolean</code> class
* would match a <code>boolean</code> primitive.</p>
@ -298,12 +288,6 @@ public class MethodUtils {
/**
* <p>Invoke a named method whose parameter type matches the object type.</p>
*
* <p>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.</p>
*
* <p>This method supports calls to methods taking primitive parameters
* via passing in wrapping classes. So, for example, a <code>Boolean</code> class
* would match a <code>boolean</code> primitive.</p>
@ -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);
}
/**
* <p>Invoke a method whose parameter type matches exactly the object
* type.</p>
*
* <p> This is a convenient wrapper for
* {@link #invokeExactMethod(Object object,String methodName,Object [] args)}.
* </p>
*
* @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);
}
/**
* <p>Invoke a method whose parameter types match exactly the object
* types.</p>
*
* <p> This uses reflection to invoke the method obtained from a call to
* {@link #getAccessibleMethod}.</p>
*
* @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);
}
/**
* <p>Invoke a method whose parameter types match exactly the parameter
* types given.</p>
*
* <p>This uses reflection to invoke the method obtained from a call to
* {@link #getAccessibleMethod}.</p>
*
* @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);
}
/**
* <p>Return an accessible method (that is, one that can be invoked via
* reflection) with given name and a single parameter. If no such method

View File

@ -171,16 +171,16 @@ public class MethodUtilsTestCase extends TestCase {
/**
* <p> Test <code>invokeExactMethod</code>.
* <p> Test <code>invokeMethod</code>.
*/
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",