Good catch!

Gary

---------- Forwarded message ----------
From: <pascalschumac...@apache.org>
Date: Sat, Oct 22, 2016 at 7:50 AM
Subject: [lang] MethodUtils#invokeMethod(Object object, boolean
forceAccess, String methodName, Object[] args, Class<?>[] parameterTypes):
do not restore the accessibility of the method object, because
Method#setAccessible only modifies the behavior of the Access
To: comm...@commons.apache.org


Repository: commons-lang
Updated Branches:
  refs/heads/master 8274993e2 -> e2dbe55d5


MethodUtils#invokeMethod(Object object, boolean forceAccess, String
methodName, Object[] args, Class<?>[] parameterTypes): do not restore the
accessibility of the method object, because Method#setAccessible only
modifies the behavior of the AccessibleObject not of the actual method.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e2dbe55d
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e2dbe55d
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e2dbe55d

Branch: refs/heads/master
Commit: e2dbe55d56bd6c1209276050f527d36717e33e1d
Parents: 8274993
Author: pascalschumacher <pascalschumac...@gmx.net>
Authored: Sat Oct 22 16:39:11 2016 +0200
Committer: pascalschumacher <pascalschumac...@gmx.net>
Committed: Sat Oct 22 16:49:12 2016 +0200

----------------------------------------------------------------------
 .../commons/lang3/reflect/MethodUtils.java      | 51 ++++++++------------
 .../commons/lang3/reflect/MethodUtilsTest.java  |  3 --
 2 files changed, 19 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/
e2dbe55d/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
index 388eba7..29fbf37 100644
--- a/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
+++ b/src/main/java/org/apache/commons/lang3/reflect/MethodUtils.java
@@ -199,49 +199,36 @@ public class MethodUtils {
      */
     public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName,
             Object[] args, Class<?>[] parameterTypes)
-            throws NoSuchMethodException, IllegalAccessException,
-            InvocationTargetException {
+            throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
         parameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
         args = ArrayUtils.nullToEmpty(args);

         final String messagePrefix;
         Method method = null;
-        boolean isOriginallyAccessible = false;
-        Object result = null;

-        try {
-            if (forceAccess) {
-                messagePrefix = "No such method: ";
-                method = getMatchingMethod(object.getClass(),
-                        methodName, parameterTypes);
-                if (method != null) {
-                    isOriginallyAccessible = method.isAccessible();
-                    if (!isOriginallyAccessible) {
-                        method.setAccessible(true);
-                    }
+        if (forceAccess) {
+            messagePrefix = "No such method: ";
+            method = getMatchingMethod(object.getClass(),
+                    methodName, parameterTypes);
+            if (method != null) {
+                if (!method.isAccessible()) {
+                    method.setAccessible(true);
                 }
-            } else {
-                messagePrefix = "No such accessible method: ";
-                method = getMatchingAccessibleMethod(object.getClass(),
-                        methodName, parameterTypes);
-            }
-
-            if (method == null) {
-                throw new NoSuchMethodException(messagePrefix
-                        + methodName + "() on object: "
-                        + object.getClass().getName());
             }
-            args = toVarArgs(method, args);
-
-            result = method.invoke(object, args);
+        } else {
+            messagePrefix = "No such accessible method: ";
+            method = getMatchingAccessibleMethod(object.getClass(),
+                    methodName, parameterTypes);
         }
-        finally {
-            if (method != null && forceAccess && method.isAccessible() !=
isOriginallyAccessible) {
-                method.setAccessible(isOriginallyAccessible);
-            }
+
+        if (method == null) {
+            throw new NoSuchMethodException(messagePrefix
+                    + methodName + "() on object: "
+                    + object.getClass().getName());
         }
+        args = toVarArgs(method, args);

-        return result;
+        return method.invoke(object, args);
     }

     /**

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/
e2dbe55d/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
index ec755f2..fc78008 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java
@@ -763,10 +763,7 @@ public class MethodUtilsTest {

     @Test
     public void testInvokeMethodForceAccessNoArgs() throws Exception {
-        Method privateStringStuffMethod =
MethodUtils.getMatchingMethod(TestBean.class,
"privateStringStuff");
-        Assert.assertFalse(privateStringStuffMethod.isAccessible());
         Assert.assertEquals("privateStringStuff()",
MethodUtils.invokeMethod(testBean, true, "privateStringStuff"));
-        Assert.assertFalse(privateStringStuffMethod.isAccessible());
     }

     @Test




-- 
E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
Java Persistence with Hibernate, Second Edition
<https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459>
JUnit in Action, Second Edition
<https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021>
Spring Batch in Action
<https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action>
<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Reply via email to