Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.openrewrite.java.RemoveUnusedImports should not removed imports for public static inner class usage #3283

Closed
iparadiso opened this issue May 31, 2023 · 3 comments · Fixed by #4479
Labels
bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Comments

@iparadiso
Copy link

org.openrewrite.java.RemoveUnusedImports doesn't appear to spot static public inner class usage that's used with wildcard imports statements.

What version of OpenRewrite are you using?

I am using version: 7.40.6

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

Consider the following class

package com.a.b.c;
public final class ParentBaseClass {
  public static abstract class BaseImplClass {
     void foo() {
      } 
  }
}

Which is then used in the following class

import com.a.b.c.ParentBaseClass.*
import java.util.List;
public class MyClass extends BaseImplClass {
   @Override
   public void foo() {
   }
}

What did you expect to see?

import com.a.b.c.ParentBaseClass.*
public class MyClass extends BaseImplClass {
   @Override
   public void foo() {
   }
}

What did you see instead?

public class MyClass extends BaseImplClass {
   @Override
   public void foo() {
   }
}
@iparadiso iparadiso added the bug Something isn't working label May 31, 2023
@joanvr joanvr moved this to Backlog in OpenRewrite Jun 2, 2023
@joanvr
Copy link
Contributor

joanvr commented Jun 2, 2023

Hi, thanks for reporting this issue! Would you be open to contribute to write a unit test for it or even a fix?

@timtebeek timtebeek changed the title org.openrewrite.java.RemoveUnusedImports should not removed imports for public static inner class usage org.openrewrite.java.RemoveUnusedImports should not removed imports for public static inner class usage Feb 5, 2024
@timtebeek
Copy link
Contributor

timtebeek commented Feb 5, 2024

Confirmed with this (slightly different) test.

@Test
void nestedImports() {
    rewriteRun(
      java(
        """
          package foo;
                        
          public interface J {
              final class One implements J {}
              final class Two implements J {}
              final class Three implements J {}
              final class Four implements J {}
              final class Five implements J {}
              final class Six implements J {}
          }
          """
      ),
      java(
        """
          package bar;
          
          import foo.J;
          import foo.J.*;
                        
          class Quz {
            void test() {
              J j = null;
              One one = null;
              Two two = null;
              Three three = null;
              Four four = null;
              Five five = null;
              Six six = null;
            }
          }
          """
      )
    );
}

@timtebeek timtebeek added the test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail label Feb 5, 2024
@timtebeek
Copy link
Contributor

@Laurens-W Since you evaluated RemoveUnusedImports in #4450 it might make sense to briefly look at this one as well. The test is already there; perhaps the fix is similar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants