rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Allow `AddOrUpdateAnnotationAttribute` to add values to an array

Open timtebeek opened this issue 1 year ago • 1 comments

What problem are you trying to solve?

AddOrUpdateAnnotationAttribute can not currently add values to an array. When you try that you get

ClassCastException: class org.openrewrite.java.tree.J$NewArray cannot be cast to class org.openrewrite.java.tree.J$Literal

Describe the solution you'd like

To be able to add a value as seen here

Before

@SpringBootApplication(scanBasePackages = {"com.abc"})

After

@SpringBootApplication(scanBasePackages = {"com.abc", "com.xyz"})

Have you considered any alternatives or workarounds?

We could create a new recipe, or explicitly not allow this. Not clear yet if we should append to existing array, or overwrite existing value.

timtebeek avatar Apr 29 '24 15:04 timtebeek

From further discussion duplicates should be prevented; append when element is not already present. For removal of elements a similar change can be made to the remove attribute from annotation recipe. https://github.com/openrewrite/rewrite/blob/bf4dcfe8a4656f089b8de75a56263f7669950b74/rewrite-java/src/main/java/org/openrewrite/java/RemoveAnnotationAttribute.java#L29

timtebeek avatar Apr 30 '24 09:04 timtebeek