Skip to content

Commit

Permalink
Added support for annotated varargs. (#3882)
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima authored Jan 3, 2024
1 parent 77ab3fb commit a26ec03
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.LeadingBrace;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

Expand Down Expand Up @@ -1154,18 +1155,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) {
int saveCursor = cursor;
Space before = whitespace();
JLeftPadded<Space> dimension;
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY);
markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId()));
} else {
cursor++;
dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before);
} else {
cursor = saveCursor;
return elemType;
}

return new J.ArrayType(
randomId(),
fmt,
Markers.EMPTY,
markers,
elemType,
null,
dimension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaParsingException;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
Expand Down Expand Up @@ -1281,11 +1282,22 @@ private TypeTree arrayTypeTree(Tree tree, Map<Integer, JCAnnotation> annotationP
}

Space prefix = whitespace();
Markers markers = Markers.EMPTY;
TypeTree elemType = convert(typeIdent);
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));
JLeftPadded<Space> dimension;
int saveCursor = cursor;
whitespace();
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
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 @@ -1303,20 +1315,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
int saveCursor = cursor;
whitespace();
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
JLeftPadded<Space> dimension;
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)
);
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
dimension = padLeft(sourceBefore("["), sourceBefore("]"));
}
cursor = saveCursor;

return new J.ArrayType(
randomId(),
EMPTY,
markers,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
dimension,
typeMapping.type(tree)
);
}
return baseType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.LeadingBrace;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

Expand Down Expand Up @@ -1159,18 +1160,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) {
int saveCursor = cursor;
Space before = whitespace();
JLeftPadded<Space> dimension;
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY);
markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId()));
} else {
cursor++;
dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before);
} else {
cursor = saveCursor;
return elemType;
}

return new J.ArrayType(
randomId(),
fmt,
Markers.EMPTY,
markers,
elemType,
null,
dimension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openrewrite.java.JavaParsingException;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.CompactConstructor;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
Expand Down Expand Up @@ -1358,11 +1359,22 @@ private TypeTree arrayTypeTree(Tree tree, Map<Integer, JCAnnotation> annotationP
}

Space prefix = whitespace();
Markers markers = Markers.EMPTY;
TypeTree elemType = convert(typeIdent);
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));
JLeftPadded<Space> dimension;
int saveCursor = cursor;
whitespace();
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
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 @@ -1380,20 +1392,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
int saveCursor = cursor;
whitespace();
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
JLeftPadded<Space> dimension;
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)
);
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
dimension = padLeft(sourceBefore("["), sourceBefore("]"));
}
cursor = saveCursor;

return new J.ArrayType(
randomId(),
EMPTY,
markers,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
dimension,
typeMapping.type(tree)
);
}
return baseType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.LeadingBrace;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

Expand Down Expand Up @@ -1158,18 +1159,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) {
int saveCursor = cursor;
Space before = whitespace();
JLeftPadded<Space> dimension;
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY);
markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId()));
} else {
cursor++;
dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before);
} else {
cursor = saveCursor;
return elemType;
}

return new J.ArrayType(
randomId(),
fmt,
Markers.EMPTY,
markers,
elemType,
null,
dimension,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openrewrite.java.JavaParsingException;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.marker.CompactConstructor;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.marker.OmitParentheses;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;
Expand Down Expand Up @@ -1356,11 +1357,22 @@ private TypeTree arrayTypeTree(Tree tree, Map<Integer, JCAnnotation> annotationP
}

Space prefix = whitespace();
Markers markers = Markers.EMPTY;
TypeTree elemType = convert(typeIdent);
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
JLeftPadded<Space> dimension = padLeft(sourceBefore("["), sourceBefore("]"));
JLeftPadded<Space> dimension;
int saveCursor = cursor;
whitespace();
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
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 @@ -1378,20 +1390,26 @@ private TypeTree mapDimensions(TypeTree baseType, Tree tree, Map<Integer, JCAnno
List<J.Annotation> annotations = leadingAnnotations(annotationPosTable);
int saveCursor = cursor;
whitespace();
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
JLeftPadded<Space> dimension;
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)
);
markers = markers.addIfAbsent(new OmitBrackets(randomId()));
dimension = padLeft(EMPTY, EMPTY);
} else {
cursor = saveCursor;
dimension = padLeft(sourceBefore("["), sourceBefore("]"));
}
cursor = saveCursor;

return new J.ArrayType(
randomId(),
EMPTY,
markers,
mapDimensions(baseType, ((JCArrayTypeTree) typeIdent).elemtype, annotationPosTable),
annotations,
dimension,
typeMapping.type(tree)
);
}
return baseType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.LeadingBrace;
import org.openrewrite.java.marker.OmitBrackets;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

Expand Down Expand Up @@ -1075,18 +1076,20 @@ public J visitArrayType(ArrayTypeTree node, Space fmt) {
int saveCursor = cursor;
Space before = whitespace();
JLeftPadded<Space> dimension;
if (source.startsWith("[", cursor)) {
Markers markers = Markers.EMPTY;
if (source.startsWith("...", cursor)) {
cursor = saveCursor;
dimension = JLeftPadded.build(EMPTY).withBefore(EMPTY);
markers = markers.addIfAbsent(new OmitBrackets(Tree.randomId()));
} else {
cursor++;
dimension = JLeftPadded.build(Space.build(sourceBeforeAsString("]"), emptyList())).withBefore(before);
} else {
cursor = saveCursor;
return elemType;
}

return new J.ArrayType(
randomId(),
fmt,
Markers.EMPTY,
markers,
elemType,
null,
dimension,
Expand Down
Loading

0 comments on commit a26ec03

Please sign in to comment.