Eliminate inspection of annotations on core Java annotations
This commit picks up where SPR-11483 left off, with the goal of eliminating all unnecessary inspection of core JDK annotations in Spring's annotation search algorithms in AnnotatedElementUtils and AnnotationMetadataReadingVisitor. Issue: SPR-12989
This commit is contained in:
parent
ba84458c65
commit
5d67219a4e
|
@ -406,8 +406,8 @@ public class AnnotatedElementUtils {
|
||||||
|
|
||||||
// Search in local annotations
|
// Search in local annotations
|
||||||
for (Annotation annotation : annotations) {
|
for (Annotation annotation : annotations) {
|
||||||
// TODO Test for !AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
|
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
|
||||||
if (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0) {
|
&& (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0)) {
|
||||||
T result = processor.process(annotation, metaDepth);
|
T result = processor.process(annotation, metaDepth);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -636,7 +636,19 @@ public abstract class AnnotationUtils {
|
||||||
*/
|
*/
|
||||||
public static boolean isInJavaLangAnnotationPackage(Annotation annotation) {
|
public static boolean isInJavaLangAnnotationPackage(Annotation annotation) {
|
||||||
Assert.notNull(annotation, "Annotation must not be null");
|
Assert.notNull(annotation, "Annotation must not be null");
|
||||||
return annotation.annotationType().getName().startsWith("java.lang.annotation");
|
return isInJavaLangAnnotationPackage(annotation.annotationType().getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the {@link Annotation} with the supplied name is defined
|
||||||
|
* in the core JDK {@code java.lang.annotation} package.
|
||||||
|
* @param annotationType the name of the annotation type to check (never {@code null} or empty)
|
||||||
|
* @return {@code true} if the annotation is in the {@code java.lang.annotation} package
|
||||||
|
* @since 4.2
|
||||||
|
*/
|
||||||
|
public static boolean isInJavaLangAnnotationPackage(String annotationType) {
|
||||||
|
Assert.hasText(annotationType, "annotationType must not be null or empty");
|
||||||
|
return annotationType.startsWith("java.lang.annotation");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -28,6 +28,7 @@ import org.springframework.asm.MethodVisitor;
|
||||||
import org.springframework.asm.Opcodes;
|
import org.springframework.asm.Opcodes;
|
||||||
import org.springframework.asm.Type;
|
import org.springframework.asm.Type;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.core.type.MethodMetadata;
|
import org.springframework.core.type.MethodMetadata;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
@ -114,7 +115,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAnnotated(String annotationType) {
|
public boolean isAnnotated(String annotationType) {
|
||||||
return this.attributesMap.containsKey(annotationType);
|
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationType) && this.attributesMap.containsKey(annotationType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -57,13 +57,13 @@ public class AnnotatedElementUtilsTests {
|
||||||
@Test
|
@Test
|
||||||
public void getMetaAnnotationTypesOnClassWithMetaDepth1() {
|
public void getMetaAnnotationTypesOnClassWithMetaDepth1() {
|
||||||
Set<String> names = getMetaAnnotationTypes(TransactionalComponentClass.class, TransactionalComponent.class);
|
Set<String> names = getMetaAnnotationTypes(TransactionalComponentClass.class, TransactionalComponent.class);
|
||||||
assertEquals(names(Transactional.class, Component.class, Retention.class, Documented.class, Target.class, Inherited.class), names);
|
assertEquals(names(Transactional.class, Component.class), names);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
|
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
|
||||||
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class);
|
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class);
|
||||||
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class, Retention.class, Documented.class, Target.class, Inherited.class), names);
|
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class), names);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -154,7 +154,7 @@ public class AnnotationMetadataTests {
|
||||||
assertThat(metadata.getSuperClassName(), nullValue());
|
assertThat(metadata.getSuperClassName(), nullValue());
|
||||||
assertThat(metadata.getInterfaceNames().length, is(1));
|
assertThat(metadata.getInterfaceNames().length, is(1));
|
||||||
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
|
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
|
||||||
assertThat(metadata.isAnnotated(Documented.class.getName()), is(true));
|
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
|
||||||
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
|
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
|
||||||
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
|
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
|
||||||
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));
|
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));
|
||||||
|
|
Loading…
Reference in New Issue