diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 3258769e0fb..6cc2f1786d6 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -638,13 +638,13 @@ class ConfigurationClassParser { * Factory method to obtain a {@link SourceClass} from a {@link Class}. */ SourceClass asSourceClass(@Nullable Class classType) throws IOException { - if (classType == null || classType.getName().startsWith("java.lang.annotation")) { + if (classType == null || classType.getName().startsWith("java.lang.annotation.")) { return this.objectSourceClass; } try { // Sanity test that we can reflectively read annotations, // including Class attributes; if not -> fall back to ASM - for (Annotation ann : classType.getAnnotations()) { + for (Annotation ann : classType.getDeclaredAnnotations()) { AnnotationUtils.validateAnnotation(ann); } return new SourceClass(classType); @@ -670,7 +670,7 @@ class ConfigurationClassParser { * Factory method to obtain a {@link SourceClass} from a class name. */ SourceClass asSourceClass(@Nullable String className) throws IOException { - if (className == null || className.startsWith("java.lang.annotation")) { + if (className == null || className.startsWith("java.lang.annotation.")) { return this.objectSourceClass; } if (className.startsWith("java")) { @@ -1017,7 +1017,7 @@ class ConfigurationClassParser { Set result = new LinkedHashSet<>(); if (this.source instanceof Class) { Class sourceClass = (Class) this.source; - for (Annotation ann : sourceClass.getAnnotations()) { + for (Annotation ann : sourceClass.getDeclaredAnnotations()) { Class annType = ann.annotationType(); if (!annType.getName().startsWith("java")) { try { diff --git a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java index 82c435c3b3a..4f23470d968 100644 --- a/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java +++ b/spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java @@ -68,7 +68,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements */ public StandardAnnotationMetadata(Class introspectedClass, boolean nestedAnnotationsAsMap) { super(introspectedClass); - this.annotations = introspectedClass.getAnnotations(); + this.annotations = introspectedClass.getDeclaredAnnotations(); this.nestedAnnotationsAsMap = nestedAnnotationsAsMap; } 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 677e3768ce0..d3ad3fd247b 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 @@ -20,6 +20,7 @@ import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -52,7 +53,7 @@ import static org.junit.Assert.*; public class AnnotationMetadataTests { @Test - public void standardAnnotationMetadata() throws Exception { + public void standardAnnotationMetadata() { AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class, true); doTestAnnotationInfo(metadata); doTestMethodAnnotationInfo(metadata); @@ -68,7 +69,7 @@ public class AnnotationMetadataTests { } @Test - public void standardAnnotationMetadataForSubclass() throws Exception { + public void standardAnnotationMetadataForSubclass() { AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponentSubClass.class, true); doTestSubClassAnnotationInfo(metadata); } @@ -104,7 +105,7 @@ public class AnnotationMetadataTests { } @Test - public void standardAnnotationMetadataForInterface() throws Exception { + public void standardAnnotationMetadataForInterface() { AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotationMetadata.class, true); doTestMetadataForInterfaceClass(metadata); } @@ -132,7 +133,7 @@ public class AnnotationMetadataTests { } @Test - public void standardAnnotationMetadataForAnnotation() throws Exception { + public void standardAnnotationMetadataForAnnotation() { AnnotationMetadata metadata = new StandardAnnotationMetadata(Component.class, true); doTestMetadataForAnnotationClass(metadata); } @@ -172,7 +173,7 @@ public class AnnotationMetadataTests { * 'true' as is done in the main test above. */ @Test - public void standardAnnotationMetadata_nestedAnnotationsAsMap_false() throws Exception { + public void standardAnnotationMetadata_nestedAnnotationsAsMap_false() { AnnotationMetadata metadata = new StandardAnnotationMetadata(AnnotatedComponent.class); AnnotationAttributes specialAttrs = (AnnotationAttributes) metadata.getAnnotationAttributes(SpecialAttr.class.getName()); Annotation[] nestedAnnoArray = (Annotation[]) specialAttrs.get("nestedAnnoArray"); @@ -233,6 +234,20 @@ public class AnnotationMetadataTests { assertMultipleAnnotationsWithIdenticalAttributeNames(metadata); } + @Test + public void inheritedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingStandardAnnotationMetadata() { + AnnotationMetadata metadata = new StandardAnnotationMetadata(NamedComposedAnnotationExtended.class); + assertFalse(metadata.hasAnnotation(NamedComposedAnnotation.class.getName())); + } + + @Test + public void inheritedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception { + MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedComposedAnnotationExtended.class.getName()); + AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); + assertFalse(metadata.hasAnnotation(NamedComposedAnnotation.class.getName())); + } + private void assertMultipleAnnotationsWithIdenticalAttributeNames(AnnotationMetadata metadata) { AnnotationAttributes attributes1 = (AnnotationAttributes) metadata.getAnnotationAttributes( @@ -545,6 +560,7 @@ public class AnnotationMetadataTests { @NamedAnnotation3(name = "name 3") @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) + @Inherited public @interface NamedComposedAnnotation { } @@ -552,4 +568,7 @@ public class AnnotationMetadataTests { public static class NamedComposedAnnotationClass { } + public static class NamedComposedAnnotationExtended extends NamedComposedAnnotationClass { + } + }