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 25b78628c07..a340f5453a0 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 @@ -1388,21 +1388,17 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map annotations = leadingAnnotations(annotationPosTable); int saveCursor = cursor; - whitespace(); - 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) - ); - } + Space space = whitespace(); cursor = saveCursor; + return new J.ArrayType( + randomId(), + EMPTY, + Markers.EMPTY, + mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable), + annotations, + source.startsWith("...", cursor) ? JLeftPadded.build(space) : padLeft(sourceBefore("["), sourceBefore("]")), + typeMapping.type(tree) + ); } return baseType; } diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java index 3115386c7d7..dd97f0a5ff6 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/JavaParserTest.java @@ -244,4 +244,71 @@ void filterArtifacts() { assertThat(JavaParser.filterArtifacts("rewrite-java", classpath)) .containsOnly(Paths.get("/.m2/repository/org/openrewrite/rewrite-java/8.41.1/rewrite-java-8.41.1.jar")); } + + @Test + @Issue("https://github.com/openrewrite/rewrite/issues/3881") + void annotatedVargArgs() { + rewriteRun( + java( + """ + import org.jspecify.annotations.NonNull; + + class C1 { + void m(@NonNull String @NonNull[] arrayArgs) { + } + } + """ + ), + java( + """ + import org.jspecify.annotations.NonNull; + + class C2 { + void m(@NonNull String @NonNull ... varArgs) { + } + } + """ + ), + java( + """ + import org.jspecify.annotations.NonNull; + + class C3 { + void m(String @NonNull[] @NonNull[] arrayArgs) { + } + } + """ + ), + java( + """ + import org.jspecify.annotations.NonNull; + + class C4 { + void m(String @NonNull [ ] @NonNull ... s) { + } + } + """ + ), + java( + """ + import org.jspecify.annotations.NonNull; + + class C5 { + void m(@NonNull String @NonNull[] @NonNull[] arrayArgs) { + } + } + """ + ), + java( + """ + import org.jspecify.annotations.NonNull; + + class C6 { + void m(@NonNull String @NonNull [ ] @NonNull ... s) { + } + } + """ + ) + ); + } } 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 f6b8081eca3..4b88bb4e29c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -428,8 +428,8 @@ protected void printStatementTerminator(Statement s, PrintOutputCapture

p) { getCursor() .dropParentUntil( c -> c instanceof Switch || - c instanceof SwitchExpression || - c == Cursor.ROOT_VALUE + c instanceof SwitchExpression || + c == Cursor.ROOT_VALUE ) .getValue(); if (aSwitch instanceof SwitchExpression) { @@ -848,6 +848,13 @@ public J visitVariableDeclarations(VariableDeclarations multiVariable, PrintOutp p.append(']'); } if (multiVariable.getVarargs() != null) { + if (p.out.charAt(p.out.length() - 1) == ']') { + int posToCheck = p.out.length() - 2; + while (p.out.charAt(posToCheck) != '[') { + posToCheck--; + } + p.out.delete(posToCheck, p.out.length()); + } visitSpace(multiVariable.getVarargs(), Space.Location.VARARGS, p); p.append("..."); }