SEC-2151: AnnotationParameterNameDiscoverer support single annotated param

This makes sense since often times only a single argument is necessary in
the expression.
This commit is contained in:
Rob Winch 2013-09-27 11:53:39 -05:00
parent e5f034bdef
commit 61e6acb3f4
2 changed files with 13 additions and 12 deletions

View File

@ -63,8 +63,9 @@ import org.springframework.util.ReflectionUtils;
* </p> * </p>
* *
* <p> * <p>
* It is important that all the parameter names have a supported annotation on * It is important to note that if a single parameter name has a supported
* them. Otherwise, the result will be null. For example, consider the * annotation on it then all methods are resolved using
* {@link AnnotationParameterNameDiscoverer}. For example, consider the
* following: * following:
* </p> * </p>
* *
@ -76,9 +77,9 @@ import org.springframework.util.ReflectionUtils;
* </pre> * </pre>
* *
* <p> * <p>
* The result of finding parameters on the previous sample will be a null * The result of finding parameters on the previous sample will be
* String[] since only a single parameter contains an annotation. This is mostly * <code>new String[] { "to", null}</code> since only a single parameter
* due to the fact that the fallbacks for * contains an annotation. This is mostly due to the fact that the fallbacks for
* {@link PrioritizedParameterNameDiscoverer} are an all or nothing operation. * {@link PrioritizedParameterNameDiscoverer} are an all or nothing operation.
* </p> * </p>
* *
@ -149,16 +150,16 @@ public class AnnotationParameterNameDiscoverer implements
ParameterNameFactory<T> parameterNameFactory, T t) { ParameterNameFactory<T> parameterNameFactory, T t) {
int parameterCount = parameterNameFactory.getParamCount(t); int parameterCount = parameterNameFactory.getParamCount(t);
String[] paramNames = new String[parameterCount]; String[] paramNames = new String[parameterCount];
boolean found = false;
for (int i = 0; i < parameterCount; i++) { for (int i = 0; i < parameterCount; i++) {
Annotation[] annotations = parameterNameFactory.findAnnotationsAt(t, i); Annotation[] annotations = parameterNameFactory.findAnnotationsAt(t, i);
String parameterName = findParameterName(annotations); String parameterName = findParameterName(annotations);
if (parameterName == null) { if (parameterName != null) {
return null; found = true;
} else {
paramNames[i] = parameterName; paramNames[i] = parameterName;
} }
} }
return paramNames; return found ? paramNames : null;
} }
/** /**

View File

@ -22,7 +22,7 @@ public class AnnotationParameterNameDiscovererTests {
@Test @Test
public void getParameterNamesInterfaceSingleParamAnnotatedWithMultiParams() { public void getParameterNamesInterfaceSingleParamAnnotatedWithMultiParams() {
assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null});
} }
@Test @Test
@ -37,7 +37,7 @@ public class AnnotationParameterNameDiscovererTests {
@Test @Test
public void getParameterNamesClassSingleParamAnnotatedWithMultiParams() { public void getParameterNamesClassSingleParamAnnotatedWithMultiParams() {
assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null});
} }
@Test @Test
@ -64,7 +64,7 @@ public class AnnotationParameterNameDiscovererTests {
@Test @Test
public void getParameterNamesClassAnnotationOnImpl() throws Exception { public void getParameterNamesClassAnnotationOnImpl() throws Exception {
assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null});
assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(DaoImpl.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String[] {"to", "from"}); assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(DaoImpl.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String[] {"to", "from"});
} }