micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.03k stars 1.05k forks source link

Improve how InterceptedProxy's are generated #10044

Open aaronjwhiteside opened 10 months ago

aaronjwhiteside commented 10 months ago

Feature description

Per the discord help post: https://discordapp.com/channels/1121511613250412714/1167301199255568394

I have a situation where there are classes compiled outside the preview of the Micronaut's annotation processors. These classes have annotations on their methods that I want to use to apply a Micronaut MethodInterceptor to.

This causes issues because Micronaut has to be aware of the concrete class that contains these annotations, and when combined with the need to apply MethodInterceptor's to this class, it means Micronaut wants to extend the class, and so the class in question needed to be open to extending, or we need to move the annotations on to an interface and away from the concrete class, which causes issues with our hexagonal design/architecture approach.

What I propose is an optimization to how the InterceptedProxy is generated, from the metadata in the @Factory class.

If these conditions are met: 1) For a given @Factory function, if it has a @Bean annotation where typed is supplied with only interfaces 2) And the @Bean function returns a concrete type, implementing said interfaces 3) And you needed to apply some AOP to the bean, @Transactional or custom MethodInterceptors etc..

Then 1) Generate the ExecutableMethodsDefinition using the concrete return type of the @Bean function as usual. 2) BUT, generate the InterceptedProxy using the interfaces declared in the @Bean's typed list ie. don't try and extend the concrete return type to generate a proxy, if we only need to expose the bean with a given set of interfaces.

If this improvement was implemented, then I wouldn't need to open up the concrete class and the aspects being applied wouldn't need ProxyTargetConstructorMode.ALLOW or for Micronaut to extend the bean class at all.

graemerocher commented 10 months ago

Sounds like a great idea

katoquro commented 8 months ago

Hello, is this issue related to my problem? https://github.com/micronaut-projects/micronaut-core/issues/10243

graemerocher commented 8 months ago

@dstepanov can you take a look?