Skip to content

Use AnnotatedElementUtils instead of AnnotationUtils wherever feasible [SPR-13440] #18020

Closed
@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-13440 and commented

Status Quo

Spring Framework 4.2 introduced support for explicit annotation attribute overrides via @AliasFor; however, due to historical reasons, the Spring code base typically uses the lookup methods in AnnotationUtils which do not support attribute overrides.

For example, although Spring MVC supports @RequestMapping as a merged annotation, the same is not true for @ResponseStatus. @ResponseStatus is in fact supported as a meta-annotation, just not as a merged annotation. This means that the responseStatus attribute override in the following custom composed @Post annotation is currently unsupported even though it is declared syntactically correct. Furthermore, use of the responseStatus will fail silently: it will simply be ignored (see #18021).

@RequestMapping(method = POST)
@ResponseStatus
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Post {

  @AliasFor(annotation = RequestMapping.class, attribute = "path")
  String[] value() default {};

  @AliasFor(annotation = RequestMapping.class, attribute = "path")
  String[] path() default {};

  @AliasFor(annotation = ResponseStatus.class, attribute = "code")
  HttpStatus responseStatus() default HttpStatus.CREATED;
}

Deliverables

  1. Determine which annotations in the Spring Framework should be supported as merged annotations (i.e., support attribute overrides).
  2. Migrate from lookups in AnnotationUtils to methods such as findAllMergedAnnotations(), findMergedAnnotation(), or getMergedAnnotation in AnnotatedElementUtils wherever feasible.

Affects: 4.2 GA

Issue Links:

Referenced from: commits 63115ed, c5b318a, 5025c61

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: messagingIssues in messaging modules (jms, messaging)in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions