Browse Source

Skip plain Java annotations in SourceClass.getAnnotations() upfront

Includes direct reflective introspection of annotations when possible.

Closes gh-22750
pull/23050/head
Juergen Hoeller 6 years ago
parent
commit
fc9ce7cd99
  1. 38
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

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

@ -531,7 +531,7 @@ class ConfigurationClassParser { @@ -531,7 +531,7 @@ class ConfigurationClassParser {
if (visited.add(sourceClass)) {
for (SourceClass annotation : sourceClass.getAnnotations()) {
String annName = annotation.getMetadata().getClassName();
if (!annName.startsWith("java") && !annName.equals(Import.class.getName())) {
if (!annName.equals(Import.class.getName())) {
collectImports(annotation, imports, visited);
}
}
@ -539,8 +539,6 @@ class ConfigurationClassParser { @@ -539,8 +539,6 @@ class ConfigurationClassParser {
}
}
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
Collection<SourceClass> importCandidates, boolean checkForCircularImports) {
@ -562,8 +560,7 @@ class ConfigurationClassParser { @@ -562,8 +560,7 @@ class ConfigurationClassParser {
ParserStrategyUtils.invokeAwareMethods(
selector, this.environment, this.resourceLoader, this.registry);
if (selector instanceof DeferredImportSelector) {
this.deferredImportSelectorHandler.handle(
configClass, (DeferredImportSelector) selector);
this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
}
else {
String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
@ -1016,13 +1013,32 @@ class ConfigurationClassParser { @@ -1016,13 +1013,32 @@ class ConfigurationClassParser {
public Set<SourceClass> getAnnotations() {
Set<SourceClass> result = new LinkedHashSet<>();
for (String className : this.metadata.getAnnotationTypes()) {
try {
result.add(getRelated(className));
if (this.source instanceof Class) {
Class<?> sourceClass = (Class<?>) this.source;
for (Annotation ann : sourceClass.getAnnotations()) {
Class<?> annType = ann.annotationType();
if (!annType.getName().startsWith("java")) {
try {
result.add(asSourceClass(annType));
}
catch (Throwable ex) {
// An annotation not present on the classpath is being ignored
// by the JVM's class loading -> ignore here as well.
}
}
}
catch (Throwable ex) {
// An annotation not present on the classpath is being ignored
// by the JVM's class loading -> ignore here as well.
}
else {
for (String className : this.metadata.getAnnotationTypes()) {
if (!className.startsWith("java")) {
try {
result.add(getRelated(className));
}
catch (Throwable ex) {
// An annotation not present on the classpath is being ignored
// by the JVM's class loading -> ignore here as well.
}
}
}
}
return result;

Loading…
Cancel
Save