#101 - Enable annotation based parameter name discovery on MethodParameters.
MethodParameters can now get an AnnotationAttribute configured that will be inspected for parameter name lookup. If it's not set or no annotated parameter is found we fall back to the standard behavior of MethodParameter and use a LocalVariableTableParameterNameDiscoverer.
This commit is contained in:
@@ -18,10 +18,13 @@ package org.springframework.hateoas.core;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simply helper to reference a dedicated attribute of an {@link Annotation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AnnotationAttribute {
|
||||
@@ -29,13 +32,20 @@ public class AnnotationAttribute {
|
||||
private final Class<? extends Annotation> annotationType;
|
||||
private final String attributeName;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AnnotationAttribute} to the {@code value} attribute of the given {@link Annotation} type.
|
||||
*
|
||||
* @param annotationType must not be {@literal null}.
|
||||
*/
|
||||
public AnnotationAttribute(Class<? extends Annotation> annotationType) {
|
||||
this(annotationType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param annotationType
|
||||
* @param attributeName
|
||||
* Creates a new {@link AnnotationAttribute} for the given {@link Annotation} type and annotation attribute name.
|
||||
*
|
||||
* @param annotationType must not be {@literal null}.
|
||||
* @param attributeName can be {@literal null}, defaults to {@code value}.
|
||||
*/
|
||||
public AnnotationAttribute(Class<? extends Annotation> annotationType, String attributeName) {
|
||||
|
||||
@@ -46,21 +56,49 @@ public class AnnotationAttribute {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the annotation type.
|
||||
*
|
||||
* @return the annotationType
|
||||
*/
|
||||
public Class<? extends Annotation> getAnnotationType() {
|
||||
return annotationType;
|
||||
}
|
||||
|
||||
public String getValueFrom(AnnotatedElement annotatedElement) {
|
||||
/**
|
||||
* Reads the {@link Annotation} attribute's value from the given {@link MethodParameter}.
|
||||
*
|
||||
* @param parameter must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public String getValueFrom(MethodParameter parameter) {
|
||||
|
||||
Annotation annotation = annotatedElement.getAnnotation(annotationType);
|
||||
return (String) (attributeName == null ? AnnotationUtils.getValue(annotation) : AnnotationUtils.getValue(
|
||||
annotation, attributeName));
|
||||
Assert.notNull(parameter, "MethodParameter must not be null!");
|
||||
Annotation annotation = parameter.getParameterAnnotation(annotationType);
|
||||
return annotation == null ? null : getValueFrom(annotation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the {@link Annotation} attribute's value from the given {@link AnnotatedElement}.
|
||||
*
|
||||
* @param annotatedElement must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public String getValueFrom(AnnotatedElement annotatedElement) {
|
||||
|
||||
Assert.notNull(annotatedElement, "Annotated element must not be null!");
|
||||
Annotation annotation = annotatedElement.getAnnotation(annotationType);
|
||||
return annotation == null ? null : getValueFrom(annotation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Annotation} attribute's value from the given {@link Annotation}.
|
||||
*
|
||||
* @param annotation must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public String getValueFrom(Annotation annotation) {
|
||||
|
||||
Assert.notNull(annotation, "Annotation must not be null!");
|
||||
return (String) (attributeName == null ? AnnotationUtils.getValue(annotation) : AnnotationUtils.getValue(
|
||||
annotation, attributeName));
|
||||
}
|
||||
|
||||
@@ -37,18 +37,29 @@ public class MethodParameters {
|
||||
private final List<MethodParameter> parameters;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MethodParameter} from the given {@link Method}.
|
||||
* Creates a new {@link MethodParameters} from the given {@link Method}.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
*/
|
||||
public MethodParameters(Method method) {
|
||||
this(method, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MethodParameters} for the given {@link Method} and {@link AnnotationAttribute}. If the latter
|
||||
* is given, method parameter names will be looked up from the annotation attribute if present.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param namingAnnotation can be {@literal null}.
|
||||
*/
|
||||
public MethodParameters(Method method, AnnotationAttribute namingAnnotation) {
|
||||
|
||||
Assert.notNull(method);
|
||||
this.parameters = new ArrayList<MethodParameter>();
|
||||
|
||||
for (int i = 0; i < method.getParameterTypes().length; i++) {
|
||||
|
||||
MethodParameter parameter = new MethodParameter(method, i);
|
||||
MethodParameter parameter = new AnnotationNamingMethodParameter(method, i, namingAnnotation);
|
||||
parameter.initParameterNameDiscovery(DISCOVERER);
|
||||
parameters.add(parameter);
|
||||
}
|
||||
@@ -63,6 +74,25 @@ public class MethodParameters {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link MethodParameter} with the given name or {@literal null} if none found.
|
||||
*
|
||||
* @param name must not be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public MethodParameter getParameter(String name) {
|
||||
|
||||
Assert.hasText(name, "Parameter name must not be null!");
|
||||
|
||||
for (MethodParameter parameter : parameters) {
|
||||
if (name.equals(parameter.getParameterName())) {
|
||||
return parameter;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all {@link MethodParameter}s annotated with the given annotation type.
|
||||
*
|
||||
@@ -82,4 +112,54 @@ public class MethodParameters {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom {@link MethodParameter} extension that will favor the name configured in the {@link AnnotationAttribute} if
|
||||
* set over discovering it.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
private static class AnnotationNamingMethodParameter extends MethodParameter {
|
||||
|
||||
private final AnnotationAttribute attribute;
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AnnotationNamingMethodParameter} for the given {@link Method}'s parameter with the given
|
||||
* index.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @param parameterIndex
|
||||
* @param attribute can be {@literal null}
|
||||
*/
|
||||
public AnnotationNamingMethodParameter(Method method, int parameterIndex, AnnotationAttribute attribute) {
|
||||
|
||||
super(method, parameterIndex);
|
||||
this.attribute = attribute;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.MethodParameter#getParameterName()
|
||||
*/
|
||||
@Override
|
||||
public String getParameterName() {
|
||||
|
||||
if (name != null) {
|
||||
return name;
|
||||
}
|
||||
|
||||
if (attribute != null) {
|
||||
String foundName = attribute.getValueFrom(this);
|
||||
if (foundName != null) {
|
||||
name = foundName;
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
name = super.getParameterName();
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user