RemoveUnusedImports removes imports when used in Generics only <!-- Thank you for…
- User
RemoveUnusedImports removes imports when used in Generics only
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.31.1
- Maven/Gradle plugin v6.40.0
How are you running OpenRewrite?
I am using the Maven plugin, and my project is a single module project.
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>6.40.0</version> <executions> <execution> <phase>process-sources</phase> <goals> <goal>run</goal> </goals> <configuration> <activeRecipes combine.children="append"> <recipe>org.openrewrite.java.RemoveUnusedImports</recipe> <recipe>org.openrewrite.java.OrderImports</recipe> </activeRecipes> </configuration> </execution> </executions> </plugin>What is the smallest, simplest way to reproduce the problem?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you expect to see?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you see instead?
import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What is the full stack trace of any errors you encountered?
stacktrace output hereAre you interested in contributing a fix to OpenRewrite?
Interface
No new interfaces are introduced.
- Assistant
I'll locate
RemoveUnusedImportsand trace how it decides an import is unused, especially for generic type parameters. - Assistant
The bug likely stems from
sourceIdentifierNamesmissing types that only appear in generics. I'll trace how generic type references are represented in the AST. - Assistant
Reproducing the issue with a focused run to see how the AST represents generic type references.
- Assistant
Running a quick reproduction to confirm the behavior:
- Assistant
Imports are likely dropped at lines 227 and 269 when type attribution is missing, even though the name still appears in generic source text. I'll add
sourceIdentifierNameschecks there and harden name collection. - Assistant
The
visitTypeNameoverride was pulling names from fully qualified javadoc links. Removing it and relying on the condition fixes instead. - Assistant
The javadoc test fails because
sourceIdentifierNamesis too broad. I'll revert those condition changes and fix nested generic type collection intypesByPackageinstead. - Assistant
All tests passed. Running final verification:
- Assistant
Fixed
RemoveUnusedImportsincorrectly removing imports used only in generic type arguments (e.g.BloomFilterinAtomicReference<BloomFilter<T>>).Root cause
Two gaps caused imports like
com.google.common.hash.BloomFilterto be treated as unused:- Nested generic types —
typesByPackageonly handled top-levelFullyQualifiedtype parameters, so nested types likeBloomFilterinsideAtomicReference<BloomFilter<T>>were missed. - Over-aggressive removal — when type attribution was incomplete (e.g. types not on the classpath), imports were removed even though the simple name still appeared in source type positions.
- Nested generic types —
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.