You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2002/11/18 23:18:44 UTC
cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/reflect MethodUtilsTestCase.java
rdonkin 2002/11/18 14:18:44
Modified: lang/src/java/org/apache/commons/lang/reflect
MethodUtils.java
lang/src/test/org/apache/commons/lang/reflect
MethodUtilsTestCase.java
Log:
Remove invokeExactMethod. Going to consolidate on one inokeMethod that considers superclasses and doesn't break scope rules.
Revision Changes Path
1.3 +1 -138 jakarta-commons/lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java
Index: MethodUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/reflect/MethodUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MethodUtils.java 14 Nov 2002 18:51:57 -0000 1.2
+++ MethodUtils.java 18 Nov 2002 22:18:44 -0000 1.3
@@ -192,11 +192,6 @@
/**
* <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 @@
/**
* <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 @@
/**
* <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 @@
args = ArrayUtils.EMPTY_OBJECT_ARRAY;
}
-//return null;
Method method = getMatchingAccessibleMethod(
object.getClass(),
methodName,
@@ -347,126 +330,6 @@
methodName + "() on object: " + object.getClass().getName());
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
1.2 +14 -14 jakarta-commons/lang/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java
Index: MethodUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/reflect/MethodUtilsTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MethodUtilsTestCase.java 14 Nov 2002 18:53:36 -0000 1.1
+++ MethodUtilsTestCase.java 18 Nov 2002 22:18:44 -0000 1.2
@@ -171,16 +171,16 @@
/**
- * <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 @@
// METHOD TWO FAILURE
try {
- Object ret = MethodUtils.invokeExactMethod(
+ Object ret = MethodUtils.invokeMethod(
privateBeanFactory.create(),
"methodBar",
"ANOTHER TEST");
@@ -212,7 +212,7 @@
// METHOD THREE
try {
- Object ret = MethodUtils.invokeExactMethod(
+ Object ret = MethodUtils.invokeMethod(
privateBeanFactory.createSubclass(),
"methodBaz",
"YET ANOTHER TEST");
@@ -301,8 +301,8 @@
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 @@
/**
- * Simple tests for accessing static methods via invokeExactMethod().
+ * Simple tests for accessing static methods via invokeMethod().
*/
public void testSimpleStatic2() {
@@ -418,7 +418,7 @@
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 @@
((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 @@
((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",
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>