Skip to content

Commit

Permalink
Add test for inner annotations and fix (#4885)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielshamis authored Jan 10, 2025
1 parent cb5a059 commit dd67fe6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -892,4 +892,50 @@ public class A {
)
);
}

@Test
void addAttributeToNestedAnnotationArray() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Bar",
"attribute",
"",
null,
false)),
java(
"""
package org.example;
public @interface Foo {
Bar[] array() default {};
}
"""
),
java(
"""
package org.example;
public @interface Bar {
String attribute() default "";
}
"""
),
java(
"""
import org.example.Foo;
import org.example.Bar;
@Foo(array = { @Bar() })
public class A {
}
""",
"""
import org.example.Foo;
import org.example.Bar;
@Foo(array = { @Bar(attribute = "") })
public class A {
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public J.Annotation visitAnnotation(J.Annotation a, ExecutionContext ctx) {
J.Annotation original = super.visitAnnotation(a, ctx);
if (!TypeUtils.isOfClassType(a.getType(), annotationType)) {
return a;
return original;
}

String newAttributeValue = maybeQuoteStringArgument(attributeName, attributeValue, a);
Expand Down

0 comments on commit dd67fe6

Please sign in to comment.