Skip to content

Commit dd67fe6

Browse files
authored
Add test for inner annotations and fix (#4885)
1 parent cb5a059 commit dd67fe6

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

rewrite-java-test/src/test/java/org/openrewrite/java/AddOrUpdateAnnotationAttributeTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,4 +892,50 @@ public class A {
892892
)
893893
);
894894
}
895+
896+
@Test
897+
void addAttributeToNestedAnnotationArray() {
898+
rewriteRun(
899+
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
900+
"org.example.Bar",
901+
"attribute",
902+
"",
903+
null,
904+
false)),
905+
java(
906+
"""
907+
package org.example;
908+
public @interface Foo {
909+
Bar[] array() default {};
910+
}
911+
"""
912+
),
913+
java(
914+
"""
915+
package org.example;
916+
public @interface Bar {
917+
String attribute() default "";
918+
}
919+
"""
920+
),
921+
java(
922+
"""
923+
import org.example.Foo;
924+
import org.example.Bar;
925+
926+
@Foo(array = { @Bar() })
927+
public class A {
928+
}
929+
""",
930+
"""
931+
import org.example.Foo;
932+
import org.example.Bar;
933+
934+
@Foo(array = { @Bar(attribute = "") })
935+
public class A {
936+
}
937+
"""
938+
)
939+
);
940+
}
895941
}

rewrite-java/src/main/java/org/openrewrite/java/AddOrUpdateAnnotationAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8787
public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
8888
J.Annotation original = super.visitAnnotation(a, ctx);
8989
if (!TypeUtils.isOfClassType(a.getType(), annotationType)) {
90-
return a;
90+
return original;
9191
}
9292

9393
String newAttributeValue = maybeQuoteStringArgument(attributeName, attributeValue, a);

0 commit comments

Comments
 (0)