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

Fix AddOrUpdateAnnotationAttribute unable to handle FieldAccess #4898

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

SiBorea
Copy link
Contributor

@SiBorea SiBorea commented Jan 14, 2025

What's changed?

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

# Conflicts:
#	rewrite-java-test/src/test/java/org/openrewrite/java/AddOrUpdateAnnotationAttributeTest.java
@timtebeek timtebeek requested a review from MBoegers January 14, 2025 06:34
@MBoegers
Copy link
Contributor

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(
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, please go ahead.

} else if (exp instanceof J.FieldAccess) {
as = JavaTemplate.builder("#{} = #{}")
.build()
.apply(new Cursor(getCursor(), as), as.getCoordinates().replace(), var.getSimpleName(), newAttributeValue);
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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

return ((J.Annotation) JavaTemplate.apply(newAttributeValue, getCursor(), finalA.getCoordinates().replaceArguments()))

MBoegers and others added 3 commits January 15, 2025 09:50
# Conflicts:
#	rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

AddOrUpdateAnnotationAttribute unable to handle @Anno(key = ClassB.staticVar)
2 participants