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)) {
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 {
}
}
private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
Collection<SourceClass> importCandidates, boolean checkForCircularImports) {
@ -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 {
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;