Skip to content

Transitive implicit aliases do not always work with @AliasFor in merged annotations [SPR-14069] #18641

@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-14069 and commented

Status Quo

For the most post, transitive implicit aliases for annotation attribute overrides configured via @AliasFor work. However, I have detected a corner case that fails.

Failing Example

Given the following three annotations, if an attempt is made to look up a merged @ContextConfig annotation via AnnotatedElementUtils (e.g., using getMergedAnnotation() or findMergedAnnotation()) on an element annotated with @TransitiveImplicitAliasesContextConfig then the look up will fail with an AnnotationConfigurationException stating something similar to the following:

attribute 'value' and its alias 'locations' are declared with values of [{}] and [{test.groovy}], but only one is permitted.

If the value attribute in ImplicitAliasesContextConfig declares attribute = "locations" via @AliasFor, the code then works as expected. Thus, there is obviously a bug in the lookup mechanism for names of overridden attributes in conjunction with transitive implicit aliases.

@Retention(RetentionPolicy.RUNTIME)
@interface ContextConfig {

	@AliasFor(attribute = "locations")
	String[] value() default {};

	@AliasFor(attribute = "value")
	String[] locations() default {};

	Class<?>[] classes() default {};
}
@ContextConfig
@Retention(RetentionPolicy.RUNTIME)
@interface ImplicitAliasesContextConfig {

	@AliasFor(annotation = ContextConfig.class, attribute = "locations")
	String[] groovyScripts() default {};

	@AliasFor(annotation = ContextConfig.class, attribute = "locations")
	String[] xmlFiles() default {};

	// intentionally omitted: attribute = "locations"
	@AliasFor(annotation = ContextConfig.class)
	String[] locations() default {};

	// intentionally omitted: attribute = "locations"
	@AliasFor(annotation = ContextConfig.class)
	String[] value() default {};
}
@ImplicitAliasesContextConfig
@Retention(RetentionPolicy.RUNTIME)
@interface TransitiveImplicitAliasesContextConfig {

	@AliasFor(annotation = ImplicitAliasesContextConfig.class, attribute = "xmlFiles")
	String[] xml() default {};

	@AliasFor(annotation = ImplicitAliasesContextConfig.class, attribute = "groovyScripts")
	String[] groovy() default {};
}

Deliverables

  1. Determine why the aforementioned example fails and fix the bug.
    • See TODO in AnnotatedElementUtilsTests.

Affects: 4.2.2

Referenced from: commits d22480b, 2153d88

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions