rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

RemoveUnusedImports removes required static import for inherited nested type

Open protocol7 opened this issue 6 months ago • 1 comments

What version of OpenRewrite are you using?

  • rewrite-core: 8.81.4
  • rewrite-java: 8.81.4

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

This is an edge case involving a static import of a nested type through a class that inherits that nested type from an implemented interface.

package com.helloworld;

import static com.helloworld.FakeUserAccountClient.UserAccountDataAuth;

import java.util.List;

class Main {
  List<UserAccountDataAuth> getUsers() {
    return List.of(UserAccountDataAuth.create());
  }
}

class FakeUserAccountClient implements UserAccountClient {}

interface UserAccountClient {
  record UserAccountDataAuth() {
    static UserAccountDataAuth create() {
      return new UserAccountDataAuth();
    }
  }
}

Run org.openrewrite.java.RemoveUnusedImports.

What did you expect to see?

The static import should be preserved because UserAccountDataAuth is referenced as an unqualified type and static method receiver in Main.

The import is valid because FakeUserAccountClient inherits the nested type from UserAccountClient.

What did you see instead?

The recipe removes the static import:

package com.helloworld;

import java.util.List;

class Main {
  List<UserAccountDataAuth> getUsers() {
    return List.of(UserAccountDataAuth.create());
  }
}

class FakeUserAccountClient implements UserAccountClient {}

interface UserAccountClient {
  record UserAccountDataAuth() {
    static UserAccountDataAuth create() {
      return new UserAccountDataAuth();
    }
  }
}

The rewritten code no longer compiles because UserAccountDataAuth cannot be resolved.

protocol7 avatar May 05 '26 06:05 protocol7

Hello, I'd like to work on this issue. I'll start by reproducing it with a failing test and then investigate the import analysis around inherited nested types.

sakshichitnis27 avatar Jun 18 '26 12:06 sakshichitnis27