Skip to content

Commit a092dc0

Browse files
committed
Check for instanceof Throwable in AnnotationAttributes#assertNotException
Prior to this commit, AnnotationAttributes#assertNotException checked if the attribute value was an instance of Exception. Although this was typically sufficient, the scope was not always broad enough -- for example, if AnnotationReadingVisitorUtils#convertClassValues stored a Throwable in the map (such as a LinkageError). This commit fixes this by checking for an instance of Throwable in AnnotationAttributes#assertNotException. Closes gh-23424
1 parent 70be711 commit a092dc0

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ private void assertAttributePresence(String attributeName, Object attributeValue
353353
}
354354

355355
private void assertNotException(String attributeName, Object attributeValue) {
356-
if (attributeValue instanceof Exception) {
356+
if (attributeValue instanceof Throwable) {
357357
throw new IllegalArgumentException(String.format(
358358
"Attribute '%s' for annotation [%s] was not resolvable due to exception [%s]",
359-
attributeName, this.displayName, attributeValue), (Exception) attributeValue);
359+
attributeName, this.displayName, attributeValue), (Throwable) attributeValue);
360360
}
361361
}
362362

spring-core/src/test/java/org/springframework/core/annotation/AnnotationAttributesTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,21 @@ public void typeSafeAttributeAccess() {
7777
}
7878

7979
@Test
80-
public void unresolvableClass() throws Exception {
80+
public void unresolvableClassWithClassNotFoundException() throws Exception {
8181
attributes.put("unresolvableClass", new ClassNotFoundException("myclass"));
8282
exception.expect(IllegalArgumentException.class);
8383
exception.expectMessage(containsString("myclass"));
8484
attributes.getClass("unresolvableClass");
8585
}
8686

87+
@Test
88+
public void unresolvableClassWithLinkageError() throws Exception {
89+
attributes.put("unresolvableClass", new LinkageError("myclass"));
90+
exception.expect(IllegalArgumentException.class);
91+
exception.expectMessage(containsString("myclass"));
92+
attributes.getClass("unresolvableClass");
93+
}
94+
8795
@Test
8896
public void singleElementToSingleElementArrayConversionSupport() throws Exception {
8997
Filter filter = FilteredClass.class.getAnnotation(Filter.class);

0 commit comments

Comments
 (0)