Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1506,9 +1506,22 @@ private TypeTree arrayTypeTree(Tree tree, Map<Integer, JCAnnotation> annotationP
Space prefix = whitespace();
TypeTree elemType = convert(typeIdent);
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));

// Check if this is varargs (...) or regular array brackets ([])
Markers markers = Markers.EMPTY;
JLeftPadded<Space> dimension;
int nextNonWhitespace = indexOfNextNonWhitespace(cursor, source);
if (source.startsWith("...", nextNonWhitespace)) {
// Varargs syntax
markers = markers.addIfAbsent(new org.openrewrite.java.marker.Varargs(randomId()));
dimension = padLeft(sourceBefore("..."), EMPTY);
} else {
// Regular array brackets
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,
Expand All @@ -1524,22 +1537,32 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno

if (typeIdent instanceof JCArrayTypeTree) {
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
int saveCursor = cursor;
whitespace();
if (source.startsWith("[", cursor)) {
cursor = saveCursor;
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));
return new J.ArrayType(
randomId(),
EMPTY,
Markers.EMPTY,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
dimension,
typeMapping.type(tree)
);

// Check if this is varargs (...) or regular array brackets ([])
Markers markers = Markers.EMPTY;
JLeftPadded<Space> dimension;
int nextNonWhitespace = indexOfNextNonWhitespace(cursor, source);
if (source.startsWith("...", nextNonWhitespace)) {
// Varargs syntax
markers = markers.addIfAbsent(new org.openrewrite.java.marker.Varargs(randomId()));
dimension = padLeft(sourceBefore("..."), EMPTY);
} else if (source.startsWith("[", nextNonWhitespace)) {
// Regular array brackets
dimension = padLeft(sourceBefore("["), sourceBefore("]"));
} else {
// No dimension found
return baseType;
}
cursor = saveCursor;

return new J.ArrayType(
randomId(),
EMPTY,
markers,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
dimension,
typeMapping.type(tree)
);
}
return baseType;
}
Expand Down Expand Up @@ -1720,7 +1743,7 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
typeExpr = idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@') ? convert(vartype) :
typeExpr = idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@' || source.startsWith("...", idx)) ? convert(vartype) :
// we'll capture the array dimensions in a bit, just convert the element type
convert(elementType);
} else {
Expand All @@ -1736,15 +1759,6 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
typeExpr = new J.AnnotatedType(randomId(), prefix, Markers.EMPTY, ListUtils.mapFirst(typeExprAnnotations, a -> a.withPrefix(EMPTY)), typeExpr);
}

Space varargs = null;
if (typeExpr != null && typeExpr.getMarkers().findFirst(JavaVarKeyword.class).isEmpty()) {
int varargStart = indexOfNextNonWhitespace(cursor, source);
if (source.startsWith("...", varargStart)) {
varargs = format(source, cursor, varargStart);
cursor = varargStart + 3;
}
}

List<JRightPadded<J.VariableDeclarations.NamedVariable>> vars = new ArrayList<>(nodes.size());
for (int i = 0; i < nodes.size(); i++) {
JCVariableDecl n = (JCVariableDecl) nodes.get(i);
Expand All @@ -1770,7 +1784,7 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
);
}

return new J.VariableDeclarations(randomId(), fmt, Markers.EMPTY, modifierResults.getLeadingAnnotations(), modifierResults.getModifiers(), typeExpr, varargs, vars);
return new J.VariableDeclarations(randomId(), fmt, Markers.EMPTY, modifierResults.getLeadingAnnotations(), modifierResults.getModifiers(), typeExpr, null, vars);
}

private List<JLeftPadded<Space>> arrayDimensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,64 @@ class A {
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/3881")
@Test
void annotatedVarargs() {
rewriteRun(
java(
"""
package com.example;

import org.jspecify.annotations.NonNull;

class Test {
void method(@NonNull String @NonNull ... args) {
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/3881")
@Test
void annotatedVarargsFinal() {
rewriteRun(
java(
"""
package com.example;

import org.jspecify.annotations.NonNull;

class Test {
private String method(@NonNull final String @NonNull... args) {
return "";
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/3881")
@Test
void multipleParametersWithAnnotatedVarargs() {
rewriteRun(
java(
"""
package com.example;

import org.jspecify.annotations.NonNull;

class Test {
public static int resolvePoolSize(@NonNull String propertyName, @NonNull String value,
@NonNull String @NonNull... magicValues) {
return 0;
}
}
"""
)
);
}
}
25 changes: 19 additions & 6 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.marker.CompactConstructor;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.marker.Varargs;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.tree.J.*;
import org.openrewrite.marker.Marker;
Expand Down Expand Up @@ -210,9 +211,15 @@ public J visitArrayType(ArrayType arrayType, PrintOutputCapture<P> p) {
visit(arrayType.getAnnotations(), p);
if (arrayType.getDimension() != null) {
visitSpace(arrayType.getDimension().getBefore(), Space.Location.DIMENSION_PREFIX, p);
p.append('[');
visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p);
p.append(']');
if (arrayType.getMarkers().findFirst(Varargs.class).isPresent()) {
// Print varargs syntax
p.append("...");
} else {
// Print regular array brackets
p.append('[');
visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p);
p.append(']');
}

if (arrayType.getElementType() instanceof J.ArrayType) {
printDimensions((ArrayType) arrayType.getElementType(), p);
Expand All @@ -226,9 +233,15 @@ private void printDimensions(J.ArrayType arrayType, PrintOutputCapture<P> 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.getMarkers().findFirst(Varargs.class).isPresent()) {
// Print varargs syntax
p.append("...");
} else {
// Print regular array brackets
p.append('[');
visitSpace(arrayType.getDimension().getElement(), Space.Location.DIMENSION, p);
p.append(']');
}
if (arrayType.getElementType() instanceof J.ArrayType) {
printDimensions((ArrayType) arrayType.getElementType(), p);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

/**
* Indicates that a {@link org.openrewrite.java.tree.J.ArrayType} represents a varargs parameter type.
* When present, the array type should be printed using {@code ...} syntax instead of {@code []}.
* <p>
* For example, {@code String...} instead of {@code String[]}.
*/
@Value
@With
public class Varargs implements Marker {
UUID id;
}
Loading