Browse Source

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
pull/791/head
Sam Brannen 10 years ago
parent
commit
5d67219a4e
  1. 4
      spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java
  2. 14
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java
  3. 5
      spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java
  4. 4
      spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java
  5. 4
      spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

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

@ -406,8 +406,8 @@ public class AnnotatedElementUtils { @@ -406,8 +406,8 @@ public class AnnotatedElementUtils {
// Search in local annotations
for (Annotation annotation : annotations) {
// TODO Test for !AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
if (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)
&& (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0)) {
T result = processor.process(annotation, metaDepth);
if (result != null) {
return result;

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

@ -636,7 +636,19 @@ public abstract class AnnotationUtils { @@ -636,7 +636,19 @@ public abstract class AnnotationUtils {
*/
public static boolean isInJavaLangAnnotationPackage(Annotation annotation) {
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");
}
/**

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.asm.MethodVisitor; @@ -28,6 +28,7 @@ import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Opcodes;
import org.springframework.asm.Type;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.LinkedMultiValueMap;
@ -114,7 +115,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @@ -114,7 +115,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
@Override
public boolean isAnnotated(String annotationType) {
return this.attributesMap.containsKey(annotationType);
return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationType) && this.attributesMap.containsKey(annotationType));
}
@Override

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

@ -57,13 +57,13 @@ public class AnnotatedElementUtilsTests { @@ -57,13 +57,13 @@ public class AnnotatedElementUtilsTests {
@Test
public void getMetaAnnotationTypesOnClassWithMetaDepth1() {
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
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
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

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -154,7 +154,7 @@ public class AnnotationMetadataTests { @@ -154,7 +154,7 @@ public class AnnotationMetadataTests {
assertThat(metadata.getSuperClassName(), nullValue());
assertThat(metadata.getInterfaceNames().length, is(1));
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(SpecialAttr.class.getName()), is(false));
assertThat(metadata.hasAnnotation(Documented.class.getName()), is(true));

Loading…
Cancel
Save