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 4 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();
}
}
"""
)
);
}
}
26 changes: 15 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 @@ -509,6 +509,7 @@ public J visit(@Nullable Tree tree, ExecutionContext ctx) {

Path newPath = Paths.get(oldPath.replaceFirst(oldFqn, newFqn));
if (updatePath(cu, oldPath, newPath.toString())) {
getCursor().putMessage("UPDATE_PATH", true);
kennytv marked this conversation as resolved.
Show resolved Hide resolved
cu = cu.withSourcePath(newPath);
}
return super.visit(cu, ctx);
Expand All @@ -531,17 +532,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 updatePath = getCursor().pollNearestMessage("UPDATE_PATH");
if (updatePath != null && updatePath) {
kennytv marked this conversation as resolved.
Show resolved Hide resolved
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
Loading