Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't modify package declaration of siblings in ChangeType #4184

Merged
merged 6 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1802,4 +1802,48 @@ public void test(String s) {
);

}

@Issue("https://github.com/openrewrite/rewrite/issues/4182")
@Test
void doesNotModifyPackageOfSibling() {
rewriteRun(
spec -> spec.recipe(new ChangeType("org.openrewrite.Test", "org.openrewrite.subpackage.Test", false)),
java(
"""
package org.openrewrite;
public class Test {
}
""",
"""
package org.openrewrite.subpackage;
public class Test {
}
"""
),
java(
"""
package org.openrewrite;

import org.openrewrite.Test;

public class Sibling {
public Test test() {
return new Test();
}
}
""",
"""
package org.openrewrite;

import org.openrewrite.subpackage.Test;

public class Sibling {
public Test test() {
return new Test();
}
}
"""
)
);
}
}
34 changes: 23 additions & 11 deletions rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ private ChangeClassDefinition(String oldFullyQualifiedTypeName, String newFullyQ
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) tree;
for (J.ClassDeclaration declaration : cu.getClasses()) {
// Check the class name instead of source path, as it might differ
String fqn = declaration.getType().getFullyQualifiedName();
if (fqn.equals(originalType.getFullyQualifiedName())) {
getCursor().putMessage("UPDATE_PACKAGE", true);
break;
}
}

String oldPath = cu.getSourcePath().toString().replace('\\', '/');
// The old FQN must exist in the path.
String oldFqn = fqnToPath(originalType.getFullyQualifiedName());
Expand Down Expand Up @@ -531,17 +540,20 @@ private boolean updatePath(JavaSourceFile sf, String oldPath, String newPath) {

@Override
public J.Package visitPackage(J.Package pkg, ExecutionContext ctx) {
String original = pkg.getExpression().printTrimmed(getCursor()).replaceAll("\\s", "");
if (original.equals(originalType.getPackageName())) {
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(targetType);
if (fq != null) {
if (fq.getPackageName().isEmpty()) {
getCursor().putMessageOnFirstEnclosing(J.CompilationUnit.class, "UPDATE_PREFIX", true);
//noinspection DataFlowIssue
return null;
} else {
String newPkg = targetType.getPackageName();
return JavaTemplate.builder(newPkg).contextSensitive().build().apply(getCursor(), pkg.getCoordinates().replace());
Boolean updatePackage = getCursor().pollNearestMessage("UPDATE_PACKAGE");
if (updatePackage != null && updatePackage) {
String original = pkg.getExpression().printTrimmed(getCursor()).replaceAll("\\s", "");
if (original.equals(originalType.getPackageName())) {
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(targetType);
if (fq != null) {
if (fq.getPackageName().isEmpty()) {
getCursor().putMessageOnFirstEnclosing(J.CompilationUnit.class, "UPDATE_PREFIX", true);
//noinspection DataFlowIssue
return null;
} else {
String newPkg = targetType.getPackageName();
return JavaTemplate.builder(newPkg).contextSensitive().build().apply(getCursor(), pkg.getCoordinates().replace());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Path getSourcePath() {
protected boolean noTrim = false;

/**
* @param sourcePath The source path after the recipe is run.
* @param sourcePath The source path before the recipe is run.
* @return This source spec.
*/
public SourceSpec<T> path(Path sourcePath) {
Expand All @@ -115,7 +115,7 @@ public SourceSpec<T> path(Path sourcePath) {
}

/**
* @param sourcePath The source path after the recipe is run.
* @param sourcePath The source path before the recipe is run.
* @return This source spec.
*/
public SourceSpec<T> path(String sourcePath) {
Expand Down