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

Do not change package when non-recursive #4447

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -71,6 +71,56 @@ void renamePackageNonRecursive() {
class Test {
}
"""
),
java(
"""
package org.openrewrite.internal.one;
import org.openrewrite.internal.two.*;
class Test {

}
"""
)
);
}

@Test
void nonRecursiveShouldIgnoreNestedWildcardImport() {
rewriteRun(
spec -> spec.recipe(new ChangePackage(
"com.sun.net.ssl",
"javax.net.ssl",
false)),
//language=java
java(
"""
import com.sun.net.ssl.internal.*;
import com.sun.net.ssl.HostnameVerifier;
class Foo {
com.sun.net.ssl.HostnameVerifier hv;
}
""",
"""
import com.sun.net.ssl.internal.*;
import javax.net.ssl.HostnameVerifier;
class Foo {
javax.net.ssl.HostnameVerifier hv;
}
"""
),
java(
"""
import com.sun.net.ssl.internal.*;
class Bar {
com.sun.net.ssl.HostnameVerifier hv;
}
""",
"""
import com.sun.net.ssl.internal.*;
class Bar {
javax.net.ssl.HostnameVerifier hv;
}
"""
)
);
}
Expand Down Expand Up @@ -338,7 +388,7 @@ class Test {
}

@Test
void renamePackageRecursive() {
void renamePackageRecursiveByDefault() {
rewriteRun(
java(
"""
Expand Down Expand Up @@ -1156,7 +1206,7 @@ void callWorker() {
@Test
void typeInNestedPackageInheritingFromTypeInBasePackage() {
rewriteRun(
spec -> spec.recipe(new ChangePackage("java.util", "util", null)),
spec -> spec.recipe(new ChangePackage("java.util", "util", false)),
java(
"""
import java.util.concurrent.ConcurrentHashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
public @Nullable J preVisit(J tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
boolean recursive = ChangePackage.this.recursive == null || ChangePackage.this.recursive;
if (cu.getPackageDeclaration() != null) {
String original = cu.getPackageDeclaration().getExpression()
.printTrimmed(getCursor()).replaceAll("\\s", "");
if (original.startsWith(oldPackageName)) {
if (original.equals(oldPackageName) || recursive && original.startsWith(oldPackageName)) {
return SearchResult.found(cu);
}
}
boolean recursive = Boolean.TRUE.equals(ChangePackage.this.recursive);
String recursivePackageNamePrefix = oldPackageName + ".";
for (J.Import anImport : cu.getImports()) {
String importedPackage = anImport.getPackageName();
Expand Down
Loading