Polish SynthesizedAnnotation support classes

This commit is contained in:
Sam Brannen 2015-08-10 15:39:09 +02:00
parent f17173f6d5
commit 5deeaf348a
6 changed files with 16 additions and 24 deletions

View File

@ -31,17 +31,18 @@ import org.springframework.util.ObjectUtils;
*
* @author Sam Brannen
* @since 4.2
* @param <S> the type of source supported by this extractor
* @see Annotation
* @see AliasFor
* @see AnnotationUtils#synthesizeAnnotation(Annotation, AnnotatedElement)
*/
abstract class AbstractAliasAwareAnnotationAttributeExtractor implements AnnotationAttributeExtractor {
abstract class AbstractAliasAwareAnnotationAttributeExtractor<S> implements AnnotationAttributeExtractor<S> {
private final Class<? extends Annotation> annotationType;
private final AnnotatedElement annotatedElement;
private final Object source;
private final S source;
private final Map<String, String> attributeAliasMap;
@ -54,7 +55,7 @@ abstract class AbstractAliasAwareAnnotationAttributeExtractor implements Annotat
* @param source the underlying source of annotation attributes; never {@code null}
*/
AbstractAliasAwareAnnotationAttributeExtractor(Class<? extends Annotation> annotationType,
AnnotatedElement annotatedElement, Object source) {
AnnotatedElement annotatedElement, S source) {
Assert.notNull(annotationType, "annotationType must not be null");
Assert.notNull(source, "source must not be null");
@ -76,7 +77,7 @@ abstract class AbstractAliasAwareAnnotationAttributeExtractor implements Annotat
}
@Override
public Object getSource() {
public final S getSource() {
return this.source;
}

View File

@ -28,9 +28,10 @@ import java.lang.reflect.Method;
*
* @author Sam Brannen
* @since 4.2
* @param <S> the type of source supported by this extractor
* @see SynthesizedAnnotationInvocationHandler
*/
interface AnnotationAttributeExtractor {
interface AnnotationAttributeExtractor<S> {
/**
* Get the type of annotation that this extractor extracts attribute
@ -48,7 +49,7 @@ interface AnnotationAttributeExtractor {
/**
* Get the underlying source of annotation attributes.
*/
Object getSource();
S getSource();
/**
* Get the attribute value from the underlying {@linkplain #getSource source}

View File

@ -1225,7 +1225,7 @@ public abstract class AnnotationUtils {
return annotation;
}
AnnotationAttributeExtractor attributeExtractor =
DefaultAnnotationAttributeExtractor attributeExtractor =
new DefaultAnnotationAttributeExtractor(annotation, annotatedElement);
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
A synthesizedAnnotation = (A) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),
@ -1273,7 +1273,7 @@ public abstract class AnnotationUtils {
return null;
}
AnnotationAttributeExtractor attributeExtractor =
MapAnnotationAttributeExtractor attributeExtractor =
new MapAnnotationAttributeExtractor(attributes, annotationType, annotatedElement);
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
A synthesizedAnnotation = (A) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),

View File

@ -34,7 +34,7 @@ import org.springframework.util.ReflectionUtils;
* @see MapAnnotationAttributeExtractor
* @see AnnotationUtils#synthesizeAnnotation(Annotation, AnnotatedElement)
*/
class DefaultAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttributeExtractor {
class DefaultAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttributeExtractor<Annotation> {
/**
* Construct a new {@code DefaultAnnotationAttributeExtractor}.
@ -47,11 +47,6 @@ class DefaultAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAt
}
@Override
public Annotation getSource() {
return (Annotation) super.getSource();
}
@Override
protected Object getRawAttributeValue(Method attributeMethod) {
ReflectionUtils.makeAccessible(attributeMethod);
@ -60,7 +55,7 @@ class DefaultAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAt
@Override
protected Object getRawAttributeValue(String attributeName) {
Method attributeMethod = ReflectionUtils.findMethod(getSource().annotationType(), attributeName);
Method attributeMethod = ReflectionUtils.findMethod(getAnnotationType(), attributeName);
return getRawAttributeValue(attributeMethod);
}

View File

@ -38,7 +38,7 @@ import static org.springframework.core.annotation.AnnotationUtils.*;
* @see DefaultAnnotationAttributeExtractor
* @see AnnotationUtils#synthesizeAnnotation(Map, Class, AnnotatedElement)
*/
class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttributeExtractor {
class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttributeExtractor<Map<String, Object>> {
/**
* Construct a new {@code MapAnnotationAttributeExtractor}.
@ -57,14 +57,9 @@ class MapAnnotationAttributeExtractor extends AbstractAliasAwareAnnotationAttrib
}
@SuppressWarnings("unchecked")
public Map<String, Object> getSource() {
return (Map<String, Object>) super.getSource();
}
@Override
protected Object getRawAttributeValue(Method attributeMethod) {
return getSource().get(attributeMethod.getName());
return getRawAttributeValue(attributeMethod.getName());
}
@Override

View File

@ -45,7 +45,7 @@ import static org.springframework.util.ReflectionUtils.*;
*/
class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
private final AnnotationAttributeExtractor attributeExtractor;
private final AnnotationAttributeExtractor<?> attributeExtractor;
private final Map<String, Object> valueCache = new ConcurrentHashMap<String, Object>(8);
@ -55,7 +55,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
* the supplied {@link AnnotationAttributeExtractor}.
* @param attributeExtractor the extractor to delegate to
*/
SynthesizedAnnotationInvocationHandler(AnnotationAttributeExtractor attributeExtractor) {
SynthesizedAnnotationInvocationHandler(AnnotationAttributeExtractor<?> attributeExtractor) {
Assert.notNull(attributeExtractor, "AnnotationAttributeExtractor must not be null");
this.attributeExtractor = attributeExtractor;
}