Skip plain Java annotations in SourceClass.getAnnotations() upfront

Includes direct reflective introspection of annotations when possible.

Closes gh-22750
This commit is contained in:
Juergen Hoeller 2019-04-08 15:32:52 +02:00
parent a1668ad1c2
commit fc9ce7cd99
1 changed files with 27 additions and 11 deletions

View File

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