-
Notifications
You must be signed in to change notification settings - Fork 359
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
Fix AddOrUpdateAnnotationAttribute unable to handle FieldAccess #4898
base: main
Are you sure you want to change the base?
Conversation
Hi @SiBorea I also stumbled across this a few hours ago, thanks for the fix. If you have any questions regarding my comments, feel free to ask. :) |
@@ -893,6 +892,47 @@ public class A { | |||
); | |||
} | |||
|
|||
@Test | |||
void updateFieldAccessAttribute() { | |||
rewriteRun( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you try to apply the recipe to change the value from hello
to Cost.HI
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The attributeValue
of the recipe is of type String, don't think we can change the value from hello
to Const.HI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it a little bit, the result shows that it can only change the value to "Const.HI"
, not Const.HI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be possible. The attributeValue
's value is interpreted as whatever it is. Did you try the FQN Const.HI
?
If you don't mind, I would have a look myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, please go ahead.
rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
Outdated
Show resolved
Hide resolved
} else if (exp instanceof J.FieldAccess) { | ||
as = JavaTemplate.builder("#{} = #{}") | ||
.build() | ||
.apply(new Cursor(getCursor(), as), as.getCoordinates().replace(), var.getSimpleName(), newAttributeValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think you need a new Cursor
here? getCursor()
should do the trick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using getCursor()
here will return a J.Annotation
object, rather than J.FieldAccess
we need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A I see, thats right. Within this recipe the situation is solved by generating an new Annotation with one Assigment and then call get(0)
, see
rewrite/rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
Line 239 in ae8acf4
return ((J.Annotation) JavaTemplate.apply(newAttributeValue, getCursor(), finalA.getCoordinates().replaceArguments())) |
rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
Outdated
Show resolved
Hide resolved
# Conflicts: # rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
What's changed?
Checklist