Skip to content

Commit

Permalink
Ensure better Kotlin compatibility
Browse files Browse the repository at this point in the history
In Kotlin the top-level LST element is a `K.CompilationUnit`, which isn't a subtype of `J.CompilationUnit`, but they both implement the `JavaSourceFile` interface.
  • Loading branch information
knutwannheden committed Oct 17, 2023
1 parent 6ed13dc commit b7a3112
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.internal.TypesInUse;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -68,10 +69,10 @@ private OptimizedFlowModels getOptimizedFlowModelsForTypesInUse(TypesInUse types
}

private OptimizedFlowModels getOrComputeOptimizedFlowModels(Cursor cursor) {
Cursor cuCursor = cursor.dropParentUntil(J.CompilationUnit.class::isInstance);
Cursor cuCursor = cursor.dropParentUntil(JavaSourceFile.class::isInstance);
return cuCursor.computeMessageIfAbsent(
CURSOR_MESSAGE_KEY,
__ -> getOptimizedFlowModelsForTypesInUse(cuCursor.<J.CompilationUnit>getValue().getTypesInUse())
__ -> getOptimizedFlowModelsForTypesInUse(cuCursor.<JavaSourceFile>getValue().getTypesInUse())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openrewrite.analysis.trait.expr.Call;
import org.openrewrite.java.internal.TypesInUse;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -72,10 +73,10 @@ private OptimizedSinkModels getOptimizedSinkModelsForTypesInUse(TypesInUse types
}

private OptimizedSinkModels getOrComputeOptimizedSinkModels(Cursor cursor) {
Cursor cuCursor = cursor.dropParentUntil(J.CompilationUnit.class::isInstance);
Cursor cuCursor = cursor.dropParentUntil(JavaSourceFile.class::isInstance);
return cuCursor.computeMessageIfAbsent(
CURSOR_MESSAGE_KEY,
__ -> getOptimizedSinkModelsForTypesInUse(cuCursor.<J.CompilationUnit>getValue().getTypesInUse())
__ -> getOptimizedSinkModelsForTypesInUse(cuCursor.<JavaSourceFile>getValue().getTypesInUse())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.openrewrite.analysis.trait.variable.Variable;
import org.openrewrite.analysis.trait.variable.VariableUtil;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Javadoc;
import org.openrewrite.java.tree.*;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -172,7 +169,7 @@ public boolean isRValue() {
}

private static Variable computeVariable(VarAccessBase varAccess, Cursor cursor, J.Identifier varAccessIdent) {
Cursor compilationUnit = cursor.dropParentUntil(J.CompilationUnit.class::isInstance);
Cursor compilationUnit = cursor.dropParentUntil(JavaSourceFile.class::isInstance);
AtomicBoolean found = new AtomicBoolean(false);
Variable[] closestVariable = new Variable[1];
new JavaVisitor<AtomicBoolean>() {
Expand Down Expand Up @@ -357,7 +354,7 @@ public Collection<Flag> getFlags() {
}

static Field create(JavaType.Variable variable, Cursor anyCursor) {
return new FieldFromJavaTypeVariable(variable, anyCursor.dropParentUntil(J.CompilationUnit.class::isInstance));
return new FieldFromJavaTypeVariable(variable, anyCursor.dropParentUntil(JavaSourceFile.class::isInstance));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openrewrite.analysis.util.FlagUtil;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.util.Collection;
Expand Down Expand Up @@ -99,13 +100,13 @@ public Option<JavaType> getType() {
public Collection<VarAccess> getVarAccesses() {
// Searching starts at the J.CompilationUnit because we want to find all references to this field, within the file,
// not just within the class (which may contain multiple classes).
Cursor searchScope = parentBlock.dropParentUntil(J.CompilationUnit.class::isInstance);
Cursor searchScope = parentBlock.dropParentUntil(JavaSourceFile.class::isInstance);
return VarAccess.findAllInScope(searchScope, this);
}

@Override
public Collection<Expr> getAssignedValues() {
return VariableUtil.findAssignedValues(parentBlock.dropParentUntil(J.CompilationUnit.class::isInstance), this);
return VariableUtil.findAssignedValues(parentBlock.dropParentUntil(JavaSourceFile.class::isInstance), this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import org.openrewrite.analysis.trait.member.StaticInitializerMethod;
import org.openrewrite.analysis.trait.util.TraitErrors;
import org.openrewrite.analysis.util.FlagUtil;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.*;

import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -89,12 +86,12 @@ public Option<JavaType> getType() {

@Override
public Collection<VarAccess> getVarAccesses() {
return VarAccess.findAllInScope(cursor.dropParentUntil(J.CompilationUnit.class::isInstance), this);
return VarAccess.findAllInScope(cursor.dropParentUntil(JavaSourceFile.class::isInstance), this);
}

@Override
public Collection<Expr> getAssignedValues() {
return VariableUtil.findAssignedValues(cursor.dropParentUntil(J.CompilationUnit.class::isInstance), this);
return VariableUtil.findAssignedValues(cursor.dropParentUntil(JavaSourceFile.class::isInstance), this);
}

@Override
Expand Down

0 comments on commit b7a3112

Please sign in to comment.