-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
METHOD_PARAMETER support in AnnotationsTransformer #8054
Comments
Hi, I tried to test the code I implemented as a demonstration but it seems not to work, or to put it in more details, it seems that parameters support in Jandex is not complete. There is a code there, but it seems not work well. The problem is that all the annotations I have on parameters are reported by Jandex as a |
So in order to support |
I tried filtering in my demo, but after more investigation it seems to me that no annotation is |
Do you have a link to your test? |
I apologise, this was a bug in my test. The annotations present on parameters are indeed processed as In regards to:
This is a problem only when we go with the option 1. But with the option 2 we have no problems because there are no clients which are explicitly applied to And in regards to:
Well, this is actually possible but the API it is limited as hell and results in hardly readable code when used. I wrapped it in some nice facade which corresponds to the underlying annotation target in a nicer way, so to get list of parameters from Which I can use in the following way (here in a build step): @BuildStep
AnnotationsTransformerBuildItem doMissingParameterAnnotationsTransformation(Reflector reflector) {
final DotName dn = DotName.createSimple("com.github.sarxos.abberwoult.annotation.ReceivesTesting$ReceivesActor");
final ClassRef clazz = reflector.findClass(dn).getOrNull();
if (clazz != null) {
for (MethodRef method : clazz.getMethods()) {
if (!method.getName().equals("onInteger")) {
continue;
}
System.out.println("method ........ " + method.getName());
for (ParameterRef p : method.getParameters()) {
System.out.println(" parameter ...... " + p.getPosition());
System.out.println(" name ........... " + p.getName());
System.out.println(" annotations .... " + p.getAnnotations());
System.out.println(" type ........... " + p.getTypeName());
System.out.println(" --- ");
}
}
}
return new AnnotationsTransformerBuildItem(new MissingParameterAnnotationsTransformer(reflector));
} With the class like this: public class ReceivesTesting {
public static class ReceivesActor extends SimpleActor implements Utils {
public void onInteger(@Receives Integer index, Long id) {
reply(11);
}
}
} It outputs:
|
Let me describe the use case which caused me to propose this feature. I have methods which can receive some values, e.g.: public void onInteger(@Receives Integer index) { ... }
public void onSomething(@Receives Something some) { ... } And there is a possibility to receive it from a specific source, e.g. from events stream. Such parameters are annotated with public void onSomething(@Receives @Event Something some) { ... } So what I wanted to achieve is that, similarly to how So when someone annotate it like this: public void onSomething(@Event Something some) { ... } It will automatically become: public void onSomething(@Receives @Event Something some) { ... } I found this to be simple for fields and methods since this is well supported, but I found no way to do it for method parameters. Just for your information. This is not a blocker for me. I can implement the same functionality in a few different ways, but I choose to do this with I hoped to implement support for Initially I hoped that it will be enough to use my code, but it seems not to work well and after thinking about time required to implement this and potential drawbacks I'm really considering to drop this feature request... What are your thoughts? |
The "client code" was calling |
Ok, I get your point. Indeed in my w/a I do not work on top of |
Hello Quarkus Team!
Description
The
AnnotationsTransformer
has this method to decide if transformer implementation should be used for annotation of a specific target kind:And right now it works with the following target kinds:
AnnotationTarget.Kind.CLASS
AnnotationTarget.Kind.METHOD
AnnotationTarget.Kind.FIELD
Especially, it works just fine when I use it like this:
But as you can see above, there is no support for
AnnotationTarget.Kind.METHOD_PARAMETER
so the following will not work and the transformer will not run at all:This is because of
AnnotationStore
internal implementation, which, when I use:Will be applied to annotations of both
METHOD_PARAMETER
andMETHOD
targets. This is tricky and unclear and in overall cause this weird logic:METHOD
it will transform method and parameter annotations.METHOD_PARAMETER
it will transform no annotations at all.As @mkouba explained to me in #2493 - this is because Jandex API for method parameters is very limited and in result cause user unfriendly
AnnotationsTransformer
API.Implementation ideas
Ok, the idea is here (not tested):
sarxos@d298653
However I have some doubts in regards to if and how this change should be handled. This is something I would like to discuss.
My first doubt is in regards to which direction change in logic should be made. I have two ideas:
1.a. When transformer is applied to
METHOD
it will consume method annotations only.1.b. When transformer is applied to
METHOD_PARAMETER
it will consume parameter annotations only.METHOD_PARAMETER
support:2.a. When transformer is applied to
METHOD
it will consume both method and parameter annotations.2.b. When transformer is applied to
METHOD_PARAMETER
it will consume parameter annotationsI would prefer option 1 since it introduces well defined logic in the public API, at the price of a little bit more complicated internal implementation to w/a Jandex limitations.
Option 2 is so-so, because it adds support for
METHOD_PARAMETER
but keeps public API ambiguous, but it may be more safe since it's less likely to introduce potential bugs in the framework downstream, i.e. in already existing Quarkus extensions which explicitly transforms parameter annotations.Please let me know what are your thoughts.
The text was updated successfully, but these errors were encountered: