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 388eba789..29fbf3759 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); - } - finally { - if (method != null && forceAccess && method.isAccessible() != isOriginallyAccessible) { - method.setAccessible(isOriginallyAccessible); } + } else { + messagePrefix = "No such accessible method: "; + method = getMatchingAccessibleMethod(object.getClass(), + methodName, parameterTypes); } - return result; + if (method == null) { + throw new NoSuchMethodException(messagePrefix + + methodName + "() on object: " + + object.getClass().getName()); + } + args = toVarArgs(method, args); + + return method.invoke(object, args); } /** 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 ec755f21d..fc7800823 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