Skip to content

Commit ddebda1

Browse files
committed
Ignoring non-loadable annotations in SourceClass.getAnnotations() and therefore in the collectImports algorithm
Issue: SPR-11086
1 parent 6802f81 commit ddebda1

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

+23-12
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, S
359359
try {
360360
if (visited.add(sourceClass)) {
361361
for (SourceClass annotation : sourceClass.getAnnotations()) {
362-
if (!annotation.getMetadata().getClassName().startsWith("java") && !annotation.isAssignable(Import.class)) {
362+
String annName = annotation.getMetadata().getClassName();
363+
if (!annName.startsWith("java") && !annName.equals(Import.class.getName())) {
363364
collectImports(annotation, imports, visited);
364365
}
365366
}
@@ -525,11 +526,11 @@ public SourceClass asSourceClass(Class<?> classType) throws IOException, ClassNo
525526
try {
526527
// Sanity test that we can read annotations, if not fall back to ASM
527528
classType.getAnnotations();
529+
return new SourceClass(classType);
528530
}
529531
catch (Throwable ex) {
530532
return asSourceClass(classType.getName());
531533
}
532-
return new SourceClass(classType);
533534
}
534535

535536
/**
@@ -549,7 +550,7 @@ public Collection<SourceClass> asSourceClasses(String[] classNames) throws IOExc
549550
public SourceClass asSourceClass(String className) throws IOException, ClassNotFoundException {
550551
if (className.startsWith("java")) {
551552
// Never use ASM for core java types
552-
return new SourceClass(this.resourceLoader.getClassLoader().loadClass( className));
553+
return new SourceClass(this.resourceLoader.getClassLoader().loadClass(className));
553554
}
554555
return new SourceClass(this.metadataReaderFactory.getMetadataReader(className));
555556
}
@@ -718,12 +719,18 @@ public SourceClass getSuperClass() throws IOException, ClassNotFoundException {
718719
return asSourceClass(((MetadataReader) this.source).getClassMetadata().getSuperClassName());
719720
}
720721

721-
public Set<SourceClass> getAnnotations() throws IOException, ClassNotFoundException {
722-
Set<SourceClass> annotations = new LinkedHashSet<SourceClass>();
723-
for (String annotation : this.metadata.getAnnotationTypes()) {
724-
annotations.add(getRelated(annotation));
722+
public Set<SourceClass> getAnnotations() throws IOException {
723+
Set<SourceClass> result = new LinkedHashSet<SourceClass>();
724+
for (String className : this.metadata.getAnnotationTypes()) {
725+
try {
726+
result.add(getRelated(className));
727+
}
728+
catch (Throwable ex) {
729+
// An annotation not present on the classpath is being ignored
730+
// by the JVM's class loading -> ignore here as well.
731+
}
725732
}
726-
return annotations;
733+
return result;
727734
}
728735

729736
public Collection<SourceClass> getAnnotationAttributes(String annotationType, String attribute)
@@ -734,11 +741,11 @@ public Collection<SourceClass> getAnnotationAttributes(String annotationType, St
734741
return Collections.emptySet();
735742
}
736743
String[] classNames = (String[]) annotationAttributes.get(attribute);
737-
Set<SourceClass> rtn = new LinkedHashSet<SourceClass>();
744+
Set<SourceClass> result = new LinkedHashSet<SourceClass>();
738745
for (String className : classNames) {
739-
rtn.add(getRelated(className));
746+
result.add(getRelated(className));
740747
}
741-
return rtn;
748+
return result;
742749
}
743750

744751
private SourceClass getRelated(String className) throws IOException, ClassNotFoundException {
@@ -748,7 +755,11 @@ private SourceClass getRelated(String className) throws IOException, ClassNotFou
748755
return asSourceClass(clazz);
749756
}
750757
catch (ClassNotFoundException ex) {
751-
// ignore
758+
// Ignore -> fall back to ASM next, except for core java types.
759+
if (className.startsWith("java")) {
760+
throw ex;
761+
}
762+
return new SourceClass(metadataReaderFactory.getMetadataReader(className));
752763
}
753764
}
754765
return asSourceClass(className);

0 commit comments

Comments
 (0)