From ac73c01cae5a69d8f6b4870edb8c60ea97612f5f Mon Sep 17 00:00:00 2001 From: Kun Li Date: Tue, 7 Nov 2023 11:48:35 -0800 Subject: [PATCH] Fix missing import-alias type, and pass all tests of RenameTypeAliasTest --- .../java/org/openrewrite/kotlin/RenameTypeAlias.java | 12 +++++++++++- .../kotlin/internal/KotlinTreeParserVisitor.java | 4 ++-- .../openrewrite/kotlin/internal/PsiTreePrinter.java | 2 +- .../openrewrite/kotlin/KotlinTypeSignatureBuilder.kt | 9 +++++++++ .../org/openrewrite/kotlin/RenameTypeAliasTest.java | 12 ++++++------ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java b/src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java index cb33fdac1..63e408e8c 100644 --- a/src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java +++ b/src/main/java/org/openrewrite/kotlin/RenameTypeAlias.java @@ -59,7 +59,8 @@ public J.Identifier visitIdentifier(J.Identifier i, ExecutionContext executionCo if (!i.getSimpleName().equals(aliasName) || !TypeUtils.isOfClassType(i.getType(), fullyQualifiedAliasedType)) { return i; } - if (!isVariableName(getCursor().getParentTreeCursor(), i)) { + if (!isVariableName(getCursor().getParentTreeCursor(), i) || + isAliasImport(getCursor().getParentTreeCursor(), i) ) { i = i.withSimpleName(newName); } return i; @@ -95,4 +96,13 @@ private boolean isVariableName(Cursor cursor, J.Identifier ident) { } return true; } + + private boolean isAliasImport(Cursor cursor, J.Identifier id) { + if (cursor.getValue() instanceof J.Import) { + J.Import ji = (J.Import) cursor.getValue(); + return ji.getAlias() == id; + } + + return false; + } } diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 24d0b3911..94ff7947a 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -2384,7 +2384,7 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex Markers.EMPTY, new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY), padLeft(Space.EMPTY, (J.Identifier) reference), - null + type(importDirective) ); } @@ -2395,7 +2395,7 @@ public J visitImportDirective(KtImportDirective importDirective, ExecutionContex rpStatic, (J.FieldAccess) reference, // TODO: fix NPE. - alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), null)) : null + alias != null ? padLeft(prefix(alias), createIdentifier(requireNonNull(alias.getNameIdentifier()), type(alias))) : null ); } diff --git a/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java b/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java index 1ec855eb3..a65b0c946 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java +++ b/src/main/java/org/openrewrite/kotlin/internal/PsiTreePrinter.java @@ -66,7 +66,7 @@ public class PsiTreePrinter { private static KotlinIrTypeMapping irTypeMapping = new KotlinIrTypeMapping(new JavaTypeCache()); // Set to true to print types and verify, otherwise just verify the parse to print idempotent. - private final static boolean printTypes = false; + private final static boolean printTypes = true; private final List outputLines; diff --git a/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeSignatureBuilder.kt b/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeSignatureBuilder.kt index 47f9322d2..fd5899647 100644 --- a/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeSignatureBuilder.kt +++ b/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeSignatureBuilder.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaTypeParameter import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.types.Variance import org.openrewrite.java.JavaTypeSignatureBuilder import org.openrewrite.java.tree.JavaType @@ -167,6 +168,14 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val javaElement(type) } + is FirResolvedImport -> { + signature(type.importedFqName) + } + + is FqName -> { + type.asString() + } + else -> "{undefined}" } } diff --git a/src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java b/src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java index 21ebc0e66..f4eaf674a 100644 --- a/src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java +++ b/src/test/java/org/openrewrite/kotlin/RenameTypeAliasTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.kotlin.Assertions.kotlin; -public class RenameTypeAliasTest implements RewriteTest { +class RenameTypeAliasTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new RenameTypeAlias("OldAlias", "NewAlias", "Test")); @@ -71,10 +71,10 @@ class Test ); } - @ExpectedToFail("FirImport does not contain type attribution.") @Test - void _import() { + void aliasImport() { rewriteRun( + spec -> spec.recipe(new RenameTypeAlias("OldAlias", "NewAlias", "foo.Test")), kotlin( """ package foo @@ -84,13 +84,13 @@ class Test kotlin( """ import foo.Test as OldAlias - + val a : OldAlias = OldAlias() """, """ import foo.Test as NewAlias - - val a : NewAlias = Test() + + val a : NewAlias = NewAlias() """ ) );