diff --git a/src/main/java/org/openrewrite/kotlin/tree/K.java b/src/main/java/org/openrewrite/kotlin/tree/K.java index 0d75dd0e7..a21492875 100644 --- a/src/main/java/org/openrewrite/kotlin/tree/K.java +++ b/src/main/java/org/openrewrite/kotlin/tree/K.java @@ -480,7 +480,10 @@ public J2 withMarkers(Markers markers) { @Override public J2 withType(@Nullable JavaType type) { - return (J2) withTypedTree(typedTree.withType(type)); + if (typedTree instanceof FunctionType) { + return (J2) withTypedTree(typedTree.withType(type)); + } + return (J2) this; } @Override diff --git a/src/test/java/org/openrewrite/kotlin/ChangePackageTest.java b/src/test/java/org/openrewrite/kotlin/ChangePackageTest.java index 2aa0676ba..c0a65b034 100644 --- a/src/test/java/org/openrewrite/kotlin/ChangePackageTest.java +++ b/src/test/java/org/openrewrite/kotlin/ChangePackageTest.java @@ -25,7 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.kotlin.Assertions.kotlin; -public class ChangePackageTest implements RewriteTest { +class ChangePackageTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new ChangePackage("a.b", "x.y", false)); @@ -118,13 +118,13 @@ void changeDefinition() { spec -> spec.recipe(new ChangePackage("org.foo", "x.y.z", false)), kotlin( """ - package org.foo - class Test - """, + package org.foo + class Test + """, """ - package x.y.z - class Test - """, + package x.y.z + class Test + """, spec -> spec.path("org/foo/Test.kt").afterRecipe(cu -> { assertThat(PathUtils.separatorsToUnix(cu.getSourcePath().toString())).isEqualTo("x/y/z/Test.kt"); assertThat(TypeUtils.isOfClassType(cu.getClasses().get(0).getType(), "x.y.z.Test")).isTrue(); @@ -132,4 +132,36 @@ class Test ) ); } + + @Test + void changePackageNameWithInheritance() { + rewriteRun( + spec -> spec.recipe(new ChangePackage("org.a", "org.b", false)), + kotlin( + """ + package org.a + + open class Animal() { + } + + class Dog : Animal() { + } + """, + """ + package org.b + + open class Animal() { + } + + class Dog : Animal() { + } + """, + spec -> spec.path("org/a/Dog.kt").afterRecipe(cu -> { + assertThat(PathUtils.separatorsToUnix(cu.getSourcePath().toString())).isEqualTo("org/b/Dog.kt"); + assertThat(TypeUtils.isOfClassType(cu.getClasses().get(0).getType(), "org.b.Animal")).isTrue(); + assertThat(TypeUtils.isOfClassType(cu.getClasses().get(1).getType(), "org.b.Dog")).isTrue(); + }) + ) + ); + } }