ISSUE DESCRIPTION RemoveUnusedImports removes imports when used in Generics only <!--…
- User
ISSUE DESCRIPTION
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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me check recent git history for this file:
Assistant
Let me write a quick reproduction test. First check how tests look:
Assistant
The simple case passes. Let me try to reproduce with a scenario closer to the issue — a two-type-parameter or different structure:
Assistant
Test passes. The issue may need a more nuanced case (e.g., wildcard bounds or deeper nesting). Let me try variants:
Assistant
Let me verify the tests are actually executing:
Assistant
The issue example passes on HEAD. Let me check when the type-parameter handling was added to understand what's still missing:
Assistant
Let me write a battery of edge-case tests to find what still fails:
Assistant
Let me examine how
TypesInUsecollects types:Assistant
Let me write a broad battery of generics constructs to find any failing case:
Assistant
Still passes — possibly because
NoMissingTypescatches something else. Let me debug directly:Assistant
NoMissingTypesprotects the return-type case via method-type checks, but nothing checksJ.ParameterizedType. Let me see the Assertions filter defaults:Assistant
Now I'll design the fix in
RemoveUnusedImports: retain imports whose simple name matches an identifier with missing/unknown type attribution. First check javadoc-related existing tests to gauge risk:Assistant
Now I'll implement the fix in
RemoveUnusedImports:Assistant
Now the non-static branch conditions (lines 234 and 276-284):
Assistant
Now add the
collectUnresolvedTypeNamesmethod aftercollectSourceIdentifierNames:Assistant
Both pass. Now run the full existing test suites for imports:
Assistant
All pass. Clean up the scratch test and review the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.