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
I'll start by exploring the repository structure and locating the RemoveUnusedImports recipe so I can reproduce the issue.
- Assistant
Let me examine theRemoveUnusedImports implementation and its tests.
- Assistant
Let me continue reading the file, especially the part about collecting types and the source identifier names.
- Assistant
Let me continue reading the non-static import logic and source identifier collection.
- Assistant
The key seems to be
sourceIdentifierNamesand how generics are collected — I'll inspect those methods next. - Assistant
Let me read the rest of the file for collectSourceIdentifierNames and collectUnqualifiedTypeNames.
- Assistant
I'll look at related tests and recent history around
sourceIdentifierNames. - Assistant
The recent type-attribution fix looks related — I'll inspect that change and the existing tests.
- Assistant
I'll inspect the related regression tests and try to reproduce the generics issue.
- Assistant
I'll search tests and TypeUtils for how generic type parameters are attributed on extends clauses.
- Assistant
I'll inspect TypeUtils.asFullyQualified and how existing tests are structured so I can reproduce the bug.
- Assistant
Checking whether Parameterized implements FullyQualified and how TypesInUse works.
- Assistant
JavaType.Parameterized extends FullyQualified. So asFullyQualified works.
- Assistant
Let me find TypesInUse.build
- Assistant
Let me read TypesInUse.java
- Assistant
Let me find FindTypesInUse
- Assistant
Let me read FindTypesInUse fully.
- Assistant
I'll add a temporary reproduction test and run it to confirm the bug.
- Assistant
Let me check NoMissingTypes and try reproducing the issue with a standalone test.
- Assistant
I'll add a temporary reproduction test near the end of the test class.
- Assistant
I'll create a temporary external test (won't leave permanent test changes) and run it.
- Assistant
The create helper left an empty file — I'll write the test contents correctly.
- Assistant
Running the reproduction test next.
- Assistant
Checking the test results more carefully.
- Assistant
Forcing a clean re-run of the tests.
- Assistant
Tests passed with local source types — checking if this was already fixed and searching related issues.
- Assistant
I'll try reproducing with classpath-style types and without a companion source file.
- Assistant
Updating the temporary test to match the issue's exact Guava-based case.
- Assistant
I'll debug by dumping collected type and identifier info for the failing-like case.
- Assistant
I'll pull the test stdout from the Gradle reports instead.
- Assistant
Let me read the XML for stdout.
- Assistant
Cleaning stale results and re-running the debug test.
- Assistant
Need to fix the print method.
- Assistant
python3 not found, use python or just cat + sed.
- Assistant
Current code keeps that import — I'll try bounds-only generics and also incomplete type info next.
- Assistant
Let me fix OrderImports constructor.
- Assistant
OrderImports requires Boolean and String.
- Assistant
Found it: with unknown types the import is removed. Checking failures and NoMissingTypes next.
- Assistant
I'll re-check every failing test case in the XML results.
- Assistant
Investigating FindMissingTypes and whether generic type arguments can slip through as unknown.
- Assistant
Let me read the visitor more carefully around type checking.
- Assistant
Checking when identifiers are allowed to have a null type.
- Assistant
Checking isParameterizedType — it may allow null types.
- Assistant
I'll check how ParameterizedType type is validated and what happens with unknown types without the classpath.
- Assistant
I have a solid reproduction. Reviewing the full import-usage logic once more before applying a fix.
- Assistant
Checking how type names are visited in JavaVisitor.
- Assistant
Checking how ParameterizedType visits its clazz name.
- Assistant
I'll check the default visitTypeName implementation.
- Assistant
Checking the recent static-field qualifier fix — sourceIdentifierNames may blanket-cover that.
- Assistant
Checking the FQN for Unknown types.
- Assistant
Implementing the fix: collect type simple names via
visitTypeNameso imports used only as generic type arguments are retained. - Assistant
Applying the fix in RemoveUnusedImports.
- Assistant
Updating the import-usage checks next.
- Assistant
Need to go to the line first from the editor's perspective.
- Assistant
Now I can replace.
- Assistant
Updating the second removal condition the same way.
- Assistant
Adding the
collectReferencedTypeSimpleNamesmethod. - Assistant
I'll add the new method after collectSourceIdentifierNames.
- Assistant
Running the reproduction tests and the full RemoveUnusedImports suite.
- Assistant
ParameterizedType.getClazz() returns Expression — adjusting the method accordingly.
- Assistant
Let me speak read ParameterizedType.
- Assistant
Switching `TypeTree` to `NameTree`/`Expression` where needed.
- Assistant
Checking how NameTree and TypeTree relate.
- Assistant
Updating types: getClazz returns NameTree, so I'll use that.
- Assistant
Re-running the tests.
- Assistant
Verifying that type-parameter trees are still fully visited so names are collected correctly.
- Assistant
All tests passed. Cleaning up the temporary reproduction test.
- Assistant
Reverting the unrelated settings.gradle.kts change.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.