Skip to content

Commit ac61b13

Browse files
committed
AnnotatedElementUtils wraps unexpected exceptions with descriptive IllegalStateException
Issue: SPR-10441
1 parent 1763bfb commit ac61b13

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ public class AnnotatedElementUtils {
4242
public static Set<String> getMetaAnnotationTypes(AnnotatedElement element, String annotationType) {
4343
final Set<String> types = new LinkedHashSet<String>();
4444
process(element, annotationType, true, new Processor<Object>() {
45-
4645
@Override
4746
public Object process(Annotation annotation, int metaDepth) {
4847
if (metaDepth > 0) {
4948
types.add(annotation.annotationType().getName());
5049
}
5150
return null;
5251
}
53-
5452
@Override
5553
public void postProcess(Annotation annotation, Object result) {
5654
}
@@ -60,15 +58,13 @@ public void postProcess(Annotation annotation, Object result) {
6058

6159
public static boolean hasMetaAnnotationTypes(AnnotatedElement element, String annotationType) {
6260
return Boolean.TRUE.equals(process(element, annotationType, true, new Processor<Boolean>() {
63-
6461
@Override
6562
public Boolean process(Annotation annotation, int metaDepth) {
6663
if (metaDepth > 0) {
6764
return Boolean.TRUE;
6865
}
6966
return null;
7067
}
71-
7268
@Override
7369
public void postProcess(Annotation annotation, Boolean result) {
7470
}
@@ -77,12 +73,10 @@ public void postProcess(Annotation annotation, Boolean result) {
7773

7874
public static boolean isAnnotated(AnnotatedElement element, String annotationType) {
7975
return Boolean.TRUE.equals(process(element, annotationType, true, new Processor<Boolean>() {
80-
8176
@Override
8277
public Boolean process(Annotation annotation, int metaDepth) {
8378
return Boolean.TRUE;
8479
}
85-
8680
@Override
8781
public void postProcess(Annotation annotation, Boolean result) {
8882
}
@@ -97,12 +91,10 @@ public static AnnotationAttributes getAnnotationAttributes(AnnotatedElement elem
9791
final boolean classValuesAsString, final boolean nestedAnnotationsAsMap) {
9892

9993
return process(element, annotationType, true, new Processor<AnnotationAttributes>() {
100-
10194
@Override
10295
public AnnotationAttributes process(Annotation annotation, int metaDepth) {
10396
return AnnotationUtils.getAnnotationAttributes(annotation, classValuesAsString, nestedAnnotationsAsMap);
10497
}
105-
10698
@Override
10799
public void postProcess(Annotation annotation, AnnotationAttributes result) {
108100
for (String key : result.keySet()) {
@@ -117,8 +109,7 @@ public void postProcess(Annotation annotation, AnnotationAttributes result) {
117109
});
118110
}
119111

120-
public static MultiValueMap<String, Object> getAllAnnotationAttributes(AnnotatedElement element,
121-
final String annotationType) {
112+
public static MultiValueMap<String, Object> getAllAnnotationAttributes(AnnotatedElement element, String annotationType) {
122113
return getAllAnnotationAttributes(element, annotationType, false, false);
123114
}
124115

@@ -127,7 +118,6 @@ public static MultiValueMap<String, Object> getAllAnnotationAttributes(Annotated
127118

128119
final MultiValueMap<String, Object> attributes = new LinkedMultiValueMap<String, Object>();
129120
process(element, annotationType, false, new Processor<Void>() {
130-
131121
@Override
132122
public Void process(Annotation annotation, int metaDepth) {
133123
if (annotation.annotationType().getName().equals(annotationType)) {
@@ -138,7 +128,6 @@ public Void process(Annotation annotation, int metaDepth) {
138128
}
139129
return null;
140130
}
141-
142131
@Override
143132
public void postProcess(Annotation annotation, Void result) {
144133
for (String key : attributes.keySet()) {
@@ -157,12 +146,10 @@ public void postProcess(Annotation annotation, Void result) {
157146
/**
158147
* Process all annotations of the specified {@code annotationType} and
159148
* recursively all meta-annotations on the specified {@code element}.
160-
*
161149
* <p>If the {@code traverseClassHierarchy} flag is {@code true} and the sought
162150
* annotation is neither <em>directly present</em> on the given element nor
163151
* present on the given element as a meta-annotation, then the algorithm will
164152
* recursively search through the class hierarchy of the given element.
165-
*
166153
* @param element the annotated element
167154
* @param annotationType the annotation type to find
168155
* @param traverseClassHierarchy whether or not to traverse up the class
@@ -172,19 +159,23 @@ public void postProcess(Annotation annotation, Void result) {
172159
*/
173160
private static <T> T process(AnnotatedElement element, String annotationType, boolean traverseClassHierarchy,
174161
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+
}
176169
}
177170

178171
/**
179172
* Perform the search algorithm for the {@link #process} method, avoiding
180173
* endless recursion by tracking which annotated elements have already been
181174
* <em>visited</em>.
182-
*
183175
* <p>The {@code metaDepth} parameter represents the depth of the annotation
184176
* relative to the initial element. For example, an annotation that is
185177
* <em>present</em> on the element will have a depth of 0; a meta-annotation
186178
* will have a depth of 1; and a meta-meta-annotation will have a depth of 2.
187-
*
188179
* @param element the annotated element
189180
* @param annotationType the annotation type to find
190181
* @param traverseClassHierarchy whether or not to traverse up the class
@@ -198,10 +189,8 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
198189
Processor<T> processor, Set<AnnotatedElement> visited, int metaDepth) {
199190

200191
if (visited.add(element)) {
201-
202-
Annotation[] annotations = traverseClassHierarchy ? element.getDeclaredAnnotations()
203-
: element.getAnnotations();
204-
192+
Annotation[] annotations =
193+
(traverseClassHierarchy ? element.getDeclaredAnnotations() : element.getAnnotations());
205194
for (Annotation annotation : annotations) {
206195
if (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0) {
207196
T result = processor.process(annotation, metaDepth);
@@ -216,7 +205,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
216205
}
217206
}
218207
}
219-
220208
for (Annotation annotation : annotations) {
221209
if (!isInJavaLangAnnotationPackage(annotation)) {
222210
T result = doProcess(annotation.annotationType(), annotationType, traverseClassHierarchy,
@@ -227,7 +215,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
227215
}
228216
}
229217
}
230-
231218
if (traverseClassHierarchy && element instanceof Class) {
232219
Class<?> superclass = ((Class<?>) element).getSuperclass();
233220
if (superclass != null && !superclass.equals(Object.class)) {
@@ -239,7 +226,6 @@ private static <T> T doProcess(AnnotatedElement element, String annotationType,
239226
}
240227
}
241228
}
242-
243229
return null;
244230
}
245231

@@ -252,13 +238,11 @@ private static interface Processor<T> {
252238

253239
/**
254240
* Called to process the annotation.
255-
*
256241
* <p>The {@code metaDepth} parameter represents the depth of the
257242
* annotation relative to the initial element. For example, an annotation
258243
* that is <em>present</em> on the element will have a depth of 0; a
259244
* meta-annotation will have a depth of 1; and a meta-meta-annotation
260245
* will have a depth of 2.
261-
*
262246
* @param annotation the annotation to process
263247
* @param metaDepth the depth of the annotation relative to the initial element
264248
* @return the result of the processing or {@code null} to continue

0 commit comments

Comments
 (0)