Skip to content

Fix AddOrUpdateAnnotation recipe changing implicit value into explicit value in annotations with arrays #5693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,43 @@ public class A {
);
}

@Test
void arrayInAnnotationValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"newTest",
null,
false,
false)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"oldTest"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"newTest"})
public class A {
}
"""
)
);
}

@Test
void arrayInputMoreThanOneInAnnotationAttribute() {
rewriteRun(
Expand Down Expand Up @@ -573,6 +610,43 @@ public class A {
);
}

@Test
void arrayInputMoreThanOneInAnnotationValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"newTest1,newTest2",
null,
false,
false)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"oldTest"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"newTest1", "newTest2"})
public class A {
}
"""
)
);
}

@Test
void addArrayInputInAnnotationAttribute() {
rewriteRun(
Expand Down Expand Up @@ -759,6 +833,74 @@ public class A {
);
}

@Test
void addOtherAttributeInArrayValueAnnotation() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
"string",
"test",
null,
null,
false)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
String string() default "";
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"newTest1", "newTest2"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo(string = "test", value = {"newTest1", "newTest2"})
public class A {
}
"""
)
);
}

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

@Foo({"a"})
public class A {
}
"""
)
);
}

@Test
void appendSingleValueToExistingArrayAttribute() {
rewriteRun(
Expand Down Expand Up @@ -796,6 +938,43 @@ public class A {
);
}

@Test
void appendSingleValueToExistingArrayValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"b",
null,
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"a"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"a", "b"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttribute() {
rewriteRun(
Expand Down Expand Up @@ -833,6 +1012,43 @@ public class A {
);
}

@Test
void appendMultipleValuesToExistingArrayValueAttribute() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"b,c",
null,
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"a"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"a", "b", "c"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttributeWithOverlap() {
rewriteRun(
Expand Down Expand Up @@ -870,6 +1086,43 @@ public class A {
);
}

@Test
void appendMultipleValuesToExistingArrayValueAttributeWithOverlap() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"b,c",
null,
false,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"a", "b"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"a", "b", "c"})
public class A {
}
"""
)
);
}

@Test
void appendMultipleValuesToExistingArrayAttributeNonSet() {
rewriteRun(
Expand Down Expand Up @@ -910,6 +1163,46 @@ public class A {
);
}

@Test
void appendMultipleValuesToExistingArrayValueAttributeNonSet() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute(
"org.example.Foo",
null,
"b,c",
null,
true,
true)),
java(
"""
package org.example;
public @interface Foo {
String[] value() default {};
}

public class A {
}
"""
),
java(
"""
import org.example.Foo;

@Foo({"a", "b"})
public class A {
}
""",
"""
import org.example.Foo;

@Foo({"a", "b", "b", "c"})
public class A {
}
"""
)
);
}

@Test
void updateFieldAccessAttribute() {
rewriteRun(
Expand Down
Loading