Skip to content

Commit

Permalink
Fix uses of J.ArrayType
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Dec 29, 2023
1 parent 011873f commit 644084b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeTree;

public class CastArraysAsListToList extends Recipe {

Expand Down Expand Up @@ -53,25 +54,28 @@ private static class CastArraysAsListToListVisitor extends JavaVisitor<Execution
@Override
public J visitTypeCast(J.TypeCast typeCast, ExecutionContext executionContext) {
J j = super.visitTypeCast(typeCast, executionContext);
if (!(j instanceof J.TypeCast)) {
if (!(j instanceof J.TypeCast) || !(((J.TypeCast) j).getType() instanceof JavaType.Array)) {
return j;
}
typeCast = (J.TypeCast) j;
JavaType elementType = ((JavaType.Array) typeCast.getType()).getElemType();
while (elementType instanceof JavaType.Array) {
elementType = ((JavaType.Array) elementType).getElemType();
}

boolean matches = typeCast.getClazz().getTree() instanceof J.ArrayType
&& (typeCast.getType() instanceof JavaType.Class || typeCast.getType() instanceof JavaType.Parameterized)
&& ((JavaType.FullyQualified) typeCast.getType()).getOwningClass() == null // does not support inner class now
boolean matches = (elementType instanceof JavaType.Class || elementType instanceof JavaType.Parameterized)
&& ((JavaType.FullyQualified) elementType).getOwningClass() == null // does not support inner class now
&& LIST_TO_ARRAY.matches(typeCast.getExpression())
&& typeCast.getExpression() instanceof J.MethodInvocation
&& ARRAYS_AS_LIST.matches(((J.MethodInvocation) typeCast.getExpression()).getSelect());
if (!matches) {
return typeCast;
}

String fullyQualifiedName = ((JavaType.FullyQualified) typeCast.getType()).getFullyQualifiedName();
int dimensionSize = ((J.ArrayType) typeCast.getClazz().getTree()).getDimensions().size();
String fullyQualifiedName = ((JavaType.FullyQualified) elementType).getFullyQualifiedName();
J.ArrayType castType = (J.ArrayType) typeCast.getClazz().getTree();

if (fullyQualifiedName.equals("java.lang.Object") && dimensionSize == 1) {
if (fullyQualifiedName.equals("java.lang.Object") && !(castType.getElementType() instanceof J.ArrayType)) {
// we don't need to fix this case because toArray() does return Object[] type
return typeCast;
}
Expand All @@ -81,7 +85,7 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext executionContext) {
String className = fullyQualifiedName.substring(fullyQualifiedName.lastIndexOf(".") + 1);
newArrayString.append(className);
newArrayString.append("[0]");
for (int i = 0; i < dimensionSize - 1; i++) {
for (TypeTree temp = castType.getElementType(); temp instanceof J.ArrayType; temp = ((J.ArrayType) temp).getElementType()) {
newArrayString.append("[]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
*/
package org.openrewrite.java.migrate.lang.var;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
Expand All @@ -30,6 +25,9 @@
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.emptyList;

public class UseVarForGenericsConstructors extends Recipe {
Expand Down Expand Up @@ -202,10 +200,8 @@ private static Expression typeToExpression(JavaType type) {
return new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, emptyList(), className, type, null);
}
if (type instanceof JavaType.Array) {
int dimensions = StringUtils.countOccurrences(type.toString(), "[]");
List<JRightPadded<Space>> dimensionsDefinition = Collections.nCopies(dimensions, JRightPadded.build(Space.EMPTY));
TypeTree elemType = (TypeTree) typeToExpression(((JavaType.Array) type).getElemType());
return new J.ArrayType(Tree.randomId(), Space.EMPTY, Markers.EMPTY, elemType, dimensionsDefinition);
return new J.ArrayType(Tree.randomId(), Space.EMPTY, Markers.EMPTY, elemType, null, JLeftPadded.build(Space.EMPTY), type);
}
if (type instanceof JavaType.GenericTypeVariable) {
String variableName = ((JavaType.GenericTypeVariable) type).getName();
Expand Down

0 comments on commit 644084b

Please sign in to comment.