Skip to content

Commit fc9ce7c

Browse files
committed
Skip plain Java annotations in SourceClass.getAnnotations() upfront
Includes direct reflective introspection of annotations when possible. Closes gh-22750
1 parent a1668ad commit fc9ce7c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,14 @@ private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, S
531531
if (visited.add(sourceClass)) {
532532
for (SourceClass annotation : sourceClass.getAnnotations()) {
533533
String annName = annotation.getMetadata().getClassName();
534-
if (!annName.startsWith("java") && !annName.equals(Import.class.getName())) {
534+
if (!annName.equals(Import.class.getName())) {
535535
collectImports(annotation, imports, visited);
536536
}
537537
}
538538
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
539539
}
540540
}
541541

542-
543-
544542
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
545543
Collection<SourceClass> importCandidates, boolean checkForCircularImports) {
546544

@@ -562,8 +560,7 @@ private void processImports(ConfigurationClass configClass, SourceClass currentS
562560
ParserStrategyUtils.invokeAwareMethods(
563561
selector, this.environment, this.resourceLoader, this.registry);
564562
if (selector instanceof DeferredImportSelector) {
565-
this.deferredImportSelectorHandler.handle(
566-
configClass, (DeferredImportSelector) selector);
563+
this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
567564
}
568565
else {
569566
String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
@@ -1016,13 +1013,32 @@ public Set<SourceClass> getInterfaces() throws IOException {
10161013

10171014
public Set<SourceClass> getAnnotations() {
10181015
Set<SourceClass> result = new LinkedHashSet<>();
1019-
for (String className : this.metadata.getAnnotationTypes()) {
1020-
try {
1021-
result.add(getRelated(className));
1016+
if (this.source instanceof Class) {
1017+
Class<?> sourceClass = (Class<?>) this.source;
1018+
for (Annotation ann : sourceClass.getAnnotations()) {
1019+
Class<?> annType = ann.annotationType();
1020+
if (!annType.getName().startsWith("java")) {
1021+
try {
1022+
result.add(asSourceClass(annType));
1023+
}
1024+
catch (Throwable ex) {
1025+
// An annotation not present on the classpath is being ignored
1026+
// by the JVM's class loading -> ignore here as well.
1027+
}
1028+
}
10221029
}
1023-
catch (Throwable ex) {
1024-
// An annotation not present on the classpath is being ignored
1025-
// by the JVM's class loading -> ignore here as well.
1030+
}
1031+
else {
1032+
for (String className : this.metadata.getAnnotationTypes()) {
1033+
if (!className.startsWith("java")) {
1034+
try {
1035+
result.add(getRelated(className));
1036+
}
1037+
catch (Throwable ex) {
1038+
// An annotation not present on the classpath is being ignored
1039+
// by the JVM's class loading -> ignore here as well.
1040+
}
1041+
}
10261042
}
10271043
}
10281044
return result;

0 commit comments

Comments
 (0)