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