Skip to content

Commit

Permalink
Merge pull request #101 from openrewrite/kunli/fix-changePackage
Browse files Browse the repository at this point in the history
Fix a changePackage error with class inheritance
  • Loading branch information
kunli2 authored Apr 14, 2023
2 parents dbedf95 + 1b3b8dc commit 360e005
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ public <J2 extends Tree> J2 withMarkers(Markers markers) {

@Override
public <J2 extends J> 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
Expand Down
46 changes: 39 additions & 7 deletions src/test/java/org/openrewrite/kotlin/ChangePackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -118,18 +118,50 @@ 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();
})
)
);
}

@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();
})
)
);
}
}

0 comments on commit 360e005

Please sign in to comment.