Skip to content

Commit

Permalink
Enable package private SourceFile path renames in ChangeType.
Browse files Browse the repository at this point in the history
fixes #2528
  • Loading branch information
traceyyoshima committed Dec 14, 2022
1 parent 5c65f5a commit e6ab1b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,49 @@ interface ChangeTypeTest : JavaRecipeTest, RewriteTest {
""".trimIndent() + "\n")
}

@Issue("https://github.com/openrewrite/rewrite/issues/2528")
@Test
fun changePathOfNonPublicClass(@TempDir tempDir: Path) {
val sources = JavaParser.fromJavaVersion().build().parse(
listOf(tempDir.resolve("a/b/C.java").apply {
toFile().parentFile.mkdirs()
// language=java
toFile().writeText("""
package a.b;
class C {
}
class D {
}
""".trimIndent())
}),
tempDir,
InMemoryExecutionContext()
)

val results = ChangeType("a.b.C", "x.y.Z", false).run(sources).results

// similarity index doesn't matter
// language=diff
assertThat(results.joinToString("") { it.diff() }).isEqualTo("""
diff --git a/a/b/C.java b/x/y/Z.java
similarity index 0%
rename from a/b/C.java
rename to x/y/Z.java
index 1ef60ec..3b77cb3 100644
--- a/a/b/C.java
+++ b/x/y/Z.java
@@ -1,5 +1,5 @@ org.openrewrite.java.ChangeType
-package a.b;
-class C {
+package x.y;
+class Z {
}
class D {
}
\ No newline at end of file
""".trimIndent() + "\n")
}

@Issue("https://github.com/openrewrite/rewrite/issues/1904")
@Test
fun renamePackage() = rewriteRun(
Expand Down
26 changes: 9 additions & 17 deletions rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
.map(it -> it.getType().getFullyQualifiedName())
.collect(Collectors.toSet());

if (fullyQualifiedTarget != null && !(fullyQualifiedTarget.getOwningClass() != null && classNames.contains(getParentClassName(fullyQualifiedTarget)))) {
if (fullyQualifiedTarget != null && !(fullyQualifiedTarget.getOwningClass() != null && classNames.contains(getTopLevelClassName(fullyQualifiedTarget)))) {
if (fullyQualifiedTarget.getOwningClass() != null && !"java.lang".equals(fullyQualifiedTarget.getPackageName())) {
c = (J.CompilationUnit) new AddImport(fullyQualifiedTarget.getOwningClass().getFullyQualifiedName(), null, true).visit(c, ctx);
}
Expand All @@ -181,14 +181,6 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
return c;
}

private String getParentClassName(JavaType.FullyQualified fullyQualified) {
if (fullyQualified.getOwningClass() == null) {
return fullyQualified.getFullyQualifiedName();
}

return getParentClassName(fullyQualified.getOwningClass());
}

@Override
public J visitImport(J.Import impoort, ExecutionContext executionContext) {
// visitCompilationUnit() handles changing the imports.
Expand Down Expand Up @@ -497,18 +489,11 @@ private String fqnToPath(String fullyQualifiedName) {

private boolean updatePath(JavaSourceFile sf, String oldPath, String newPath) {
return !oldPath.equals(newPath) && sf.getClasses().stream()
.anyMatch(o -> J.Modifier.hasModifier(o.getModifiers(), J.Modifier.Type.Public) &&
.anyMatch(o -> !J.Modifier.hasModifier(o.getModifiers(), J.Modifier.Type.Private) &&
o.getType() != null && !o.getType().getFullyQualifiedName().contains("$") &&
TypeUtils.isOfClassType(o.getType(), getTopLevelClassName(originalType)));
}

private String getTopLevelClassName(JavaType.FullyQualified classType) {
if (classType.getOwningClass() == null) {
return classType.getFullyQualifiedName();
}
return getTopLevelClassName(classType.getOwningClass());
}

@Override
public J.Package visitPackage(J.Package pkg, ExecutionContext executionContext) {
J.Package p = super.visitPackage(pkg, executionContext);
Expand Down Expand Up @@ -594,4 +579,11 @@ private boolean isTargetFullyQualifiedType(@Nullable JavaType.FullyQualified fq)
return fq != null && TypeUtils.isOfClassType(fq, originalType.getFullyQualifiedName());
}
}

public static String getTopLevelClassName(JavaType.FullyQualified classType) {
if (classType.getOwningClass() == null) {
return classType.getFullyQualifiedName();
}
return getTopLevelClassName(classType.getOwningClass());
}
}

0 comments on commit e6ab1b1

Please sign in to comment.