Skip to content

Commit

Permalink
Appropriately handle single dimensional array types from old LSTs. (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima authored Dec 28, 2023
1 parent 306b32a commit 2b9e95d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;
Expand Down Expand Up @@ -88,7 +89,7 @@ public J.ArrayType visitArrayType(J.ArrayType arrayType, ExecutionContext ctx) {
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY.addIfAbsent(new SearchResult(Tree.randomId(), "")),
((J.ArrayType) arrayType.getElementType()).getElementType(),
((J.ArrayType) arrayType.getElementType()).getElementType().withType(arrayType.getType()),
Arrays.asList(
JRightPadded.build(Space.EMPTY).withAfter(Space.build("", emptyList())),
JRightPadded.build(Space.EMPTY).withAfter(Space.build(" ", emptyList()))
Expand Down Expand Up @@ -130,4 +131,53 @@ public J.ArrayType visitArrayType(J.ArrayType arrayType, Object o) {
)
);
}

@Test
void singleDimensionalArrayFromJsonCreatorConstructor() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ArrayType visitArrayType(J.ArrayType arrayType, ExecutionContext ctx) {
if (arrayType.getMarkers().findFirst(SearchResult.class).isEmpty()) {
assert arrayType.getType() == null || "java.lang.Integer[]".equals(arrayType.getType().toString());
// Construct a new J.ArrayType from an old LST model.
//noinspection deprecation
return new J.ArrayType(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY.addIfAbsent(new SearchResult(Tree.randomId(), "")),
arrayType.getElementType(),
singletonList(
JRightPadded.build(Space.EMPTY).withAfter(Space.build("", emptyList()))
),
null,
null,
null
);
}
return super.visitArrayType(arrayType, ctx);
}
})),
java(
"""
class Test {
Integer[] n = new Integer[0];
}
""",
"""
class Test {
/*~~()~~>*/Integer[] n = new Integer[0];
}
""",
spec -> spec.afterRecipe(cu -> new JavaIsoVisitor<>() {
@Override
public J.ArrayType visitArrayType(J.ArrayType arrayType, Object o) {
assert arrayType.getType() != null;
assertThat(arrayType.getType().toString()).isEqualTo("java.lang.Integer");
return super.visitArrayType(arrayType, o);
}
}.visit(cu, 0))
)
);
}
}
14 changes: 5 additions & 9 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,8 @@ final class ArrayType implements J, TypeTree, Expression {
this.prefix = prefix;
this.markers = markers;
if (dimensions != null) {
JavaType javaType = elementType.getType();
for (int i = 0; i < dimensions.size(); i++) {
javaType = new JavaType.Array(null, javaType);
}
this.elementType = mapElement(elementType, mapType(javaType), dimensions, dimensions.size() - 1);
this.type = javaType;
this.elementType = dimensions.size() == 1 ? elementType : mapElement(elementType, mapType(elementType.getType()), dimensions, dimensions.size() - 2);
this.type = elementType.getType();
} else {
this.elementType = elementType;
this.type = type == null ? JavaType.Unknown.getInstance() : type;
Expand All @@ -376,15 +372,15 @@ final class ArrayType implements J, TypeTree, Expression {
}
}

private TypeTree mapElement(TypeTree elementType, @Nullable JavaType javaType, List<JRightPadded<Space>> dimensions, int count) {
private TypeTree mapElement(TypeTree elementType, @Nullable JavaType javaType, List<JRightPadded<Space>> dimensions, Integer count) {
JavaType nextType = mapType(javaType);
return new ArrayType(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
count <= 1 ? elementType.withType(nextType) : mapElement(elementType, nextType, dimensions, count - 1),
count == 0 ? elementType.withType(nextType) : mapElement(elementType, nextType, dimensions, count - 1),
null,
JLeftPadded.build(dimensions.get(count - 1).getAfter()).withBefore(dimensions.get(count - 1).getElement()),
JLeftPadded.build(dimensions.get(count).getAfter()).withBefore(dimensions.get(count).getElement()),
javaType
);
}
Expand Down

0 comments on commit 2b9e95d

Please sign in to comment.