Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser does not handle annotated varargs correctly #4628

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -1388,21 +1388,17 @@ 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)
);
}
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
"""
)
);
}
}
11 changes: 9 additions & 2 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ protected void printStatementTerminator(Statement s, PrintOutputCapture<P> 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) {
Expand Down Expand Up @@ -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("...");
}
Expand Down
Loading