@@ -42,15 +42,13 @@ public class AnnotatedElementUtils {
42
42
public static Set <String > getMetaAnnotationTypes (AnnotatedElement element , String annotationType ) {
43
43
final Set <String > types = new LinkedHashSet <String >();
44
44
process (element , annotationType , true , new Processor <Object >() {
45
-
46
45
@ Override
47
46
public Object process (Annotation annotation , int metaDepth ) {
48
47
if (metaDepth > 0 ) {
49
48
types .add (annotation .annotationType ().getName ());
50
49
}
51
50
return null ;
52
51
}
53
-
54
52
@ Override
55
53
public void postProcess (Annotation annotation , Object result ) {
56
54
}
@@ -60,15 +58,13 @@ public void postProcess(Annotation annotation, Object result) {
60
58
61
59
public static boolean hasMetaAnnotationTypes (AnnotatedElement element , String annotationType ) {
62
60
return Boolean .TRUE .equals (process (element , annotationType , true , new Processor <Boolean >() {
63
-
64
61
@ Override
65
62
public Boolean process (Annotation annotation , int metaDepth ) {
66
63
if (metaDepth > 0 ) {
67
64
return Boolean .TRUE ;
68
65
}
69
66
return null ;
70
67
}
71
-
72
68
@ Override
73
69
public void postProcess (Annotation annotation , Boolean result ) {
74
70
}
@@ -77,12 +73,10 @@ public void postProcess(Annotation annotation, Boolean result) {
77
73
78
74
public static boolean isAnnotated (AnnotatedElement element , String annotationType ) {
79
75
return Boolean .TRUE .equals (process (element , annotationType , true , new Processor <Boolean >() {
80
-
81
76
@ Override
82
77
public Boolean process (Annotation annotation , int metaDepth ) {
83
78
return Boolean .TRUE ;
84
79
}
85
-
86
80
@ Override
87
81
public void postProcess (Annotation annotation , Boolean result ) {
88
82
}
@@ -97,12 +91,10 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
97
91
final boolean classValuesAsString , final boolean nestedAnnotationsAsMap ) {
98
92
99
93
return process (element , annotationType , true , new Processor <AnnotationAttributes >() {
100
-
101
94
@ Override
102
95
public AnnotationAttributes process (Annotation annotation , int metaDepth ) {
103
96
return AnnotationUtils .getAnnotationAttributes (annotation , classValuesAsString , nestedAnnotationsAsMap );
104
97
}
105
-
106
98
@ Override
107
99
public void postProcess (Annotation annotation , AnnotationAttributes result ) {
108
100
for (String key : result .keySet ()) {
@@ -117,8 +109,7 @@ public void postProcess(Annotation annotation, AnnotationAttributes result) {
117
109
});
118
110
}
119
111
120
- public static MultiValueMap <String , Object > getAllAnnotationAttributes (AnnotatedElement element ,
121
- final String annotationType ) {
112
+ public static MultiValueMap <String , Object > getAllAnnotationAttributes (AnnotatedElement element , String annotationType ) {
122
113
return getAllAnnotationAttributes (element , annotationType , false , false );
123
114
}
124
115
@@ -127,7 +118,6 @@ public static MultiValueMap<String, Object> getAllAnnotationAttributes(Annotated
127
118
128
119
final MultiValueMap <String , Object > attributes = new LinkedMultiValueMap <String , Object >();
129
120
process (element , annotationType , false , new Processor <Void >() {
130
-
131
121
@ Override
132
122
public Void process (Annotation annotation , int metaDepth ) {
133
123
if (annotation .annotationType ().getName ().equals (annotationType )) {
@@ -138,7 +128,6 @@ public Void process(Annotation annotation, int metaDepth) {
138
128
}
139
129
return null ;
140
130
}
141
-
142
131
@ Override
143
132
public void postProcess (Annotation annotation , Void result ) {
144
133
for (String key : attributes .keySet ()) {
@@ -157,12 +146,10 @@ public void postProcess(Annotation annotation, Void result) {
157
146
/**
158
147
* Process all annotations of the specified {@code annotationType} and
159
148
* recursively all meta-annotations on the specified {@code element}.
160
- *
161
149
* <p>If the {@code traverseClassHierarchy} flag is {@code true} and the sought
162
150
* annotation is neither <em>directly present</em> on the given element nor
163
151
* present on the given element as a meta-annotation, then the algorithm will
164
152
* recursively search through the class hierarchy of the given element.
165
- *
166
153
* @param element the annotated element
167
154
* @param annotationType the annotation type to find
168
155
* @param traverseClassHierarchy whether or not to traverse up the class
@@ -172,19 +159,23 @@ public void postProcess(Annotation annotation, Void result) {
172
159
*/
173
160
private static <T > T process (AnnotatedElement element , String annotationType , boolean traverseClassHierarchy ,
174
161
Processor <T > processor ) {
175
- return doProcess (element , annotationType , traverseClassHierarchy , processor , new HashSet <AnnotatedElement >(), 0 );
162
+
163
+ try {
164
+ return doProcess (element , annotationType , traverseClassHierarchy , processor , new HashSet <AnnotatedElement >(), 0 );
165
+ }
166
+ catch (Throwable ex ) {
167
+ throw new IllegalStateException ("Failed to introspect annotations: " + element , ex );
168
+ }
176
169
}
177
170
178
171
/**
179
172
* Perform the search algorithm for the {@link #process} method, avoiding
180
173
* endless recursion by tracking which annotated elements have already been
181
174
* <em>visited</em>.
182
- *
183
175
* <p>The {@code metaDepth} parameter represents the depth of the annotation
184
176
* relative to the initial element. For example, an annotation that is
185
177
* <em>present</em> on the element will have a depth of 0; a meta-annotation
186
178
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
187
- *
188
179
* @param element the annotated element
189
180
* @param annotationType the annotation type to find
190
181
* @param traverseClassHierarchy whether or not to traverse up the class
@@ -198,10 +189,8 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
198
189
Processor <T > processor , Set <AnnotatedElement > visited , int metaDepth ) {
199
190
200
191
if (visited .add (element )) {
201
-
202
- Annotation [] annotations = traverseClassHierarchy ? element .getDeclaredAnnotations ()
203
- : element .getAnnotations ();
204
-
192
+ Annotation [] annotations =
193
+ (traverseClassHierarchy ? element .getDeclaredAnnotations () : element .getAnnotations ());
205
194
for (Annotation annotation : annotations ) {
206
195
if (annotation .annotationType ().getName ().equals (annotationType ) || metaDepth > 0 ) {
207
196
T result = processor .process (annotation , metaDepth );
@@ -216,7 +205,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
216
205
}
217
206
}
218
207
}
219
-
220
208
for (Annotation annotation : annotations ) {
221
209
if (!isInJavaLangAnnotationPackage (annotation )) {
222
210
T result = doProcess (annotation .annotationType (), annotationType , traverseClassHierarchy ,
@@ -227,7 +215,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
227
215
}
228
216
}
229
217
}
230
-
231
218
if (traverseClassHierarchy && element instanceof Class ) {
232
219
Class <?> superclass = ((Class <?>) element ).getSuperclass ();
233
220
if (superclass != null && !superclass .equals (Object .class )) {
@@ -239,7 +226,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
239
226
}
240
227
}
241
228
}
242
-
243
229
return null ;
244
230
}
245
231
@@ -252,13 +238,11 @@ private static interface Processor<T> {
252
238
253
239
/**
254
240
* Called to process the annotation.
255
- *
256
241
* <p>The {@code metaDepth} parameter represents the depth of the
257
242
* annotation relative to the initial element. For example, an annotation
258
243
* that is <em>present</em> on the element will have a depth of 0; a
259
244
* meta-annotation will have a depth of 1; and a meta-meta-annotation
260
245
* will have a depth of 2.
261
- *
262
246
* @param annotation the annotation to process
263
247
* @param metaDepth the depth of the annotation relative to the initial element
264
248
* @return the result of the processing or {@code null} to continue
0 commit comments