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
This commit is contained in:
Sam Brannen 2015-05-22 16:42:26 +02:00
parent ca09b1ff20
commit e7ea92561d
3 changed files with 95 additions and 0 deletions

View File

@ -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<? extends Annotation> annotationType = (Class<? extends Annotation>)
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);

View File

@ -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 {
}

View File

@ -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 "";
}