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 @@ -1298,7 +1298,11 @@ public J visitAnnotatedType(AnnotatedTypeTree node, Space fmt) {
if (node.getUnderlyingType() instanceof JCFieldAccess) {
return new J.AnnotatedType(randomId(), fmt, Markers.EMPTY, leadingAnnotations, annotatedTypeTree(node.getUnderlyingType(), annotationPosTable));
} else if (node.getUnderlyingType() instanceof JCArrayTypeTree) {
return new J.AnnotatedType(randomId(), fmt, Markers.EMPTY, leadingAnnotations, arrayTypeTree(node, annotationPosTable));
J.AnnotatedType annotatedType = new J.AnnotatedType(randomId(), fmt, Markers.EMPTY, leadingAnnotations, arrayTypeTree(node, annotationPosTable));
// if (((JCArrayTypeTree) node.getUnderlyingType()).getType() instanceof JCAnnotatedType) {
// visitAnnotatedType((AnnotatedTypeTree) ((JCArrayTypeTree) node.getUnderlyingType()).getType(), fmt);
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
// }
return annotatedType;
}
}
return new J.AnnotatedType(randomId(), fmt, Markers.EMPTY, leadingAnnotations, convert(node.getUnderlyingType()));
Expand Down Expand Up @@ -1387,7 +1391,7 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno
if (typeIdent instanceof JCArrayTypeTree) {
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
int saveCursor = cursor;
whitespace();
Space space = whitespace();
if (source.startsWith("[", cursor)) {
cursor = saveCursor;
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));
Expand All @@ -1400,6 +1404,18 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno
dimension,
typeMapping.type(tree)
);
} else if (source.startsWith("...", cursor)) {
J.ArrayType type = new J.ArrayType(
randomId(),
EMPTY,
Markers.EMPTY,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
JLeftPadded.build(space),
typeMapping.type(tree)
);
cursor = saveCursor;
return type;
}
cursor = saveCursor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,71 @@ class A {
)
);
}

@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) {
}
}
"""
)
);
}
}
13 changes: 11 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,15 @@ public J visitVariableDeclarations(VariableDeclarations multiVariable, PrintOutp
p.append(']');
}
if (multiVariable.getVarargs() != null) {
if (!(multiVariable.getTypeExpression() instanceof ArrayType)) {
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