mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-03-09 06:50:05 +00:00
SEC-2956: Improve AnnotationParameterNameDiscoverer Performance
This commit is contained in:
parent
cbed1d75ee
commit
c79bceab03
@ -39,7 +39,7 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* import org.springframework.security.access.method.P;
|
* import org.springframework.security.access.method.P;
|
||||||
*
|
*
|
||||||
* {@code @PostAuthorize("#to == returnObject.to")}
|
* {@code @PostAuthorize("#to == returnObject.to")}
|
||||||
* public Message findMessageByTo(@P("to") String to);
|
* public Message findMessageByTo(@P("to") String to);
|
||||||
* </pre>
|
* </pre>
|
||||||
@ -69,7 +69,7 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* import org.springframework.security.access.method.P;
|
* import org.springframework.security.access.method.P;
|
||||||
*
|
*
|
||||||
* {@code @PostAuthorize("#to == returnObject.to")}
|
* {@code @PostAuthorize("#to == returnObject.to")}
|
||||||
* public Message findMessageByToAndFrom(@P("to") User to, User from);
|
* public Message findMessageByToAndFrom(@P("to") User to, User from);
|
||||||
* </pre>
|
* </pre>
|
||||||
@ -102,7 +102,7 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
|
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
|
||||||
* .lang.reflect.Method)
|
* .lang.reflect.Method)
|
||||||
*/
|
*/
|
||||||
@ -127,7 +127,7 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
*
|
*
|
||||||
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
|
* @see org.springframework.core.ParameterNameDiscoverer#getParameterNames(java
|
||||||
* .lang.reflect.Constructor)
|
* .lang.reflect.Constructor)
|
||||||
*/
|
*/
|
||||||
@ -145,11 +145,12 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
*/
|
*/
|
||||||
private <T extends AccessibleObject> String[] lookupParameterNames(
|
private <T extends AccessibleObject> String[] lookupParameterNames(
|
||||||
ParameterNameFactory<T> parameterNameFactory, T t) {
|
ParameterNameFactory<T> parameterNameFactory, T t) {
|
||||||
int parameterCount = parameterNameFactory.getParamCount(t);
|
Annotation[][] parameterAnnotations = parameterNameFactory.findParameterAnnotations(t);
|
||||||
|
int parameterCount = parameterAnnotations.length;
|
||||||
String[] paramNames = new String[parameterCount];
|
String[] paramNames = new String[parameterCount];
|
||||||
boolean found = false;
|
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 = parameterAnnotations[i];
|
||||||
String parameterName = findParameterName(annotations);
|
String parameterName = findParameterName(annotations);
|
||||||
if (parameterName != null) {
|
if (parameterName != null) {
|
||||||
found = true;
|
found = true;
|
||||||
@ -178,22 +179,16 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final ParameterNameFactory<Constructor<?>> CONSTRUCTOR_METHODPARAM_FACTORY = new ParameterNameFactory<Constructor<?>>() {
|
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) {
|
public Annotation[][] findParameterAnnotations(Constructor<?> constructor) {
|
||||||
return constructor.getParameterAnnotations()[index];
|
return constructor.getParameterAnnotations();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final ParameterNameFactory<Method> METHOD_METHODPARAM_FACTORY = new ParameterNameFactory<Method>() {
|
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) {
|
public Annotation[][] findParameterAnnotations(Method method) {
|
||||||
return method.getParameterAnnotations()[index];
|
return method.getParameterAnnotations();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -206,12 +201,6 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
* @param <T> the type to inspect (i.e. {@link Method} or {@link Constructor})
|
* @param <T> the type to inspect (i.e. {@link Method} or {@link Constructor})
|
||||||
*/
|
*/
|
||||||
private interface ParameterNameFactory<T extends AccessibleObject> {
|
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
|
* Gets the {@link Annotation}s at a specified index
|
||||||
@ -219,6 +208,6 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameDiscovere
|
|||||||
* @param index
|
* @param index
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Annotation[] findAnnotationsAt(T t, int index);
|
Annotation[][] findParameterAnnotations(T t);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user