Skip to content

Commit

Permalink
Check class declarations instead of source path
Browse files Browse the repository at this point in the history
Source path and package declaration technically do not have to match, so this instead only looks for fully qualified class names
  • Loading branch information
kennytv committed May 11, 2024
1 parent e66aae7 commit c9163dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,22 @@ 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());
String newFqn = fqnToPath(targetType.getFullyQualifiedName());

Path newPath = Paths.get(oldPath.replaceFirst(oldFqn, newFqn));
if (updatePath(cu, oldPath, newPath.toString())) {
getCursor().putMessage("UPDATE_PACKAGE", true);
cu = cu.withSourcePath(newPath);
}
return super.visit(cu, ctx);
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

0 comments on commit c9163dd

Please sign in to comment.