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?
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
I’ll trace
RemoveUnusedImports, identify how usage is collected for generic type trees, add the smallest production fix without modifying repository tests, then run the relevant test/build checks and inspect the diff. - Assistant
The failure is in the
TypesInUseconversion: for a parameterized type, the recipe records the outer type and only handles direct fully-qualified type arguments. A nested argument such asAtomicReference<BloomFilter<T>>can itself beJavaType.Parameterized, soBloomFilteris never added to the package/type usage map. I’ll make that collection recursive, while preserving existing handling of type variables and other type forms. - Assistant
The first edit attempt could not run because this environment lacks Python, so no file was changed. I’ll apply the same targeted replacement using the available shell tooling.
- Assistant
Compilation succeeded, and the focused tests exposed one compatibility regression: wildcard import unfolding relies on retaining the attributed parameterized type object in its usage set, not only its raw type. I’ll preserve that existing representation and additionally recurse into nested parameterized arguments, then rerun the focused suite.
- Assistant
The final diff is now limited to the intended 19 production lines; the earlier broad indentation accident was reverted. The recursive collection preserves existing behavior and adds nested generic arguments. Compilation and the complete
RemoveUnusedImportsTestclass pass, with only the repository’s pre-existingsettings.gradle.ktschange remaining outside this fix. I’ll submit now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.