Skip to content

Commit

Permalink
Fix AddOrUpdateAnnotationAttribute unable to handle FieldAccess (#4898)
Browse files Browse the repository at this point in the history
* Fix AddOrUpdateAnnotationAttribute unable to handle FieldAccess

(cherry picked from commit 7d7451a)

* Fix AddOrUpdateAnnotationAttribute unable to handle FieldAccess

(cherry picked from commit 7d7451a)

* Fix omitting addOnly and code style

* Use getCursor() for JavaTemplate

---------

Co-authored-by: Merlin Bögershausen <[email protected]>
  • Loading branch information
SiBorea and MBoegers authored Jan 16, 2025
1 parent 68f86ee commit 1fd511c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,47 @@ public class A {
);
}

@Test
void updateFieldAccessAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", "value", "hello", null, false, null)),
java(
"""
package org.example;
public class Const {
public static final String HI = "hi";
}
"""),
java(
"""
package org.example;
public @interface Foo {
String value() default "";
}
"""
),
java(
"""
import org.example.Foo;
import org.example.Const;
@Foo(value = Const.HI)
public class A {
}
""",
"""
import org.example.Foo;
import org.example.Const;
@Foo(value = "hello")
public class A {
}
"""
)
);
}

@Test
void addAttributeToNestedAnnotationArray() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markers;

Expand Down Expand Up @@ -208,14 +211,24 @@ public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {

return as.withAssignment(((J.NewArray) as.getAssignment()).withInitializer(jLiteralList));
} else {
J.Literal value = (J.Literal) as.getAssignment();
if (newAttributeValue.equals(value.getValueSource()) || Boolean.TRUE.equals(addOnly)) {
return it;
}
if (!valueMatches(value, oldAttributeValue)) {
return it;
Expression exp = as.getAssignment();
if (exp instanceof J.Literal) {
J.Literal value = (J.Literal) exp;
if (newAttributeValue.equals(value.getValueSource()) || Boolean.TRUE.equals(addOnly)) {
return it;
}
if (!valueMatches(value, oldAttributeValue)) {
return it;
}
return as.withAssignment(value.withValue(newAttributeValue).withValueSource(newAttributeValue));
} else if (exp instanceof J.FieldAccess) {
if (Boolean.TRUE.equals(addOnly)) {
return it;
}
int index = finalA.getArguments().indexOf(as);
as = (J.Assignment) ((J.Annotation) JavaTemplate.apply("#{} = #{}", getCursor(), as.getCoordinates().replace(), var.getSimpleName(), newAttributeValue)).getArguments().get(index);
return as;
}
return as.withAssignment(value.withValue(newAttributeValue).withValueSource(newAttributeValue));
}
} else if (it instanceof J.Literal) {
// The only way anything except an assignment can appear is if there's an implicit assignment to "value"
Expand Down

0 comments on commit 1fd511c

Please sign in to comment.