openrewrite / openrewrite/rewrite

`maybeAddImport` adds import although all references are fully qualified

Open
#4,261 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I developing a recipe with OpenRewrite v8.27.1 and have this test:

@Test void isMigratedToMethods_whenFullyQualified() {
    // language=java
    rewriteRun(java(
        """
        package de.foo.bar;
        
        @org.testng.annotations.Test
        public class BazTest {
        
            public void shouldDoStuff() {
                //
            }
        
            public void shouldDoMoreStuff() {
                //
            }
        }
        """,
        """
        package de.foo.bar;
        
        public class BazTest {
        
            @org.junit.jupiter.api.Test
            public void shouldDoStuff() {
                //
            }
        
            @org.junit.jupiter.api.Test
            public void shouldDoMoreStuff() {
                //
            }
        }
        """
    ));
}

The Visitor under test contains a call to maybeAddImport("org.junit.jupiter.api.Test").

What did you expect to see?

No import added, as it is not necessary.

What did you see instead?

 package de.foo.bar;
 
+import org.junit.jupiter.api.Test;
+
 public class BazTest {
 
     @org.junit.jupiter.api.Test

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

Start by locating the Visitor's maybeAddImport("org.junit.jupiter.api.Test") call and reproduce the issue with the isMigratedToMethods_whenFullyQualified test shown in the report. Trace how fully qualified references are detected; done means the shown test no longer adds an import when every reference remains fully qualified.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.