Skip to content

Commit 8abe949

Browse files
author
Phillip Webb
committed
Fix ArrayStoreException reading subclassed enums
Fix ASM AnnotationAttributesReadingVisitor to correctly deal with subclasses enums. Issue: SPR-10914
1 parent f3611e7 commit 8abe949

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import org.apache.commons.logging.Log;
3030
import org.apache.commons.logging.LogFactory;
31-
3231
import org.springframework.asm.AnnotationVisitor;
3332
import org.springframework.asm.SpringAsmInfo;
3433
import org.springframework.asm.Type;
@@ -131,7 +130,13 @@ public void visit(String attributeName, Object attributeValue) {
131130
newValue = ObjectUtils.addObjectToArray((Object[]) existingValue, newValue);
132131
}
133132
else {
134-
Object[] newArray = (Object[]) Array.newInstance(newValue.getClass(), 1);
133+
Class<?> arrayClass = newValue.getClass();
134+
if(Enum.class.isAssignableFrom(arrayClass)) {
135+
while(arrayClass.getSuperclass() != null && !arrayClass.isEnum()) {
136+
arrayClass = arrayClass.getSuperclass();
137+
}
138+
}
139+
Object[] newArray = (Object[]) Array.newInstance(arrayClass, 1);
135140
newArray[0] = newValue;
136141
newValue = newArray;
137142
}

spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void doTestAnnotationInfo(AnnotationMetadata metadata) {
9898
assertThat(metadata.hasAnnotation(Component.class.getName()), is(true));
9999
assertThat(metadata.hasAnnotation(Scope.class.getName()), is(true));
100100
assertThat(metadata.hasAnnotation(SpecialAttr.class.getName()), is(true));
101-
assertThat(metadata.getAnnotationTypes().size(), is(5));
101+
assertThat(metadata.getAnnotationTypes().size(), is(6));
102102
assertThat(metadata.getAnnotationTypes().contains(Component.class.getName()), is(true));
103103
assertThat(metadata.getAnnotationTypes().contains(Scope.class.getName()), is(true));
104104
assertThat(metadata.getAnnotationTypes().contains(SpecialAttr.class.getName()), is(true));
@@ -247,6 +247,20 @@ public static enum SomeEnum {
247247
public @interface MetaMetaAnnotation {
248248
}
249249

250+
@Target(ElementType.TYPE)
251+
@Retention(RetentionPolicy.RUNTIME)
252+
public @interface EnumSubclasses {
253+
SubclassEnum[] value();
254+
}
255+
256+
// SPR-10914
257+
public static enum SubclassEnum {
258+
FOO {
259+
},
260+
BAR {
261+
};
262+
}
263+
250264
@Component("myName")
251265
@Scope("myScope")
252266
@SpecialAttr(clazz = String.class, state = Thread.State.NEW,
@@ -258,6 +272,7 @@ public static enum SomeEnum {
258272
@SuppressWarnings({"serial", "unused"})
259273
@DirectAnnotation("direct")
260274
@MetaMetaAnnotation
275+
@EnumSubclasses({ SubclassEnum.FOO, SubclassEnum.BAR })
261276
private static class AnnotatedComponent implements Serializable {
262277

263278
@TestAutowired

0 commit comments

Comments
 (0)