Throw exception if required meta-annotation is not present

It is a configuration error if an alias is declared via @AliasFor for
an attribute in a meta-annotation and the meta-annotation is not
meta-present. However, prior to this commit, the support for validating
the configuration of @AliasFor in AnnotationUtils currently silently
ignored such errors.

This commit fixes this by throwing an AnnotationConfigurationException
whenever a required meta-annotation is not present or meta-present on
an annotation that declares an explicit alias for an attribute in the
meta-annotation.

Issue: SPR-13335
This commit is contained in:
Sam Brannen
2015-08-09 19:14:29 +02:00
parent 78ff4ff542
commit 8289036165
3 changed files with 47 additions and 13 deletions

View File

@@ -831,6 +831,18 @@ public class AnnotationUtilsTests {
synthesizeAnnotation(annotation);
}
@Test
public void synthesizeAnnotationWithAttributeAliasForMetaAnnotationThatIsNotMetaPresent() throws Exception {
AliasedComposedContextConfigNotMetaPresent annotation = AliasedComposedContextConfigNotMetaPresentClass.class.getAnnotation(AliasedComposedContextConfigNotMetaPresent.class);
exception.expect(AnnotationConfigurationException.class);
exception.expectMessage(startsWith("@AliasFor declaration on attribute [xmlConfigFile] in annotation"));
exception.expectMessage(containsString(AliasedComposedContextConfigNotMetaPresent.class.getName()));
exception.expectMessage(containsString("declares an alias for attribute [locations] in meta-annotation"));
exception.expectMessage(containsString(ContextConfig.class.getName()));
exception.expectMessage(endsWith("which is not meta-present."));
synthesizeAnnotation(annotation);
}
@Test
public void synthesizeAnnotationWithAttributeAliases() throws Exception {
Method method = WebController.class.getMethod("handleMappedWithValueAttribute");
@@ -1661,6 +1673,18 @@ public class AnnotationUtilsTests {
static class AliasForAttributeWithDifferentDefaultValueClass {
}
// @ContextConfig --> Intentionally NOT meta-present
@Retention(RetentionPolicy.RUNTIME)
@interface AliasedComposedContextConfigNotMetaPresent {
@AliasFor(annotation = ContextConfig.class, attribute = "locations")
String xmlConfigFile();
}
@AliasedComposedContextConfigNotMetaPresent(xmlConfigFile = "test.xml")
static class AliasedComposedContextConfigNotMetaPresentClass {
}
@ContextConfig
@Retention(RetentionPolicy.RUNTIME)
@interface AliasedComposedContextConfig {