Skip to content

Commit

Permalink
SEC-2956: Improve AnnotationParameterNameDiscoverer Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Winch committed Aug 19, 2015
1 parent cbed1d7 commit c79bcea
Showing 1 changed file with 12 additions and 23 deletions.
Expand Up @@ -39,7 +39,7 @@
*
* <pre>
* import org.springframework.security.access.method.P;
*
*
* {@code @PostAuthorize("#to == returnObject.to")}
* public Message findMessageByTo(@P("to") String to);
* </pre>
Expand Down Expand Up @@ -69,7 +69,7 @@
*
* <pre>
* import org.springframework.security.access.method.P;
*
*
* {@code @PostAuthorize("#to == returnObject.to")}
* public Message findMessageByToAndFrom(@P("to") User to, User from);
* </pre>
Expand Down Expand Up @@ -102,7 +102,7 @@ public AnnotationParameterNameDiscoverer(Set<String> annotationClassesToUse) {

/*
* (non-Javadoc)
*
*
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
* .lang.reflect.Method)
*/
Expand All @@ -127,7 +127,7 @@ public String[] getParameterNames(Method method) {

/*
* (non-Javadoc)
*
*
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
* .lang.reflect.Constructor)
*/
Expand All @@ -145,11 +145,12 @@ public String[] getParameterNames(Constructor<?> constructor) {
*/
private <T extends AccessibleObject> String[] lookupParameterNames(
ParameterNameFactory<T> parameterNameFactory, T t) {
int parameterCount = parameterNameFactory.getParamCount(t);
Annotation[][] parameterAnnotations = parameterNameFactory.findParameterAnnotations(t);
int parameterCount = parameterAnnotations.length;
String[] paramNames = new String[parameterCount];
boolean found = false;
for (int i = 0; i < parameterCount; i++) {
Annotation[] annotations = parameterNameFactory.findAnnotationsAt(t, i);
Annotation[] annotations = parameterAnnotations[i];
String parameterName = findParameterName(annotations);
if (parameterName != null) {
found = true;
Expand Down Expand Up @@ -178,22 +179,16 @@ private String findParameterName(Annotation[] parameterAnnotations) {
}

private static final ParameterNameFactory<Constructor<?>> CONSTRUCTOR_METHODPARAM_FACTORY = new ParameterNameFactory<Constructor<?>>() {
public int getParamCount(Constructor<?> constructor) {
return constructor.getParameterTypes().length;
}

public Annotation[] findAnnotationsAt(Constructor<?> constructor, int index) {
return constructor.getParameterAnnotations()[index];
public Annotation[][] findParameterAnnotations(Constructor<?> constructor) {
return constructor.getParameterAnnotations();
}
};

private static final ParameterNameFactory<Method> METHOD_METHODPARAM_FACTORY = new ParameterNameFactory<Method>() {
public int getParamCount(Method method) {
return method.getParameterTypes().length;
}

public Annotation[] findAnnotationsAt(Method method, int index) {
return method.getParameterAnnotations()[index];
public Annotation[][] findParameterAnnotations(Method method) {
return method.getParameterAnnotations();
}
};

Expand All @@ -206,19 +201,13 @@ public Annotation[] findAnnotationsAt(Method method, int index) {
* @param <T> the type to inspect (i.e. {@link Method} or {@link Constructor})
*/
private interface ParameterNameFactory<T extends AccessibleObject> {
/**
* Gets the parameter count
* @param t
* @return
*/
int getParamCount(T t);

/**
* Gets the {@link Annotation}s at a specified index
* @param t
* @param index
* @return
*/
Annotation[] findAnnotationsAt(T t, int index);
Annotation[][] findParameterAnnotations(T t);
}
}

1 comment on commit c79bcea

@cemo
Copy link

@cemo cemo commented on c79bcea Aug 30, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwinch I believe that this class would be better to part of Spring Framework. What do you think?

I had opened an issue related to this:
https://jira.spring.io/browse/SPR-11659

Please sign in to comment.