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);
}
return annotatedType;
}
}
return new J.AnnotatedType(randomId(), fmt, Markers.EMPTY, leadingAnnotations, convert(node.getUnderlyingType()));
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) {
}
}
"""
)
);
}
}
19 changes: 17 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,21 @@ public J visitVariableDeclarations(VariableDeclarations multiVariable, PrintOutp
p.append(']');
}
if (multiVariable.getVarargs() != null) {
if (!(multiVariable.getTypeExpression() instanceof ArrayType)) {
// The `visit(multiVariable.getTypeExpression(), p);` statement above does not know the
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
// enclosing VariableDeclarations is a vararg therefore we have to remove unnecessary dimensions here
if (p.out.charAt(p.out.length() - 1) == ']') {
int posToCheck = p.out.length() - 2;
while (p.out.charAt(posToCheck) != '[') {
if (Character.isWhitespace(p.out.charAt(posToCheck))) {
posToCheck--;
} else {
throw new IllegalStateException("Vararg was interpreted with non empty dimensions");
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
}
}
p.out.delete(posToCheck, p.out.length());
}
}
visitSpace(multiVariable.getVarargs(), Space.Location.VARARGS, p);
p.append("...");
}
Expand Down
Loading