|
34 | 34 | import static java.util.Collections.*; |
35 | 35 | import static java.util.Objects.requireNonNull; |
36 | 36 | import static java.util.stream.Collectors.joining; |
| 37 | +import static java.util.stream.Collectors.toList; |
37 | 38 | import static org.openrewrite.Tree.randomId; |
38 | 39 | import static org.openrewrite.java.tree.Space.SINGLE_SPACE; |
39 | 40 | import static org.openrewrite.marker.Markers.EMPTY; |
@@ -171,6 +172,16 @@ public J.Annotation visitAnnotation(J.Annotation original, ExecutionContext ctx) |
171 | 172 | if (!valueMatches(exp, oldAttributeValue) || newAttributeValue.equals(((J.Literal) exp).getValueSource())) { |
172 | 173 | return as; |
173 | 174 | } |
| 175 | + if (TRUE.equals(appendArray) && attributeIsArray(annotation)) { |
| 176 | + List<Expression> updatedList = updateInitializer(annotation, singletonList(as.getAssignment()), getAttributeValues()); |
| 177 | + Expression flattenedList = createAnnotationLiteralFromString( |
| 178 | + annotation, |
| 179 | + wrapValues(updatedList.stream() |
| 180 | + .map(e -> ((J.Literal) e).getValueSource()) |
| 181 | + .collect(toList()), true) |
| 182 | + ); |
| 183 | + return as.withAssignment(flattenedList); |
| 184 | + } |
174 | 185 | return as.withAssignment(createAnnotationLiteral(annotation, newAttributeValue)); |
175 | 186 | } |
176 | 187 | if (exp instanceof J.FieldAccess) { |
@@ -246,6 +257,12 @@ private Expression createAnnotationLiteral(J.Annotation annotation, String newAt |
246 | 257 | .getArguments().get(0); |
247 | 258 | } |
248 | 259 |
|
| 260 | + private Expression createAnnotationLiteralFromString(J.Annotation annotation, String updatedAttributeValue) { |
| 261 | + //noinspection ConstantConditions |
| 262 | + return JavaTemplate.<J.Annotation>apply("#{}", getCursor(), annotation.getCoordinates().replaceArguments(), updatedAttributeValue) |
| 263 | + .getArguments().get(0); |
| 264 | + } |
| 265 | + |
249 | 266 | private J.Assignment createAnnotationAssignment(J.Annotation annotation, String name, @Nullable Object parameter) { |
250 | 267 | //noinspection ConstantConditions |
251 | 268 | return (J.Assignment) JavaTemplate.<J.Annotation>apply(name + " = " + (parameter instanceof J ? "#{any()}" : "#{}"), getCursor(), annotation.getCoordinates().replaceArguments(), parameter) |
@@ -324,7 +341,14 @@ private List<String> getAttributeValues() { |
324 | 341 | } |
325 | 342 |
|
326 | 343 | private String getAttributeValuesAsString() { |
327 | | - return getAttributeValues().stream().map(String::valueOf).collect(joining("\", \"", "{\"", "\"}")); |
| 344 | + return wrapValues(getAttributeValues(), false); |
| 345 | + } |
| 346 | + |
| 347 | + private String wrapValues(List<@Nullable String> values, boolean quoteless) { |
| 348 | + if (quoteless) { |
| 349 | + return values.stream().map(String::valueOf).collect(joining(", ", "{", "}")); |
| 350 | + } |
| 351 | + return values.stream().map(String::valueOf).collect(joining("\", \"", "{\"", "\"}")); |
328 | 352 | } |
329 | 353 |
|
330 | 354 | private static boolean isAnnotationWithOnlyValueMethod(J.Annotation annotation) { |
|
0 commit comments