Skip to content

Commit 5d67219

Browse files
committed
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
1 parent ba84458 commit 5d67219

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,8 @@ private static <T> T processWithGetSemantics(AnnotatedElement element, String an
406406

407407
// Search in local annotations
408408
for (Annotation annotation : annotations) {
409-
// TODO Test for !AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
410-
if (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0) {
409+
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
410+
&& (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0)) {
411411
T result = processor.process(annotation, metaDepth);
412412
if (result != null) {
413413
return result;

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,19 @@ public static boolean isAnnotationInherited(Class<? extends Annotation> annotati
636636
*/
637637
public static boolean isInJavaLangAnnotationPackage(Annotation annotation) {
638638
Assert.notNull(annotation, "Annotation must not be null");
639-
return annotation.annotationType().getName().startsWith("java.lang.annotation");
639+
return isInJavaLangAnnotationPackage(annotation.annotationType().getName());
640+
}
641+
642+
/**
643+
* Determine if the {@link Annotation} with the supplied name is defined
644+
* in the core JDK {@code java.lang.annotation} package.
645+
* @param annotationType the name of the annotation type to check (never {@code null} or empty)
646+
* @return {@code true} if the annotation is in the {@code java.lang.annotation} package
647+
* @since 4.2
648+
*/
649+
public static boolean isInJavaLangAnnotationPackage(String annotationType) {
650+
Assert.hasText(annotationType, "annotationType must not be null or empty");
651+
return annotationType.startsWith("java.lang.annotation");
640652
}
641653

642654
/**

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.asm.Opcodes;
2929
import org.springframework.asm.Type;
3030
import org.springframework.core.annotation.AnnotationAttributes;
31+
import org.springframework.core.annotation.AnnotationUtils;
3132
import org.springframework.core.type.AnnotationMetadata;
3233
import org.springframework.core.type.MethodMetadata;
3334
import org.springframework.util.LinkedMultiValueMap;
@@ -114,7 +115,7 @@ public boolean hasMetaAnnotation(String metaAnnotationType) {
114115

115116
@Override
116117
public boolean isAnnotated(String annotationType) {
117-
return this.attributesMap.containsKey(annotationType);
118+
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationType) && this.attributesMap.containsKey(annotationType));
118119
}
119120

120121
@Override

spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void getMetaAnnotationTypesOnNonAnnotatedClass() {
5757
@Test
5858
public void getMetaAnnotationTypesOnClassWithMetaDepth1() {
5959
Set<String> names = getMetaAnnotationTypes(TransactionalComponentClass.class, TransactionalComponent.class);
60-
assertEquals(names(Transactional.class, Component.class, Retention.class, Documented.class, Target.class, Inherited.class), names);
60+
assertEquals(names(Transactional.class, Component.class), names);
6161
}
6262

6363
@Test
6464
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
6565
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class);
66-
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class, Retention.class, Documented.class, Target.class, Inherited.class), names);
66+
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class), names);
6767
}
6868

6969
@Test

spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -154,7 +154,7 @@ private void doTestMetadataForAnnotationClass(AnnotationMetadata metadata) {
154154
assertThat(metadata.getSuperClassName(), nullValue());
155155
assertThat(metadata.getInterfaceNames().length, is(1));
156156
assertThat(metadata.getInterfaceNames()[0], is(Annotation.class.getName()));
157-
assertThat(metadata.isAnnotated(Documented.class.getName()), is(true));
157+
assertThat(metadata.isAnnotated(Documented.class.getName()), is(false));
158158
assertThat(metadata.isAnnotated(Scope.class.getName()), is(false));
159159
assertThat(metadata.isAnnotated(SpecialAttr.class.getName()), is(false));
160160
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));

0 commit comments

Comments
 (0)