From 8a293f51a4d1f7edcb6a4403826b96f7795d2a76 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 4 May 2019 08:28:30 -0700 Subject: [PATCH] Don't expose RetentionPolicy.CLASS annotations Update ASM based metadata readers so that only RetentionPolicy.RUNTIME annotations are exposed. This aligned behavior with the reflection based implementation. Closes gh-22886 --- .../src/test/java/example/scannable_scoped/MyScope.java | 6 +++++- .../annotation/configuration/spr9031/Spr9031Tests.java | 6 +++++- .../type/classreading/AnnotationMetadataReadingVisitor.java | 5 ++++- .../type/classreading/MethodMetadataReadingVisitor.java | 5 ++++- .../core/type/AnnotationTypeFilterTests.java | 6 +++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/spring-context/src/test/java/example/scannable_scoped/MyScope.java b/spring-context/src/test/java/example/scannable_scoped/MyScope.java index caf69aa7b3..b21fc4d0e1 100644 --- a/spring-context/src/test/java/example/scannable_scoped/MyScope.java +++ b/spring-context/src/test/java/example/scannable_scoped/MyScope.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -16,9 +16,13 @@ package example.scannable_scoped; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.ScopedProxyMode; +@Retention(RetentionPolicy.RUNTIME) public @interface MyScope { String value() default BeanDefinition.SCOPE_SINGLETON; ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT; diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/spr9031/Spr9031Tests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/spr9031/Spr9031Tests.java index b10006aaec..e0283e7900 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/spr9031/Spr9031Tests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/spr9031/Spr9031Tests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2019 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. @@ -16,6 +16,9 @@ package org.springframework.context.annotation.configuration.spr9031; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -75,5 +78,6 @@ public class Spr9031Tests { @Autowired Spr9031Component scanned; } + @Retention(RetentionPolicy.RUNTIME) public @interface MarkerAnnotation {} } 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 1cd76e0ca3..03a96cf431 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-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -85,6 +85,9 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + if (!visible) { + return null; + } String className = Type.getType(desc).getClassName(); this.annotationSet.add(className); return new AnnotationAttributesReadingVisitor( diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java index 97dc10024e..a0e681577e 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -79,6 +79,9 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho @Override public AnnotationVisitor visitAnnotation(final String desc, boolean visible) { + if (!visible) { + return null; + } this.methodMetadataSet.add(this); String className = Type.getType(desc).getClassName(); return new AnnotationAttributesReadingVisitor( diff --git a/spring-core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java b/spring-core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java index c752246f31..1dc7235ce9 100644 --- a/spring-core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java +++ b/spring-core/src/test/java/org/springframework/core/type/AnnotationTypeFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -17,6 +17,8 @@ package org.springframework.core.type; import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import org.junit.Test; @@ -108,6 +110,7 @@ public class AnnotationTypeFilterTests { // and interfering with ClassloadingAssertions.assertClassNotLoaded() @Inherited + @Retention(RetentionPolicy.RUNTIME) private @interface InheritedAnnotation { } @@ -132,6 +135,7 @@ public class AnnotationTypeFilterTests { } + @Retention(RetentionPolicy.RUNTIME) private @interface NonInheritedAnnotation { }