From a5184e62f61bece5c261a910bb74c148ab14ebfd Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Wed, 3 Jan 2024 14:07:53 -0700 Subject: [PATCH] Added support for annotated varargs. --- .../ReloadableJava11JavadocVisitor.java | 13 +++-- .../ReloadableJava11ParserVisitor.java | 46 ++++++++++++------ .../ReloadableJava17JavadocVisitor.java | 13 +++-- .../ReloadableJava17ParserVisitor.java | 46 ++++++++++++------ .../ReloadableJava21JavadocVisitor.java | 13 +++-- .../ReloadableJava21ParserVisitor.java | 46 ++++++++++++------ .../java/ReloadableJava8JavadocVisitor.java | 13 +++-- .../java/ReloadableJava8ParserVisitor.java | 47 +++++++++++++------ .../openrewrite/java/tree/AnnotationTest.java | 27 +++++++++++ .../org/openrewrite/java/JavaPrinter.java | 17 ++++--- .../org/openrewrite/java/JavadocPrinter.java | 3 +- .../openrewrite/java/marker/OmitBrackets.java | 28 +++++++++++ 12 files changed, 227 insertions(+), 85 deletions(-) create mode 100644 rewrite-java/src/main/java/org/openrewrite/java/marker/OmitBrackets.java diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java index 75e1e9f1202..cd35b3e43f3 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11JavadocVisitor.java @@ -33,6 +33,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.LeadingBrace; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1154,18 +1155,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) { int saveCursor = cursor; Space before = whitespace(); JLeftPadded dimension; - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY); + markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId())); + } else { cursor++; dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before); - } else { - cursor = saveCursor; - return elemType; } return new J.ArrayType( randomId(), fmt, - Markers.EMPTY, + markers, elemType, null, dimension, diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java index 304ffbc46c5..3ea8ebe444e 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11ParserVisitor.java @@ -34,6 +34,7 @@ import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaParsingException; import org.openrewrite.java.internal.JavaTypeCache; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1281,11 +1282,22 @@ private TypeTree arrayTypeTree(Tree tree, Map annotationP } Space prefix = whitespace(); + Markers markers = Markers.EMPTY; TypeTree elemType = convert(typeIdent); List annotations = leadingAnnotations(annotationPosTable); - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); + JLeftPadded dimension; + int saveCursor = cursor; + whitespace(); + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); + } assert arrayTypeTree != null; - return new J.ArrayType(randomId(), prefix, Markers.EMPTY, + return new J.ArrayType(randomId(), prefix, markers, count == 1 ? elemType : mapDimensions(elemType, arrayTypeTree.getType(), annotationPosTable), annotations, dimension, @@ -1303,20 +1315,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map annotations = leadingAnnotations(annotationPosTable); int saveCursor = cursor; whitespace(); - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + JLeftPadded dimension; + if (source.startsWith("...", cursor)) { cursor = saveCursor; - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); - return new J.ArrayType( - randomId(), - EMPTY, - Markers.EMPTY, - mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), - annotations, - dimension, - typeMapping.type(tree) - ); + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); } - cursor = saveCursor; + + return new J.ArrayType( + randomId(), + EMPTY, + markers, + mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), + annotations, + dimension, + typeMapping.type(tree) + ); } return baseType; } diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java index b4ec5d3c105..9080cd8d3ed 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17JavadocVisitor.java @@ -36,6 +36,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.LeadingBrace; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1159,18 +1160,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) { int saveCursor = cursor; Space before = whitespace(); JLeftPadded dimension; - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY); + markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId())); + } else { cursor++; dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before); - } else { - cursor = saveCursor; - return elemType; } return new J.ArrayType( randomId(), fmt, - Markers.EMPTY, + markers, elemType, null, dimension, diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java index 50c0829c2ef..afb56502329 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java @@ -35,6 +35,7 @@ import org.openrewrite.java.JavaParsingException; import org.openrewrite.java.internal.JavaTypeCache; import org.openrewrite.java.marker.CompactConstructor; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1358,11 +1359,22 @@ private TypeTree arrayTypeTree(Tree tree, Map annotationP } Space prefix = whitespace(); + Markers markers = Markers.EMPTY; TypeTree elemType = convert(typeIdent); List annotations = leadingAnnotations(annotationPosTable); - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); + JLeftPadded dimension; + int saveCursor = cursor; + whitespace(); + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); + } assert arrayTypeTree != null; - return new J.ArrayType(randomId(), prefix, Markers.EMPTY, + return new J.ArrayType(randomId(), prefix, markers, count == 1 ? elemType : mapDimensions(elemType, arrayTypeTree.getType(), annotationPosTable), annotations, dimension, @@ -1380,20 +1392,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map annotations = leadingAnnotations(annotationPosTable); int saveCursor = cursor; whitespace(); - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + JLeftPadded dimension; + if (source.startsWith("...", cursor)) { cursor = saveCursor; - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); - return new J.ArrayType( - randomId(), - EMPTY, - Markers.EMPTY, - mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), - annotations, - dimension, - typeMapping.type(tree) - ); + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); } - cursor = saveCursor; + + return new J.ArrayType( + randomId(), + EMPTY, + markers, + mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), + annotations, + dimension, + typeMapping.type(tree) + ); } return baseType; } diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java index b6ed208cd32..8d372b52471 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21JavadocVisitor.java @@ -36,6 +36,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.LeadingBrace; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1158,18 +1159,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) { int saveCursor = cursor; Space before = whitespace(); JLeftPadded dimension; - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY); + markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId())); + } else { cursor++; dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before); - } else { - cursor = saveCursor; - return elemType; } return new J.ArrayType( randomId(), fmt, - Markers.EMPTY, + markers, elemType, null, dimension, diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java index 10b90522da6..792e6874a33 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java @@ -35,6 +35,7 @@ import org.openrewrite.java.JavaParsingException; import org.openrewrite.java.internal.JavaTypeCache; import org.openrewrite.java.marker.CompactConstructor; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1356,11 +1357,22 @@ private TypeTree arrayTypeTree(Tree tree, Map annotationP } Space prefix = whitespace(); + Markers markers = Markers.EMPTY; TypeTree elemType = convert(typeIdent); List annotations = leadingAnnotations(annotationPosTable); - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); + JLeftPadded dimension; + int saveCursor = cursor; + whitespace(); + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); + } assert arrayTypeTree != null; - return new J.ArrayType(randomId(), prefix, Markers.EMPTY, + return new J.ArrayType(randomId(), prefix, markers, count == 1 ? elemType : mapDimensions(elemType, arrayTypeTree.getType(), annotationPosTable), annotations, dimension, @@ -1378,20 +1390,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map annotations = leadingAnnotations(annotationPosTable); int saveCursor = cursor; whitespace(); - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + JLeftPadded dimension; + if (source.startsWith("...", cursor)) { cursor = saveCursor; - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); - return new J.ArrayType( - randomId(), - EMPTY, - Markers.EMPTY, - mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), - annotations, - dimension, - typeMapping.type(tree) - ); + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); } - cursor = saveCursor; + + return new J.ArrayType( + randomId(), + EMPTY, + markers, + mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), + annotations, + dimension, + typeMapping.type(tree) + ); } return baseType; } diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java index ebcc3d0f712..f92e26c0093 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8JavadocVisitor.java @@ -33,6 +33,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.LeadingBrace; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1075,18 +1076,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) { int saveCursor = cursor; Space before = whitespace(); JLeftPadded dimension; - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY); + markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId())); + } else { cursor++; dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before); - } else { - cursor = saveCursor; - return elemType; } return new J.ArrayType( randomId(), fmt, - Markers.EMPTY, + markers, elemType, null, dimension, diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java index e2ee4fa3f0c..5a445cc375e 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8ParserVisitor.java @@ -32,6 +32,7 @@ import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.internal.JavaTypeCache; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -1261,7 +1262,6 @@ private TypeTree arrayTypeTree(Tree tree, Map annotationP JCArrayTypeTree arrayTypeTree = null; while (typeIdent instanceof JCAnnotatedType || typeIdent instanceof JCArrayTypeTree) { if (typeIdent instanceof JCAnnotatedType) { - mapAnnotations(((JCAnnotatedType) typeIdent).getAnnotations(), annotationPosTable); typeIdent = ((JCAnnotatedType) typeIdent).getUnderlyingType(); } if (typeIdent instanceof JCArrayTypeTree) { @@ -1274,11 +1274,22 @@ private TypeTree arrayTypeTree(Tree tree, Map annotationP } Space prefix = whitespace(); + Markers markers = Markers.EMPTY; TypeTree elemType = convert(typeIdent); List annotations = leadingAnnotations(annotationPosTable); - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); + JLeftPadded dimension; + int saveCursor = cursor; + whitespace(); + if (source.startsWith("...", cursor)) { + cursor = saveCursor; + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); + } assert arrayTypeTree != null; - return new J.ArrayType(randomId(), prefix, Markers.EMPTY, + return new J.ArrayType(randomId(), prefix, markers, count == 1 ? elemType : mapDimensions(elemType, arrayTypeTree.getType(), annotationPosTable), annotations, dimension, @@ -1296,20 +1307,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map annotations = leadingAnnotations(annotationPosTable); int saveCursor = cursor; whitespace(); - if (source.startsWith("[", cursor)) { + Markers markers = Markers.EMPTY; + JLeftPadded dimension; + if (source.startsWith("...", cursor)) { cursor = saveCursor; - JLeftPadded dimension = padLeft(sourceBefore("["), sourceBefore("]")); - return new J.ArrayType( - randomId(), - EMPTY, - Markers.EMPTY, - mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), - annotations, - dimension, - typeMapping.type(tree) - ); + markers = markers.addIfAbsent(new OmitBrackets(randomId())); + dimension = padLeft(EMPTY, EMPTY); + } else { + cursor = saveCursor; + dimension = padLeft(sourceBefore("["), sourceBefore("]")); } - cursor = saveCursor; + + return new J.ArrayType( + randomId(), + EMPTY, + markers, + mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), + annotations, + dimension, + typeMapping.type(tree) + ); } return baseType; } diff --git a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/AnnotationTest.java b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/AnnotationTest.java index 17855dffbfd..c72358beb45 100644 --- a/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/AnnotationTest.java +++ b/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/AnnotationTest.java @@ -16,6 +16,8 @@ package org.openrewrite.java.tree; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; import org.openrewrite.Issue; @@ -397,4 +399,29 @@ public J.ArrayType visitArrayType(J.ArrayType arrayType, Object o) { ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/3881") + @ParameterizedTest + @ValueSource(strings = { + "", + "[]" + }) + void annotatedVararg(String input) { + rewriteRun( + java( + String.format(""" + import java.lang.annotation.ElementType; + import java.lang.annotation.Target; + + class TypeAnnotationTest { + void method(Integer @A1 %s ... integers) {} + + @Target(ElementType.TYPE_USE) + private @interface A1 { + } + } + """, input) + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java index f74712242d3..60ef1a7840b 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -19,6 +19,7 @@ import org.openrewrite.PrintOutputCapture; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.CompactConstructor; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.marker.OmitParentheses; import org.openrewrite.java.tree.*; import org.openrewrite.java.tree.J.*; @@ -181,7 +182,7 @@ public J visitArrayType(ArrayType arrayType, PrintOutputCapture

p) { } visit(type, p); visit(arrayType.getAnnotations(), p); - if (arrayType.getDimension() != null) { + if (arrayType.getDimension() != null && !arrayType.getMarkers().findFirst(OmitBrackets.class).isPresent()) { visitSpace(arrayType.getDimension().getBefore(), Space.Location.DIMENSION_PREFIX, p); p.append('['); visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p); @@ -198,12 +199,14 @@ public J visitArrayType(ArrayType arrayType, PrintOutputCapture

p) { private void printDimensions(J.ArrayType arrayType, PrintOutputCapture

p) { beforeSyntax(arrayType, Space.Location.ARRAY_TYPE_PREFIX, p); visit(arrayType.getAnnotations(), p); - visitSpace(arrayType.getDimension().getBefore(), Space.Location.DIMENSION_PREFIX, p); - p.append('['); - visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p); - p.append(']'); - if (arrayType.getElementType() instanceof J.ArrayType) { - printDimensions((ArrayType) arrayType.getElementType(), p); + if (arrayType.getDimension() != null && !arrayType.getMarkers().findFirst(OmitBrackets.class).isPresent()) { + visitSpace(arrayType.getDimension().getBefore(), Space.Location.DIMENSION_PREFIX, p); + p.append('['); + visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p); + p.append(']'); + if (arrayType.getElementType() instanceof J.ArrayType) { + printDimensions((ArrayType) arrayType.getElementType(), p); + } } afterSyntax(arrayType, p); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java index 2bfe7daa235..10b746c6956 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavadocPrinter.java @@ -19,6 +19,7 @@ import org.openrewrite.PrintOutputCapture; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.marker.LeadingBrace; +import org.openrewrite.java.marker.OmitBrackets; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Marker; import org.openrewrite.marker.Markers; @@ -409,7 +410,7 @@ public J visitArrayType(J.ArrayType arrayType, PrintOutputCapture

p) { beforeSyntax(arrayType, Space.Location.ARRAY_TYPE_PREFIX, p); visit(arrayType.getElementType(), p); visit(arrayType.getAnnotations(), p); - if (arrayType.getDimension() != null) { + if (arrayType.getDimension() != null && !arrayType.getMarkers().findFirst(OmitBrackets.class).isPresent()) { visitSpace(arrayType.getDimension().getBefore(), Space.Location.DIMENSION_PREFIX, p); p.append('['); visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/marker/OmitBrackets.java b/rewrite-java/src/main/java/org/openrewrite/java/marker/OmitBrackets.java new file mode 100644 index 00000000000..f352afa6936 --- /dev/null +++ b/rewrite-java/src/main/java/org/openrewrite/java/marker/OmitBrackets.java @@ -0,0 +1,28 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.marker; + +import lombok.Value; +import lombok.With; +import org.openrewrite.marker.Marker; + +import java.util.UUID; + +@Value +@With +public class OmitBrackets implements Marker { + UUID id; +}