openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 321 forks source link

`maybeAddImport` adds import although all references are fully qualified #4261

Open Philzen opened 3 months ago

Philzen commented 3 months ago

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
timtebeek commented 3 months ago

Hmm; that's odd. I'd have imagined we'd have seen such an issue before, although it could be that we just look to see if I type is used, and not if all usages are fully qualified. 🤔