From 6851655ea9cc9f43e9d9be7e9eb12179f1772af1 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Wed, 9 Sep 2009 19:49:44 +0000 Subject: [PATCH] SEC-1177: MethodInvocationUtils Returns Null With Valid Method String and Class. Added very simple checking of declared methods on class. --- .../security/util/MethodInvocationUtils.java | 22 +++++++++++++++++-- .../util/MethodInvocationUtilsTests.java | 14 ++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/springframework/security/util/MethodInvocationUtils.java b/core/src/main/java/org/springframework/security/util/MethodInvocationUtils.java index 4f29babeae..335ea1a462 100644 --- a/core/src/main/java/org/springframework/security/util/MethodInvocationUtils.java +++ b/core/src/main/java/org/springframework/security/util/MethodInvocationUtils.java @@ -83,7 +83,11 @@ public final class MethodInvocationUtils { } /** - * Generates a MethodInvocation for specified methodName on the passed class. + * Generates a MethodInvocation for the specified methodName on the passed class. + * + * If a method with this name, taking no arguments does not exist, it will check through the declared + * methods on the class, until one is found matching the supplied name. If more than one method name matches, + * an IllegalArgumentException will be raised. * * @param clazz the class of object that will be used to find the relevant Method * @param methodName the name of the method to find @@ -91,7 +95,21 @@ public final class MethodInvocationUtils { * @return a MethodInvocation, or null if there was a problem */ public static MethodInvocation createFromClass(Class clazz, String methodName) { - return createFromClass(null, clazz, methodName, null, null); + MethodInvocation mi = createFromClass(null, clazz, methodName, null, null); + + if (mi == null) { + for (Method m : clazz.getDeclaredMethods()) { + if (m.getName().equals(methodName)) { + if (mi != null) { + throw new IllegalArgumentException("The class " + clazz + " has more than one method named" + + " '" + methodName + "'"); + } + mi = new SimpleMethodInvocation(null, m); + } + } + } + + return mi; } /** diff --git a/core/src/test/java/org/springframework/security/util/MethodInvocationUtilsTests.java b/core/src/test/java/org/springframework/security/util/MethodInvocationUtilsTests.java index 1b17a36edf..e7ca4b29f2 100644 --- a/core/src/test/java/org/springframework/security/util/MethodInvocationUtilsTests.java +++ b/core/src/test/java/org/springframework/security/util/MethodInvocationUtilsTests.java @@ -4,6 +4,7 @@ import static org.junit.Assert.*; import org.aopalliance.intercept.MethodInvocation; import org.junit.Test; +import org.springframework.security.access.annotation.BusinessServiceImpl; /** * @@ -19,13 +20,18 @@ public class MethodInvocationUtilsTests { } @Test - public void createFromClassWithNoArgInfoReturnsNullForMethodWithArgs() { - MethodInvocation mi = MethodInvocationUtils.createFromClass(String.class, "codePointAt"); - assertNull(mi); + public void createFromClassReturnsMethodIfArgInfoOmittedAndMethodNameIsUnique() { + MethodInvocation mi = MethodInvocationUtils.createFromClass(BusinessServiceImpl.class, "methodReturningAnArray"); + assertNotNull(mi); + } + + @Test(expected=IllegalArgumentException.class) + public void exceptionIsRaisedIfArgInfoOmittedAndMethodNameIsNotUnique() { + MethodInvocationUtils.createFromClass(BusinessServiceImpl.class, "methodReturningAList"); } @Test - public void createFromClassReturnsMethodIfGivArgInfoForMethodWithArgs() { + public void createFromClassReturnsMethodIfGivenArgInfoForMethodWithArgs() { MethodInvocation mi = MethodInvocationUtils.createFromClass(null, String.class, "compareTo", new Class[]{String.class}, new Object[] {""}); assertNotNull(mi);