openrewrite / openrewrite/rewrite

`ChangeType` not working with types provided to the parser using `dependsOn`.

Open
#4,030 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug java
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.17.1
  • Maven/Gradle plugin v5.23.1
  • rewrite-java v8.17.1

How are you running OpenRewrite?

I am using the following unit test:


    @Test
    void renamePackageWithDependsOn() {
        @Language("java")
        final String schemaClass1 = """
          package my.example.xprivate.x1.MySchema;
          
          public class MySchemaType {}
          """;

        @Language("java")
        final String schemaClass2 = """
          package my.example._private._1.MySchema;
          
          public class MySchemaType {}
          """;

        rewriteRun(
          spec -> spec
            .parser(JavaParser.fromJavaVersion().dependsOn(schemaClass1, schemaClass2))
            .recipe(new ChangeType(
              "my.example.xprivate.x1.MySchema.MySchemaType",
              "my.example._private._1.MySchema.MySchemaType",
              true
            )),
          //language=java
          java("""
            package my.example.app;
            
            import my.example.xprivate.x1.MySchema.MySchemaType;
            
            class MyApp {
                void doWork(MySchemaType mySchema) {}
            }
            """, """
            package my.example.app;
            
            import my.example._private._1.MySchema.MySchemaType;
            
            class MyApp {
                void doWork(MySchemaType mySchema) {}
            }
            """)
        );
    }

What is the smallest, simplest way to reproduce the problem?

Using the provided test.

What did you expect to see?

package my.example.app;
            
import my.example._private._1.MySchema.MySchemaType;
            
class MyApp {
    void doWork(MySchemaType mySchema) {}
}

What did you see instead?

diff --git a/my/example/app/MyApp.java b/my/example/app/MyApp.java
index 5a97edd..9ff90dd 100644
--- a/my/example/app/MyApp.java
+++ b/my/example/app/MyApp.java
@@ -1,7 +1,5 @@ 
 package my.example.app;
 
-import my.example._private._1.MySchema.MySchemaType;
-
 class MyApp {
     void doWork(MySchemaType mySchema) {}
 }
\ No newline at end of file

What is the full stack trace of any errors you encountered?

N/A

Are you interested in contributing a fix to OpenRewrite?

Yes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied renamePackageWithDependsOn test first, then trace JavaParser.fromJavaVersion().dependsOn(...) and ChangeType to understand how the provided types are resolved. Done means the test produces the expected import from my.example._private._1.MySchema.MySchemaType and keeps the MySchemaType usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.