Skip to content

Commit 52e3771

Browse files
committed
fix: ChangePackage from packageName is part of the submodule dir name issue
ie: ``` submodule name: p1-core and ChangePackage from: p1.sub1-> to: p2.sub1 before this pr result -> p2-core/src/java/sub1/xx after this pr result -> p1-core/src/java/p2/sub1/xx ```
1 parent 852189b commit 52e3771

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

rewrite-java/src/main/java/org/openrewrite/java/ChangePackage.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ public J postVisit(J tree, ExecutionContext ctx) {
259259
String path = ((SourceFile) sf).getSourcePath().toString().replace('\\', '/');
260260
String changingFrom = getCursor().getMessage(RENAME_FROM_KEY);
261261
assert changingFrom != null;
262-
sf = ((SourceFile) sf).withSourcePath(Paths.get(path.replaceFirst(
263-
changingFrom.replace('.', '/'),
264-
changingTo.replace('.', '/')
265-
)));
262+
final int dirIdx = path.indexOf('/');
263+
final String newPath = path.substring(0, dirIdx) + path.substring(dirIdx)
264+
.replaceFirst(
265+
changingFrom.replace('.', '/'),
266+
changingTo.replace('.', '/')
267+
);
268+
sf = ((SourceFile) sf).withSourcePath(Paths.get(newPath));
266269

267270
for (J.Import anImport : sf.getImports()) {
268271
if (anImport.getPackageName().equals(changingTo) && !anImport.isStatic()) {

0 commit comments

Comments
 (0)