Skip to content

Commit 32c17bf

Browse files
committed
Revise method and parameter names in annotation support
In AnnotatedElementUtils, all methods pertaining to merging annotation attributes have been renamed to "getMerged*()" and "findMerged*()" accordingly. Existing methods such as getAnnotationAttributes(..) have been deprecated in favor of the more descriptive "merged" variants. This aligns the naming conventions in AnnotatedElementUtils with those already present in AnnotationReadingVisitorUtils. The use of "annotationType" as a variable name for the fully qualified class name of an annotation type has been replaced with "annotationName" in order to improve the readability and intent of the code base. In MetaAnnotationUtils.AnnotationDescriptor, getMergedAnnotation() has been renamed to synthesizeAnnotation(), and the method is now overridden in UntypedAnnotationDescriptor to always throw an UnsupportedOperationException in order to avoid potential run-time ClassCastExceptions. Issue: SPR-11511
1 parent 2b339db commit 32c17bf

26 files changed

Lines changed: 390 additions & 342 deletions

File tree

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
442442

443443
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {
444444
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
445-
AnnotationAttributes attributes = AnnotatedElementUtils.getAnnotationAttributes(ao, type);
445+
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
446446
if (attributes != null) {
447447
return attributes;
448448
}

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ protected Object getTargetBean() {
248248
*/
249249
protected String getCondition() {
250250
if (this.condition == null) {
251-
EventListener eventListener = AnnotatedElementUtils.findAnnotation(this.method, EventListener.class);
251+
EventListener eventListener = AnnotatedElementUtils.findMergedAnnotation(this.method, EventListener.class);
252252
if (eventListener != null) {
253253
this.condition = eventListener.condition();
254254
}

spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

Lines changed: 154 additions & 136 deletions
Large diffs are not rendered by default.

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,10 @@ public static Object getDefaultValue(Class<? extends Annotation> annotationType,
10241024
}
10251025

10261026
/**
1027-
* <em>Synthesize</em> the supplied {@code annotation} by wrapping it in
1028-
* a dynamic proxy that transparently enforces <em>attribute alias</em>
1029-
* semantics for annotation attributes that are annotated with
1030-
* {@link AliasFor @AliasFor}.
1027+
* <em>Synthesize</em> an annotation from the supplied {@code annotation}
1028+
* by wrapping it in a dynamic proxy that transparently enforces
1029+
* <em>attribute alias</em> semantics for annotation attributes that are
1030+
* annotated with {@link AliasFor @AliasFor}.
10311031
*
10321032
* @param annotation the annotation to synthesize
10331033
* @return the synthesized annotation, if the supplied annotation is
@@ -1043,10 +1043,10 @@ static <A extends Annotation> A synthesizeAnnotation(A annotation) {
10431043
}
10441044

10451045
/**
1046-
* <em>Synthesize</em> the supplied {@code annotation} by wrapping it in
1047-
* a dynamic proxy that transparently enforces <em>attribute alias</em>
1048-
* semantics for annotation attributes that are annotated with
1049-
* {@link AliasFor @AliasFor}.
1046+
* <em>Synthesize</em> an annotation from the supplied {@code annotation}
1047+
* by wrapping it in a dynamic proxy that transparently enforces
1048+
* <em>attribute alias</em> semantics for annotation attributes that are
1049+
* annotated with {@link AliasFor @AliasFor}.
10501050
*
10511051
* @param annotation the annotation to synthesize
10521052
* @param annotatedElement the element that is annotated with the supplied
@@ -1083,15 +1083,15 @@ public static <A extends Annotation> A synthesizeAnnotation(A annotation, Annota
10831083
}
10841084

10851085
/**
1086-
* <em>Synthesize</em> the supplied map of annotation attributes by
1087-
* wrapping it in a dynamic proxy that implements an annotation of type
1088-
* {@code annotationType} and transparently enforces <em>attribute alias</em>
1089-
* semantics for annotation attributes that are annotated with
1090-
* {@link AliasFor @AliasFor}.
1086+
* <em>Synthesize</em> an annotation from the supplied map of annotation
1087+
* attributes by wrapping the map in a dynamic proxy that implements an
1088+
* annotation of the specified {@code annotationType} and transparently
1089+
* enforces <em>attribute alias</em> semantics for annotation attributes
1090+
* that are annotated with {@link AliasFor @AliasFor}.
10911091
* <p>The supplied map must contain key-value pairs for every attribute
10921092
* defined by the supplied {@code annotationType}.
10931093
* <p>Note that {@link AnnotationAttributes} is a specialized type of
1094-
* {@link Map} that is a suitable candidate for this method's
1094+
* {@link Map} that is an ideal candidate for this method's
10951095
* {@code attributes} argument.
10961096
*
10971097
* @param attributes the map of annotation attributes to synthesize

spring-core/src/main/java/org/springframework/core/type/AnnotatedTypeMetadata.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,59 +42,64 @@ public interface AnnotatedTypeMetadata {
4242
* of the given type defined.
4343
* <p>If this method returns {@code true}, then
4444
* {@link #getAnnotationAttributes} will return a non-null Map.
45-
* @param annotationType the annotation type to look for
45+
* @param annotationName the fully qualified class name of the annotation
46+
* type to look for
4647
* @return whether a matching annotation is defined
4748
*/
48-
boolean isAnnotated(String annotationType);
49+
boolean isAnnotated(String annotationName);
4950

5051
/**
5152
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
5253
* defined on the underlying element, as direct annotation or meta-annotation),
5354
* also taking attribute overrides on composed annotations into account.
54-
* @param annotationType the annotation type to look for
55+
* @param annotationName the fully qualified class name of the annotation
56+
* type to look for
5557
* @return a Map of attributes, with the attribute name as key (e.g. "value")
5658
* and the defined attribute value as Map value. This return value will be
5759
* {@code null} if no matching annotation is defined.
5860
*/
59-
Map<String, Object> getAnnotationAttributes(String annotationType);
61+
Map<String, Object> getAnnotationAttributes(String annotationName);
6062

6163
/**
6264
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
6365
* defined on the underlying element, as direct annotation or meta-annotation),
6466
* also taking attribute overrides on composed annotations into account.
65-
* @param annotationType the annotation type to look for
67+
* @param annotationName the fully qualified class name of the annotation
68+
* type to look for
6669
* @param classValuesAsString whether to convert class references to String
6770
* class names for exposure as values in the returned Map, instead of Class
6871
* references which might potentially have to be loaded first
6972
* @return a Map of attributes, with the attribute name as key (e.g. "value")
7073
* and the defined attribute value as Map value. This return value will be
7174
* {@code null} if no matching annotation is defined.
7275
*/
73-
Map<String, Object> getAnnotationAttributes(String annotationType, boolean classValuesAsString);
76+
Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString);
7477

7578
/**
7679
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
7780
* defined on the underlying element, as direct annotation or meta-annotation).
7881
* Note that this variant does <i>not</i> take attribute overrides into account.
79-
* @param annotationType the annotation type to look for
82+
* @param annotationName the fully qualified class name of the annotation
83+
* type to look for
8084
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")
8185
* and a list of the defined attribute values as Map value. This return value will
8286
* be {@code null} if no matching annotation is defined.
8387
* @see #getAllAnnotationAttributes(String, boolean)
8488
*/
85-
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType);
89+
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName);
8690

8791
/**
8892
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
8993
* defined on the underlying element, as direct annotation or meta-annotation).
9094
* Note that this variant does <i>not</i> take attribute overrides into account.
91-
* @param annotationType the annotation type to look for
95+
* @param annotationName the fully qualified class name of the annotation
96+
* type to look for
9297
* @param classValuesAsString whether to convert class references to String
9398
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")
9499
* and a list of the defined attribute values as Map value. This return value will
95100
* be {@code null} if no matching annotation is defined.
96101
* @see #getAllAnnotationAttributes(String)
97102
*/
98-
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString);
103+
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString);
99104

100105
}

spring-core/src/main/java/org/springframework/core/type/AnnotationMetadata.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,52 +34,58 @@
3434
public interface AnnotationMetadata extends ClassMetadata, AnnotatedTypeMetadata {
3535

3636
/**
37-
* Return the names of all annotation types that are <em>present</em> on the
38-
* underlying class.
37+
* Get the fully qualified class names of all annotation types that
38+
* are <em>present</em> on the underlying class.
3939
* @return the annotation type names
4040
*/
4141
Set<String> getAnnotationTypes();
4242

4343
/**
44-
* Return the names of all meta-annotation types <em>present</em> on the
45-
* given annotation type on the underlying class.
46-
* @param annotationType the meta-annotation type to look for
44+
* Get the fully qualified class names of all meta-annotation types that
45+
* are <em>present</em> on the given annotation type on the underlying class.
46+
* @param annotationName the fully qualified class name of the meta-annotation
47+
* type to look for
4748
* @return the meta-annotation type names
4849
*/
49-
Set<String> getMetaAnnotationTypes(String annotationType);
50+
Set<String> getMetaAnnotationTypes(String annotationName);
5051

5152
/**
5253
* Determine whether an annotation of the given type is <em>present</em> on
5354
* the underlying class.
54-
* @param annotationType the annotation type to look for
55-
* @return whether a matching annotation is present
55+
* @param annotationName the fully qualified class name of the annotation
56+
* type to look for
57+
* @return {@code true} if a matching annotation is present
5658
*/
57-
boolean hasAnnotation(String annotationType);
59+
boolean hasAnnotation(String annotationName);
5860

5961
/**
6062
* Determine whether the underlying class has an annotation that is itself
6163
* annotated with the meta-annotation of the given type.
62-
* @param metaAnnotationType the meta-annotation type to look for
63-
* @return whether a matching meta-annotation is present
64+
* @param metaAnnotationName the fully qualified class name of the
65+
* meta-annotation type to look for
66+
* @return {@code true} if a matching meta-annotation is present
6467
*/
65-
boolean hasMetaAnnotation(String metaAnnotationType);
68+
boolean hasMetaAnnotation(String metaAnnotationName);
6669

6770
/**
6871
* Determine whether the underlying class has any methods that are
6972
* annotated (or meta-annotated) with the given annotation type.
73+
* @param annotationName the fully qualified class name of the annotation
74+
* type to look for
7075
*/
71-
boolean hasAnnotatedMethods(String annotationType);
76+
boolean hasAnnotatedMethods(String annotationName);
7277

7378
/**
7479
* Retrieve the method metadata for all methods that are annotated
7580
* (or meta-annotated) with the given annotation type.
7681
* <p>For any returned method, {@link MethodMetadata#isAnnotated} will
7782
* return {@code true} for the given annotation type.
78-
* @param annotationType the annotation type to look for
83+
* @param annotationName the fully qualified class name of the annotation
84+
* type to look for
7985
* @return a set of {@link MethodMetadata} for methods that have a matching
8086
* annotation. The return value will be an empty set if no methods match
8187
* the annotation type.
8288
*/
83-
Set<MethodMetadata> getAnnotatedMethods(String annotationType);
89+
Set<MethodMetadata> getAnnotatedMethods(String annotationName);
8490

8591
}

spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
* @author Mark Fisher
3434
* @author Chris Beams
3535
* @author Phillip Webb
36+
* @author Sam Brannen
3637
* @since 2.5
3738
*/
3839
public class StandardAnnotationMetadata extends StandardClassMetadata implements AnnotationMetadata {
@@ -77,70 +78,70 @@ public Set<String> getAnnotationTypes() {
7778
}
7879

7980
@Override
80-
public Set<String> getMetaAnnotationTypes(String annotationType) {
81-
return AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationType);
81+
public Set<String> getMetaAnnotationTypes(String annotationName) {
82+
return AnnotatedElementUtils.getMetaAnnotationTypes(getIntrospectedClass(), annotationName);
8283
}
8384

8485
@Override
85-
public boolean hasAnnotation(String annotationType) {
86+
public boolean hasAnnotation(String annotationName) {
8687
Annotation[] anns = getIntrospectedClass().getAnnotations();
8788
for (Annotation ann : anns) {
88-
if (ann.annotationType().getName().equals(annotationType)) {
89+
if (ann.annotationType().getName().equals(annotationName)) {
8990
return true;
9091
}
9192
}
9293
return false;
9394
}
9495

9596
@Override
96-
public boolean hasMetaAnnotation(String annotationType) {
97-
return AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationType);
97+
public boolean hasMetaAnnotation(String annotationName) {
98+
return AnnotatedElementUtils.hasMetaAnnotationTypes(getIntrospectedClass(), annotationName);
9899
}
99100

100101
@Override
101-
public boolean isAnnotated(String annotationType) {
102-
return AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationType);
102+
public boolean isAnnotated(String annotationName) {
103+
return AnnotatedElementUtils.isAnnotated(getIntrospectedClass(), annotationName);
103104
}
104105

105106
@Override
106-
public Map<String, Object> getAnnotationAttributes(String annotationType) {
107-
return this.getAnnotationAttributes(annotationType, false);
107+
public Map<String, Object> getAnnotationAttributes(String annotationName) {
108+
return this.getAnnotationAttributes(annotationName, false);
108109
}
109110

110111
@Override
111-
public Map<String, Object> getAnnotationAttributes(String annotationType, boolean classValuesAsString) {
112-
return AnnotatedElementUtils.getAnnotationAttributes(getIntrospectedClass(),
113-
annotationType, classValuesAsString, this.nestedAnnotationsAsMap);
112+
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
113+
return AnnotatedElementUtils.getMergedAnnotationAttributes(getIntrospectedClass(),
114+
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
114115
}
115116

116117
@Override
117-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType) {
118-
return getAllAnnotationAttributes(annotationType, false);
118+
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
119+
return getAllAnnotationAttributes(annotationName, false);
119120
}
120121

121122
@Override
122-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString) {
123+
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
123124
return AnnotatedElementUtils.getAllAnnotationAttributes(getIntrospectedClass(),
124-
annotationType, classValuesAsString, this.nestedAnnotationsAsMap);
125+
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
125126
}
126127

127128
@Override
128-
public boolean hasAnnotatedMethods(String annotationType) {
129+
public boolean hasAnnotatedMethods(String annotationName) {
129130
Method[] methods = getIntrospectedClass().getDeclaredMethods();
130131
for (Method method : methods) {
131-
if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) {
132+
if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationName)) {
132133
return true;
133134
}
134135
}
135136
return false;
136137
}
137138

138139
@Override
139-
public Set<MethodMetadata> getAnnotatedMethods(String annotationType) {
140+
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
140141
Method[] methods = getIntrospectedClass().getDeclaredMethods();
141142
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
142143
for (Method method : methods) {
143-
if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationType)) {
144+
if (!method.isBridge() && AnnotatedElementUtils.isAnnotated(method, annotationName)) {
144145
annotatedMethods.add(new StandardMethodMetadata(method, this.nestedAnnotationsAsMap));
145146
}
146147
}

spring-core/src/main/java/org/springframework/core/type/StandardMethodMetadata.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public final Method getIntrospectedMethod() {
7373
return this.introspectedMethod;
7474
}
7575

76-
7776
@Override
7877
public String getMethodName() {
7978
return this.introspectedMethod.getName();
@@ -110,30 +109,30 @@ public boolean isOverridable() {
110109
}
111110

112111
@Override
113-
public boolean isAnnotated(String annotationType) {
114-
return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationType);
112+
public boolean isAnnotated(String annotationName) {
113+
return AnnotatedElementUtils.isAnnotated(this.introspectedMethod, annotationName);
115114
}
116115

117116
@Override
118-
public Map<String, Object> getAnnotationAttributes(String annotationType) {
119-
return getAnnotationAttributes(annotationType, false);
117+
public Map<String, Object> getAnnotationAttributes(String annotationName) {
118+
return getAnnotationAttributes(annotationName, false);
120119
}
121120

122121
@Override
123-
public Map<String, Object> getAnnotationAttributes(String annotationType, boolean classValuesAsString) {
124-
return AnnotatedElementUtils.getAnnotationAttributes(this.introspectedMethod,
125-
annotationType, classValuesAsString, this.nestedAnnotationsAsMap);
122+
public Map<String, Object> getAnnotationAttributes(String annotationName, boolean classValuesAsString) {
123+
return AnnotatedElementUtils.getMergedAnnotationAttributes(this.introspectedMethod,
124+
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
126125
}
127126

128127
@Override
129-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType) {
130-
return getAllAnnotationAttributes(annotationType, false);
128+
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName) {
129+
return getAllAnnotationAttributes(annotationName, false);
131130
}
132131

133132
@Override
134-
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString) {
133+
public MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationName, boolean classValuesAsString) {
135134
return AnnotatedElementUtils.getAllAnnotationAttributes(this.introspectedMethod,
136-
annotationType, classValuesAsString, this.nestedAnnotationsAsMap);
135+
annotationName, classValuesAsString, this.nestedAnnotationsAsMap);
137136
}
138137

139138
}

0 commit comments

Comments
 (0)