Browse Source

Consistent non-exposure of inherited annotations in AnnotationMetadata

Closes gh-22766
pull/22768/head
Juergen Hoeller 7 years ago
parent
commit
fd8fa301a6
  1. 8
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
  2. 2
      spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java
  3. 29
      spring-core/src/test/java/org/springframework/core/type/AnnotationMetadataTests.java

8
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

@ -638,13 +638,13 @@ class ConfigurationClassParser { @@ -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 { @@ -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 { @@ -1017,7 +1017,7 @@ class ConfigurationClassParser {
Set<SourceClass> 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 {

2
spring-core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

@ -68,7 +68,7 @@ public class StandardAnnotationMetadata extends StandardClassMetadata implements @@ -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;
}

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

@ -20,6 +20,7 @@ import java.io.Serializable; @@ -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.*; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -552,4 +568,7 @@ public class AnnotationMetadataTests {
public static class NamedComposedAnnotationClass {
}
public static class NamedComposedAnnotationExtended extends NamedComposedAnnotationClass {
}
}

Loading…
Cancel
Save