Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a meta-annotation that indicates the annotated element requires reflection hints #28469

Closed
snicoll opened this issue May 17, 2022 · 1 comment
Assignees
Labels
theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented May 17, 2022

We're now at a stage where we need to port a number of hints in a sustainable way and avoid having to write dedicated code for common patterns we have in the portfolio.

On such simple pattern is the following:

@EventListener
public void onContextRefresh(ContextRefreshedEvent event) { ... }

The internal implementation processing @EventListener is creating a wrapper that invokes the method via reflection so that needs an invocation hint to work in restricted environment (native image).

A more complex pattern could be

@GetMapping("/dtos/{id}")
public MyDto findById(String id) { ... }

In this case, not only the findById method is invoked by reflection but the chances are high that MyDto is going to be serialized using some sort of converter and therefore require additional hints.

Rather than having dedicated BeanRegistrationAotProcessor or BeanFactoryInitializationAotProcessor we could build a generic one that checks for the presence of a certain annotation. We could then annotate the annotations that we know require reflection so that they are processed semi-automatically.

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Reflective
public @interface EventListener { ... }

@Reflective could take the strategy to use (a bit like @Conditional does). This would help us implement a more fine-grained algorithm like the MVC use case.

@snicoll snicoll added type: enhancement A general enhancement theme: aot An issue related to Ahead-of-time processing labels May 17, 2022
@snicoll snicoll added this to the 6.0.0-M5 milestone May 17, 2022
@snicoll
Copy link
Member Author

snicoll commented May 17, 2022

One thing to mention is that adding this meta-annotation can be temporary. Once the underlying infrastructure is fully resolved ahead-of-time, and therefore the need for reflection for AOT-based context is no longer necessary, the meta-annotation can be simply removed.

@sbrannen sbrannen changed the title Introduce a meta-annotation that indicate the annotated element requires reflection hints Introduce a meta-annotation that indicates the annotated element requires reflection hints May 17, 2022
@snicoll snicoll self-assigned this May 18, 2022
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant