From e7ea92561d5d5c0f25e49def6b9d4e7d1dd6b9c6 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 22 May 2015 16:42:26 +0200 Subject: [PATCH] Verify that SynthesizedAnnotation must be public This commit introduces a test that will fail if SynthesizedAnnotation is not public as is required by the contract for getProxyClass() in java.lang.reflect.Proxy. Issue: SPR-13057 --- .../core/annotation/AnnotationUtilsTests.java | 27 +++++++++++++ .../NonPublicAliasedAnnotatedClass.java | 28 +++++++++++++ .../NonPublicAliasedAnnotation.java | 40 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotatedClass.java create mode 100644 spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotation.java diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java index 22163a48b3f..24ea05a04bb 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationUtilsTests.java @@ -34,6 +34,7 @@ import org.junit.rules.ExpectedException; import org.springframework.core.Ordered; import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass; import org.springframework.stereotype.Component; +import org.springframework.util.ClassUtils; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -609,6 +610,32 @@ public class AnnotationUtilsTests { assertEquals("actual value attribute: ", "/test", synthesizedWebMapping.value()); } + /** + * Fully reflection-based test that verifies support for + * {@linkplain AnnotationUtils#synthesizeAnnotation synthesizing annotations} + * across packages with non-public visibility of user types (e.g., a non-public + * annotation that uses {@code @AliasFor}). + */ + @Test + @SuppressWarnings("unchecked") + public void synthesizeNonPublicAnnotationWithAttributeAliasesFromDifferentPackage() throws Exception { + + Class clazz = + ClassUtils.forName("org.springframework.core.annotation.subpackage.NonPublicAliasedAnnotatedClass", null); + Class annotationType = (Class) + ClassUtils.forName("org.springframework.core.annotation.subpackage.NonPublicAliasedAnnotation", null); + + Annotation annotation = clazz.getAnnotation(annotationType); + assertNotNull(annotation); + Annotation synthesizedAnnotation = synthesizeAnnotation(annotation); + assertNotSame(annotation, synthesizedAnnotation); + + assertNotNull(synthesizedAnnotation); + assertEquals("name attribute: ", "test", getValue(synthesizedAnnotation, "name")); + assertEquals("aliased path attribute: ", "/test", getValue(synthesizedAnnotation, "path")); + assertEquals("aliased path attribute: ", "/test", getValue(synthesizedAnnotation, "value")); + } + @Test public void synthesizeAnnotationWithAttributeAliasesInNestedAnnotations() throws Exception { Hierarchy hierarchy = TestCase.class.getAnnotation(Hierarchy.class); diff --git a/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotatedClass.java b/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotatedClass.java new file mode 100644 index 00000000000..d98d5f7cd07 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotatedClass.java @@ -0,0 +1,28 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.annotation.subpackage; + +/** + * Class annotated with a non-public (i.e., package private) custom annotation + * that uses {@code @AliasFor}. + * + * @author Sam Brannen + * @since 4.2 + */ +@NonPublicAliasedAnnotation(name = "test", path = "/test") +class NonPublicAliasedAnnotatedClass { +} diff --git a/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotation.java b/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotation.java new file mode 100644 index 00000000000..c5f6ef5e0a7 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/annotation/subpackage/NonPublicAliasedAnnotation.java @@ -0,0 +1,40 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core.annotation.subpackage; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.springframework.core.annotation.AliasFor; + +/** + * Non-public mock of {@code org.springframework.web.bind.annotation.RequestMapping}. + * + * @author Sam Brannen + * @since 4.2 + */ +@Retention(RetentionPolicy.RUNTIME) +@interface NonPublicAliasedAnnotation { + + String name(); + + @AliasFor(attribute = "path") + String value() default ""; + + @AliasFor(attribute = "value") + String path() default ""; +}