openrewrite / openrewrite/rewrite

RemoveUnusedImports removes required static import for inherited nested type

Open
#7,569 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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.

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 with the org.openrewrite.java.RemoveUnusedImports recipe and run it against the minimal Java reproduction in the issue. Trace how the inherited nested type and its static import are resolved; done means the import is preserved and the rewritten code still compiles.

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
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.