Skip to content

Commit

Permalink
ChangeType does not work on J.ClassDeclaration (openrewrite#4670)
Browse files Browse the repository at this point in the history
* Add test

* Added compilation unit test

* Swap expected and actual

* Restore previous behavior

---------

Co-authored-by: Laurens Westerlaken <[email protected]>
  • Loading branch information
2 people authored and MBoegers committed Dec 18, 2024
1 parent 3b6066f commit 5b1d46c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.PathUtils;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.xml.Assertions.xml;

Expand Down Expand Up @@ -2042,4 +2046,44 @@ void changeTypeInSpringXml() {

}

@Test
void compilationUnitTest() {
@Language("java")
String helloClass = """
package hello;
public class HelloClass {}
""";

J.CompilationUnit compilationUnit = (J.CompilationUnit) JavaParser
.fromJavaVersion()
.build()
.parse(helloClass)
.findFirst()
.get();

// Check that ChangeType changes the class name for a J.CompilationUnit
compilationUnit = (J.CompilationUnit) new ChangeType("hello.HelloClass", "hello.GoodbyeClass", false).getVisitor().visit(compilationUnit, new InMemoryExecutionContext());
assertEquals("GoodbyeClass", compilationUnit.getClasses().get(0).getSimpleName());
}

@Test
void classDeclarationTest() {
@Language("java")
String helloClass = """
package hello;
public class HelloClass {}
""";

J.CompilationUnit compilationUnit = (J.CompilationUnit) JavaParser
.fromJavaVersion()
.build()
.parse(helloClass)
.findFirst()
.get();
J.ClassDeclaration classDeclaration = compilationUnit.getClasses().get(0);

// Check that ChangeType changes the class name for a J.ClassDeclaration
classDeclaration = (J.ClassDeclaration) new ChangeType("hello.HelloClass", "hello.GoodbyeClass", false).getVisitor().visit(classDeclaration, new InMemoryExecutionContext());
assertEquals("GoodbyeClass", classDeclaration.getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
@Override
public @Nullable Tree preVisit(@Nullable Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
if (tree instanceof JavaSourceFile) {
if (tree instanceof J) {
return new JavaChangeTypeVisitor(oldFullyQualifiedTypeName, newFullyQualifiedTypeName, ignoreDefinition).visit(tree, ctx, requireNonNull(getCursor().getParent()));
} else if (tree instanceof SourceFileWithReferences) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
Expand Down

0 comments on commit 5b1d46c

Please sign in to comment.