commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1166233 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/reflect/MethodUtils.java site/changes/changes.xml test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
Date Wed, 07 Sep 2011 15:39:45 GMT
Author: ggregory
Date: Wed Sep  7 15:39:45 2011
New Revision: 1166233

URL: http://svn.apache.org/viewvc?rev=1166233&view=rev
Log:
[LANG-750] Add MethodUtil APIs to call methods without parameters.

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
    commons/proper/lang/trunk/src/site/changes/changes.xml
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java?rev=1166233&r1=1166232&r2=1166233&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
Wed Sep  7 15:39:45 2011
@@ -59,6 +59,33 @@ public class MethodUtils {
     }
 
     /**
+     * <p>Invokes a named method without parameters.</p>
+     *
+     * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class,
String, Class[])}.</p>
+     *
+     * <p>This method supports calls to methods taking primitive parameters 
+     * via passing in wrapping classes. So, for example, a <code>Boolean</code>
object
+     * would match a <code>boolean</code> primitive.</p>
+     *
+     * <p>This is a convenient wrapper for
+     * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
+     * </p>
+     *
+     * @param object invoke method on this object
+     * @param methodName get method with this name
+     * @return The value returned by the invoked method
+     *
+     * @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
+     * @since 3.0.2
+     */
+    public static Object invokeMethod(Object object, String methodName) throws NoSuchMethodException,
+            IllegalAccessException, InvocationTargetException {
+        return invokeMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY);
+    }
+
+    /**
      * <p>Invokes a named method whose parameter type matches the object type.</p>
      *
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class,
String, Class[])}.</p>
@@ -134,6 +161,28 @@ public class MethodUtils {
     }
 
     /**
+     * <p>Invokes a method without parameters.</p>
+     *
+     * <p>This uses reflection to invoke the method obtained from a call to
+     * <code>getAccessibleMethod()</code>.</p>
+     *
+     * @param object invoke method on this object
+     * @param methodName get method with this name
+     * @return The value returned by the invoked method
+     *
+     * @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
+     * @since 3.0.2
+     */
+    public static Object invokeExactMethod(Object object, String methodName) throws NoSuchMethodException,
+            IllegalAccessException, InvocationTargetException {
+        return invokeExactMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY);
+    }
+
+    /**
      * <p>Invokes a method whose parameter types match exactly the object
      * types.</p>
      *
@@ -242,6 +291,35 @@ public class MethodUtils {
     }
 
     /**
+     * <p>Invokes a named static method without parameters.</p>
+     *
+     * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class,
String, Class[])}.</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 cls invoke static method on this class
+     * @param methodName get method with this name
+     * @return The value returned by the invoked method
+     *
+     * @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
+     * @since 3.0.2
+     */
+    public static Object invokeStaticMethod(Class<?> cls, String methodName) throws
NoSuchMethodException,
+            IllegalAccessException, InvocationTargetException {
+        return invokeStaticMethod(cls, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY);
+    }
+
+    /**
      * <p>Invokes a named static method whose parameter type matches the object type.</p>
      *
      * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class,
String, Class[])}.</p>
@@ -321,6 +399,28 @@ public class MethodUtils {
     }
 
     /**
+     * <p>Invokes a static method without parameters.</p>
+     *
+     * <p>This uses reflection to invoke the method obtained from a call to
+     * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
+     *
+     * @param cls invoke static method on this class
+     * @param methodName get method with this name
+     * @return The value returned by the invoked method
+     *
+     * @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
+     * @since 3.0.2
+     */
+    public static Object invokeExactStaticMethod(Class<?> cls, String methodName) throws
NoSuchMethodException,
+            IllegalAccessException, InvocationTargetException {
+        return invokeExactStaticMethod(cls, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY);
+    }
+
+    /**
      * <p>Invokes a static method whose parameter types match exactly the object
      * types.</p>
      *

Modified: commons/proper/lang/trunk/src/site/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/site/changes/changes.xml?rev=1166233&r1=1166232&r2=1166233&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/site/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/site/changes/changes.xml Wed Sep  7 15:39:45 2011
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.0.2" date="unreleased" description="September release">  
+    <action type="add" issue="LANG-750">Add MethodUtil APIs to call methods without
parameters</action>
     <action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex:
0X and -0X</action>
     <action type="update" issue="LANG-736">CharUtils static final array CHAR_STRING
is not needed to compute CHAR_STRING_ARRAY</action>
     <action type="fix" issue="LANG-744">StringUtils throws java.security.AccessControlException
on Google App Engine</action>

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java?rev=1166233&r1=1166232&r2=1166233&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
Wed Sep  7 15:39:45 2011
@@ -34,7 +34,6 @@ import org.apache.commons.lang3.math.Num
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
@@ -77,6 +76,10 @@ public class MethodUtilsTest {
             return "bar(Object)";
         }
         
+        public static void oneParameterStatic(String s) {
+            // empty
+        }
+
         @SuppressWarnings("unused")
         private void privateStuff() {
         }
@@ -159,6 +162,16 @@ public class MethodUtilsTest {
     }
 
     @Test
+    public void testInvokeMethodNoParam() throws Exception {
+        assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo"));
+    }
+
+    @Test(expected = NoSuchMethodException.class)
+    public void testInvokeMethodNoParamFailure() throws Exception {
+        assertEquals("oneParameter()", MethodUtils.invokeMethod(testBean, "oneParameter"));
+    }
+
+    @Test
     public void testInvokeExactMethod() throws Exception {
         assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo",
                 (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
@@ -197,7 +210,12 @@ public class MethodUtilsTest {
 
     @Test
     public void testInvokeExactMethodNoParam() throws Exception {
-        //assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo"));
+        assertEquals("foo()", MethodUtils.invokeExactMethod(testBean, "foo"));
+    }
+
+    @Test(expected = NoSuchMethodException.class)
+    public void testInvokeExactMethodNoParamFailure() throws Exception {
+        MethodUtils.invokeExactMethod(testBean, "oneParameter");
     }
 
     @Test
@@ -231,6 +249,16 @@ public class MethodUtilsTest {
     }
 
     @Test
+    public void testInvokeStaticMethodNoParam() throws Exception {
+        assertEquals("bar()", MethodUtils.invokeStaticMethod(TestBean.class, "bar"));
+    }
+
+    @Test(expected = NoSuchMethodException.class)
+    public void testInvokeStaticMethodNoParamFailure() throws Exception {
+        assertEquals("oneParameter()", MethodUtils.invokeStaticMethod(TestBean.class, "oneParameter"));
+    }
+
+    @Test
     public void testInvokeExactStaticMethod() throws Exception {
         assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class,
                 "bar", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
@@ -269,6 +297,16 @@ public class MethodUtilsTest {
     }
 
     @Test
+    public void testInvokeExactStaticMethodNoParam() throws Exception {
+        assertEquals("bar()", MethodUtils.invokeExactStaticMethod(TestBean.class, "bar"));
+    }
+
+    @Test(expected = NoSuchMethodException.class)
+    public void testInvokeExactStaticMethodNoParamFailure() throws Exception {
+        assertEquals("oneParameterStatic()", MethodUtils.invokeExactStaticMethod(TestBean.class,
"oneParameterStatic"));
+    }
+
+    @Test
     public void testGetAccessibleInterfaceMethod() throws Exception {
         Class<?>[][] p = { ArrayUtils.EMPTY_CLASS_ARRAY, null };
         for (Class<?>[] element : p) {



Mime
View raw message