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
Additional field annotation dropped if additional matcher matches the same field #1022
Comments
You are right, those are not additive. It's applied from bottom to top where the first matching phrase wins. This is, because most interceptions are not additive. To overcome this, you have to add a third clause: .field(ElementMatchers.named("property"))
.annotateField(AnnotationDescription.Builder.ofType(First.class).build())
.field(ElementMatchers.fieldType(String.class))
.annotateField(AnnotationDescription.Builder.ofType(Second.class).build())
.field(ElementMatchers.fieldType(String.class).and(ElementMatchers.named("property")))
.annotateField(AnnotationDescription.Builder.ofType(First.class).build())
.annotateField(AnnotationDescription.Builder.ofType(Second.class).build()) I see that this can be annoying, I will consider adding an extension to allow for "fall through" applications similar to the agent builder. |
The problem is that the code adding these two annotations doesn't actually know about each other. It's even living in separate What irritated me the most was the fact that both annotations are added if you don't use any matchers at all. |
What do you mean by the last bit? Can you give an example for not using matchers? |
In the original example, the code adds both annotations if you remove the second matcher. |
Byte Buddy treats every definition as one unit. The problem is, that Byte Buddy does not require those matchers to be combinable. The |
Turns out I already made this a few years back:
Those are additive, interceptions are not. |
Thanks Raphael, I'll try to move that model. Will keep you posted. |
That seems to do the trick. Thanks (again), Raphael! 🙇 |
Assume we want to add annotations to a field and the field is selected by matchers that ultimately end up selecting the same field. In that case, only the latter annotation definition actually ends up in the class file. Here are some sample rules:
property
-> add@First
String
-> add@Second
For a field
String property
this will end up with only@Second
being added. This also fails if the matcher for the second annotation definition is actually the same as the first matcher. Reproducer below:The text was updated successfully, but these errors were encountered: