diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index 1506ec18f9..32c2c927e2 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -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; diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index c5969b9bac..1a92f718e7 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -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"); } /** diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java index 62ceb04d7c..cb7ffa96dc 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java @@ -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; 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 @Override public boolean isAnnotated(String annotationType) { - return this.attributesMap.containsKey(annotationType); + return (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationType) && this.attributesMap.containsKey(annotationType)); } @Override diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java index d2195af6a8..89c04d351e 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotatedElementUtilsTests.java @@ -57,13 +57,13 @@ public class AnnotatedElementUtilsTests { @Test public void getMetaAnnotationTypesOnClassWithMetaDepth1() { Set 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 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 diff --git a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java index 78f32d9ee4..fd59b59f11 100644 --- a/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java +++ b/spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java @@ -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 { 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));