You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2013/02/24 13:10:14 UTC

svn commit: r1449465 - in /commons/proper/beanutils/trunk/src: changes/changes.xml main/java/org/apache/commons/beanutils/MethodUtils.java test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java

Author: britter
Date: Sun Feb 24 12:10:13 2013
New Revision: 1449465

URL: http://svn.apache.org/r1449465
Log:
[BEANUTILS-408] - MethodUtils.invokeMethod() throws NullPointerException when args==null

Modified:
    commons/proper/beanutils/trunk/src/changes/changes.xml
    commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java
    commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java

Modified: commons/proper/beanutils/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/changes/changes.xml?rev=1449465&r1=1449464&r2=1449465&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/changes/changes.xml (original)
+++ commons/proper/beanutils/trunk/src/changes/changes.xml Sun Feb 24 12:10:13 2013
@@ -40,6 +40,9 @@ The <action> type attribute can be add,u
   <body>
 
     <release version="1.8.4" date="in SVN" description="Bug fix for 1.8.3">
+      <action dev="britter" type="fix" issue="BEANUTILS-408" >
+         MethodUtils.invokeMethod() throws NullPointerException when args==null
+      </action>
       <action dev="britter" type="fix" issue="BEANUTILS-426" >
          ConstructorUtils.invokeConstructor(Class klass, Object arg) throws 
          NullPointerException when arg==null

Modified: commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java?rev=1449465&r1=1449464&r2=1449465&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java (original)
+++ commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/MethodUtils.java Sun Feb 24 12:10:13 2013
@@ -173,7 +173,7 @@ public class MethodUtils {
             IllegalAccessException,
             InvocationTargetException {
 
-        Object[] args = {arg};
+        Object[] args = toArray(arg);
         return invokeMethod(object, methodName, args);
     }
 
@@ -309,7 +309,7 @@ public class MethodUtils {
             IllegalAccessException,
             InvocationTargetException {
 
-        Object[] args = {arg};
+        Object[] args = toArray(arg);
         return invokeExactMethod(object, methodName, args);
     }
 
@@ -487,7 +487,7 @@ public class MethodUtils {
             IllegalAccessException,
             InvocationTargetException {
 
-        Object[] args = {arg};
+        Object[] args = toArray(arg);
         return invokeStaticMethod (objectClass, methodName, args);
     }
 
@@ -626,11 +626,10 @@ public class MethodUtils {
             IllegalAccessException,
             InvocationTargetException {
 
-        Object[] args = {arg};
+        Object[] args = toArray(arg);
         return invokeExactStaticMethod (objectClass, methodName, args);
     }
 
-
     /**
      * <p>Invoke a static method whose parameter types match exactly the object
      * types.</p>
@@ -671,6 +670,14 @@ public class MethodUtils {
     }
 
 
+    private static Object[] toArray(Object arg) {
+        Object[] args = {arg};
+        if (arg == null) {
+            args = null;
+        }
+        return 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

Modified: commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java?rev=1449465&r1=1449464&r2=1449465&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java (original)
+++ commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java Sun Feb 24 12:10:13 2013
@@ -185,6 +185,13 @@ public class MethodUtilsTestCase extends
         }
     }
 
+    public void testInvokeExactMethodNull() throws Exception {
+        Object object = new Object();
+        Object result = MethodUtils.invokeExactMethod(object, "toString", (Object) null);
+        assertEquals(object.toString(), result);
+    }
+
+
     /**
      * <p> Test <code>invokeMethod</code>.
      */
@@ -261,6 +268,12 @@ public class MethodUtilsTestCase extends
         MethodUtils.invokeExactMethod(parent, "getName", null, null);
     }
 
+    public void testInvokeMethodNull() throws Exception {
+        Object object = new Object();
+        Object result = MethodUtils.invokeMethod(object, "toString", (Object) null);
+        assertEquals(object.toString(), result);
+    }
+
     /**
      * <p> Test <code>invokeMethod</code> with a primitive.
      */
@@ -328,6 +341,17 @@ public class MethodUtilsTestCase extends
         assertEquals("currentCounter value", current, ((Integer) value).intValue());
     }
 
+    public void testInvokeStaticMethodNull() throws Exception {
+        int current = TestBean.currentCounter();
+        Object value = MethodUtils.invokeStaticMethod(TestBean.class, "currentCounter", (Object) null);
+        assertEquals("currentCounter value", current, ((Integer) value).intValue());
+    }
+
+    public void testInvokeExactStaticMethodNull() throws Exception {
+        int current = TestBean.currentCounter();
+        Object value = MethodUtils.invokeExactStaticMethod(TestBean.class, "currentCounter", (Object) null);
+        assertEquals("currentCounter value", current, ((Integer) value).intValue());
+    }
 
     /**
      * Simple tests for accessing static methods via invokeMethod().



Re: svn commit: r1449465 - in /commons/proper/beanutils/trunk/src: changes/changes.xml main/java/org/apache/commons/beanutils/MethodUtils.java test/java/org/apache/commons/beanutils/MethodUtilsTestCase.java

Posted by Simone Tripodi <si...@apache.org>.
Hi again,

+    private static Object[] toArray(Object arg) {
+        Object[] args = {arg};
+        if (arg == null) {
+            args = null;
+        }
+        return args;
+    }

as I wrote you in the other email.

best,
-Simo

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org