Skip to content

Commit

Permalink
Append items to annotation attribute array (#4667)
Browse files Browse the repository at this point in the history
* Add some initial tests

* Add append functionality

* Add markers

* Fix constructor after merge

* Polish code

* Verify default behavior retained for declarative recipes through use of `null` in tests

* Add test that fails due to unexpected second cycle

* Adopt Markers.findFirst

* Add newline to indicate Marker is on assignment

* Fix test and polish

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

Co-authored-by: Tim te Beek <[email protected]>

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
nielsdebruin and timtebeek authored Nov 19, 2024
1 parent f2b9232 commit e206376
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AddOrUpdateAnnotationAttributeTest implements RewriteTest {
@Test
void addValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "hello", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "hello", null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class A {
@Test
void addValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -87,7 +87,7 @@ public class A {
@Test
void addValueAttributeFullyQualifiedClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "java.math.BigDecimal.class", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "java.math.BigDecimal.class", null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -118,7 +118,7 @@ public class A {
@Test
void updateValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "hello", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "hello", null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -150,7 +150,7 @@ public class A {
@Test
void updateValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -182,7 +182,7 @@ public class A {
@Test
void removeValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, null, null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, null, null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -214,7 +214,7 @@ public class A {
@Test
void removeValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, null, null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, null, null, null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -245,7 +245,7 @@ public class A {

@Test
void addNamedAttribute() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null)),
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -282,7 +282,7 @@ void foo() {
@Test
void replaceAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -319,7 +319,7 @@ void foo() {
@Test
void removeAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", null, null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", null, null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -356,7 +356,7 @@ void foo() {
@Test
void preserveExistingAttributes() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -394,7 +394,7 @@ void foo() {

@Test
void implicitValueToExplicitValue() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null)),
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -431,7 +431,7 @@ void foo() {

@Test
void implicitValueToExplicitValueClass() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null)),
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null, null)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -469,7 +469,7 @@ void foo() {
@Test
void dontChangeWhenSetToAddOnly() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", true)),
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", true, false)),
java(
"""
package org.junit;
Expand Down Expand Up @@ -500,6 +500,7 @@ void arrayInAnnotationAttribute() {
"org.example.Foo",
"array",
"newTest",
false,
false)),
java(
"""
Expand Down Expand Up @@ -535,6 +536,7 @@ void arrayInputMoreThanOneInAnnotationAttribute() {
"org.example.Foo",
"array",
"newTest1,newTest2",
false,
false)),
java(
"""
Expand Down Expand Up @@ -570,6 +572,7 @@ void addArrayInputInAnnotationAttribute() {
"org.example.Foo",
"array",
"newTest1,newTest2",
false,
false)),
java(
"""
Expand Down Expand Up @@ -598,14 +601,51 @@ public class A {
);
}

@Test
void addArrayInputInAnnotationAttributeWithAppendTrue() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"newTest1,newTest2",
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] array() default {};
}
"""
),
java(
"""
import org.example.Foo;
@Foo
public class A {
}
""",
"""
import org.example.Foo;
@Foo(array = {"newTest1", "newTest2"})
public class A {
}
"""
)
);
}

@Test
void addArrayInputInAnnotationAttributeEmptyBraces() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"newTest1,newTest2",
false)),
false,
null)),
java(
"""
package org.example;
Expand Down Expand Up @@ -640,7 +680,8 @@ void removeArrayInputInAnnotationAttribute() {
"org.example.Foo",
"array",
null,
null)),
null,
false)),
java(
"""
package org.example;
Expand Down Expand Up @@ -675,7 +716,8 @@ void addOtherAttributeInArrayAnnotation() {
"org.example.Foo",
"string",
"test",
null)),
null,
false)),
java(
"""
package org.example;
Expand Down Expand Up @@ -703,4 +745,151 @@ public class A {
)
);
}

@Test
void appendSingleValueToExistingArrayAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"b",
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] array() default {};
}
"""
),
java(
"""
import org.example.Foo;
@Foo(array = {"a"})
public class A {
}
""",
"""
import org.example.Foo;
@Foo(array = {"a", "b"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"b,c",
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] array() default {};
}
"""
),
java(
"""
import org.example.Foo;
@Foo(array = {"a"})
public class A {
}
""",
"""
import org.example.Foo;
@Foo(array = {"a", "b", "c"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttributeWithOverlap() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"b,c",
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] array() default {};
}
"""
),
java(
"""
import org.example.Foo;
@Foo(array = {"a", "b"})
public class A {
}
""",
"""
import org.example.Foo;
@Foo(array = {"a", "b", "c"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttributeNonSet() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"array",
"b,c",
true,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] array() default {};
}
public class A {
}
"""
),
java(
"""
import org.example.Foo;
@Foo(array = {"a", "b"})
public class A {
}
""",
"""
import org.example.Foo;
@Foo(array = {"a", "b", "b", "c"})
public class A {
}
"""
)
);
}
}
Loading

0 comments on commit e206376

Please sign in to comment.