You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2006/12/27 00:25:42 UTC
svn commit: r490398 - in /jakarta/commons/proper/beanutils/trunk/src:
java/org/apache/commons/beanutils/MethodUtils.java
test/org/apache/commons/beanutils/MethodUtilsTestCase.java
test/org/apache/commons/beanutils/TestBean.java
Author: bayard
Date: Tue Dec 26 15:25:41 2006
New Revision: 490398
URL: http://svn.apache.org/viewvc?view=rev&rev=490398
Log:
Applied modified patch for Nestor Boscan's static method variants as reported in #BEANUTILS-193. Have also added a unit test for the static method calls.
Modified:
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java?view=diff&rev=490398&r1=490397&r2=490398
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java Tue Dec 26 15:25:41 2006
@@ -364,6 +364,265 @@
}
+ /**
+ * <p>Invoke a static 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 objectClass invoke static method on this class
+ * @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 invokeExactStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object[] args,
+ Class[] parameterTypes)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+
+ if (args == null) {
+ args = emptyObjectArray;
+ }
+
+ if (parameterTypes == null) {
+ parameterTypes = emptyClassArray;
+ }
+
+ Method method = getAccessibleMethod(
+ objectClass,
+ methodName,
+ parameterTypes);
+ if (method == null)
+ throw new NoSuchMethodException("No such accessible method: " +
+ methodName + "() on class: " + objectClass.getName());
+ return method.invoke(null, args);
+
+ }
+
+ /**
+ * <p>Invoke a named static 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>
+ *
+ * <p> This is a convenient wrapper for
+ * {@link #invokeStaticMethod(Class objectClass,String methodName,Object [] args)}.
+ * </p>
+ *
+ * @param objectClass invoke static method on this class
+ * @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 invokeStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object arg)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+
+ Object[] args = {arg};
+ return invokeStaticMethod (objectClass, methodName, args);
+
+ }
+
+
+ /**
+ * <p>Invoke a named static 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>
+ *
+ * <p> This is a convenient wrapper for
+ * {@link #invokeStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes)}.
+ * </p>
+ *
+ * @param objectClass invoke static method on this class
+ * @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 invokeStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object[] args)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+
+ if (args == null) {
+ args = emptyObjectArray;
+ }
+ int arguments = args.length;
+ Class parameterTypes [] = new Class[arguments];
+ for (int i = 0; i < arguments; i++) {
+ parameterTypes[i] = args[i].getClass();
+ }
+ return invokeStaticMethod (objectClass, methodName, args, parameterTypes);
+
+ }
+
+
+ /**
+ * <p>Invoke a named static method whose parameter type matches the object type.</p>
+ *
+ * <p>The behaviour of this method is less deterministic
+ * than {@link
+ * #invokeExactStaticMethod(Class objectClass,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>
+ *
+ *
+ * @param objectClass invoke static method on this class
+ * @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 invokeStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object[] args,
+ Class[] parameterTypes)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+
+ if (parameterTypes == null) {
+ parameterTypes = emptyClassArray;
+ }
+ if (args == null) {
+ args = emptyObjectArray;
+ }
+
+ Method method = getMatchingAccessibleMethod(
+ objectClass,
+ methodName,
+ parameterTypes);
+ if (method == null)
+ throw new NoSuchMethodException("No such accessible method: " +
+ methodName + "() on class: " + objectClass.getName());
+ return method.invoke(null, args);
+ }
+
+
+ /**
+ * <p>Invoke a static method whose parameter type matches exactly the object
+ * type.</p>
+ *
+ * <p> This is a convenient wrapper for
+ * {@link #invokeExactStaticMethod(Class objectClass,String methodName,Object [] args)}.
+ * </p>
+ *
+ * @param objectClass invoke static method on this class
+ * @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 invokeExactStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object arg)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+
+ Object[] args = {arg};
+ return invokeExactStaticMethod (objectClass, methodName, args);
+
+ }
+
+
+ /**
+ * <p>Invoke a static 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 objectClass invoke static method on this class
+ * @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 invokeExactStaticMethod(
+ Class objectClass,
+ String methodName,
+ Object[] args)
+ throws
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException {
+ if (args == null) {
+ args = emptyObjectArray;
+ }
+ int arguments = args.length;
+ Class parameterTypes [] = new Class[arguments];
+ for (int i = 0; i < arguments; i++) {
+ parameterTypes[i] = args[i].getClass();
+ }
+ return invokeExactStaticMethod(objectClass, methodName, args, parameterTypes);
+
+ }
+
/**
* <p>Return an accessible method (that is, one that can be invoked via
Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java?view=diff&rev=490398&r1=490397&r2=490398
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java Tue Dec 26 15:25:41 2006
@@ -301,6 +301,34 @@
assertEquals("Set float property using invokeMethod", 25.5d, bean.getDouble(), 0.01d);
}
+ public void testStaticInvokeMethod() throws Exception {
+
+ Object value = null;
+ int current = TestBean.currentCounter();
+
+ value = MethodUtils.invokeStaticMethod(TestBean.class, "currentCounter", new Object[0]);
+ assertEquals("currentCounter value", current, ((Integer) value).intValue());
+
+ MethodUtils.invokeStaticMethod(TestBean.class, "incrementCounter", new Object[0]);
+ current++;
+
+ value = MethodUtils.invokeStaticMethod(TestBean.class, "currentCounter", new Object[0]);
+ assertEquals("currentCounter value", current, ((Integer) value).intValue());
+
+ MethodUtils.invokeStaticMethod(TestBean.class, "incrementCounter", new Object[] { new Integer(8) } );
+ current += 8;
+
+ value = MethodUtils.invokeStaticMethod(TestBean.class, "currentCounter", new Object[0]);
+ assertEquals("currentCounter value", current, ((Integer) value).intValue());
+
+ MethodUtils.invokeExactStaticMethod(TestBean.class, "incrementCounter",
+ new Object[] { new Integer(8) }, new Class[] { Number.class } );
+ current += 16;
+
+ value = MethodUtils.invokeStaticMethod(TestBean.class, "currentCounter", new Object[0]);
+ assertEquals("currentCounter value", current, ((Integer) value).intValue());
+ }
+
/**
* Simple tests for accessing static methods via invokeMethod().
Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java?view=diff&rev=490398&r1=490397&r2=490398
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java Tue Dec 26 15:25:41 2006
@@ -555,7 +555,6 @@
}
-
/**
* Increment the current value of the counter by the specified amount.
*
@@ -567,5 +566,14 @@
}
+ /**
+ * Increments the current value of the count by the
+ * specified amount * 2. It has the same name
+ * as the method above so as to test the looseness
+ * of getMethod.
+ */
+ public static void incrementCounter(Number amount) {
+ counter += 2 * amount.intValue();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org