Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package org.openrewrite.java;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.Recipe;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -161,10 +163,10 @@ public record Person(
}

@Issue("https://github.com/openrewrite/rewrite/issues/5712")
@Test
void replaceArgumentsInNestedAnnotation() {
@Nested
class NestedAnnotations {
@Language("java")
String annotations = """
private final String annotations = """
package foo;

import java.lang.annotation.*;
Expand All @@ -179,55 +181,313 @@ void replaceArgumentsInNestedAnnotation() {
NestedAnnotation[] value();
}
""";
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
if (annotation.getSimpleName().equals("NestedAnnotation") &&
!annotation.getArguments().isEmpty()) {
// Check if this annotation still has the 'a' attribute that needs to be replaced
J.Assignment arg = (J.Assignment) annotation.getArguments().get(0);
if (arg.getVariable() instanceof J.Identifier &&
((J.Identifier) arg.getVariable()).getSimpleName().equals("a")) {
// Only apply the template if we haven't already transformed this annotation
J.Literal value = (J.Literal) arg.getAssignment();

// Replace 'a' with 'b' in the annotation
return JavaTemplate.builder("@NestedAnnotation(b = #{any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().dependsOn(annotations))
.imports("foo.*")
.build()
.apply(getCursor(), annotation.getCoordinates().replace(), value);
}

private final Recipe recipe = toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) {
if (annotation.getSimpleName().equals("NestedAnnotation") &&
!annotation.getArguments().isEmpty()) {
// Check if this annotation still has the 'a' attribute that needs to be replaced
J.Assignment arg = (J.Assignment) annotation.getArguments().get(0);
if (arg.getVariable() instanceof J.Identifier &&
((J.Identifier) arg.getVariable()).getSimpleName().equals("a")) {
// Only apply the template if we haven't already transformed this annotation
J.Literal value = (J.Literal) arg.getAssignment();

// Replace 'a' with 'b' in the annotation
return JavaTemplate.builder("@NestedAnnotation(b = #{any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().dependsOn(annotations))
.imports("foo.*")
.build()
.apply(getCursor(), annotation.getCoordinates().replace(), value);
}
return super.visitAnnotation(annotation, ctx);
}
})),
java(
"""
import foo.*;
return super.visitAnnotation(annotation, ctx);
}
});

@NestedAnnotations({
@NestedAnnotation(a = "1"),
@NestedAnnotation(a = "2")
})
class Test {
}
""",
"""
import foo.*;
@Test
void replaceWhenFieldAnnotatedNoIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
String field;
void method() {}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
String field;
void method() {}
}
"""
)
);
}

@NestedAnnotations({
@NestedAnnotation(b = "1"),
@NestedAnnotation(b = "2")
})
class Test {
}
"""
)
);
}
@Test
void replaceWhenFieldAnnotatedWithIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
String field;
void method() {}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
String field;
void method() {}
}
"""
)
);
}

@Test
void replaceWhenMethodAnnotatedNoIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
void method() {}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
void method() {}
}
"""
)
);
}

@Test
void replaceWhenMethodAnnotatedWithIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
void method() {}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
void method() {}
}
"""
)
);
}

@Test
void replaceWhenOuterClassAnnotatedNoIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

@NestedAnnotations({
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
class A {
void method() {}
}
""",
"""
import foo.*;

@NestedAnnotations({
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
class A {
void method() {}
}
"""
)
);
}

@Test
void replaceWhenOuterClassAnnotatedWithIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

@NestedAnnotations(value = {
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
class A {
void method() {}
}
""",
"""
import foo.*;

@NestedAnnotations(value = {
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
class A {
void method() {}
}
"""
)
);
}

@Test
void replaceWhenInnerClassAnnotatedNoIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
class B {
void method() {}
}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations({
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
class B {
void method() {}
}
}
"""
)
);
}

@Test
void replaceWhenInnerClassAnnotatedWithIdentifier() {
rewriteRun(
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(annotations))
.recipe(recipe),
//language=java
java(
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(a = "first"),
@NestedAnnotation(a = "second")
})
class B {
void method() {}
}
}
""",
"""
import foo.*;

class A {
@NestedAnnotations(value = {
@NestedAnnotation(b = "first"),
@NestedAnnotation(b = "second")
})
class B {
void method() {}
}
}
"""
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String template(Cursor cursor, String template) {
J annotationParent = j instanceof J.Annotation && cursor.getParent() != null ? cursor.getParent().firstEnclosing(J.class) : null;

int level = 1;
while (annotationParent instanceof J.NewArray || annotationParent instanceof J.Annotation) {
while (annotationParent instanceof J.NewArray || annotationParent instanceof J.Assignment || annotationParent instanceof J.Annotation) {
level += 1;
if (cursor.getParent(level) == null) {
break;
Expand Down