openrewrite / openrewrite/rewrite
RemoveUnusedImports removes required static import for inherited nested type
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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