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 to reproduce the issue.
- Assistant
Let me examine the RemoveUnusedImports implementation and its tests.
- Assistant
I'll continue reading the visitor logic, focusing on type and identifier collection.
- Assistant
Let me continue reading and also look at collectSourceIdentifierNames since that seems related.
- Assistant
The bug likely stems from source identifier collection missing generic type arguments. Checking that function next.
- Assistant
Let me read the rest of the file for collectSourceIdentifierNames and collectUnqualifiedTypeNames
- Assistant
Looking at the issue more carefully: I think the issue is in how
TypesInUsecollects types from parameterized types. Specifically, when it processes the type parameterBloomFilter<T>, it only looks for `JavaType. - Assistant
Let me look at asFullyQualified
- Assistant
I want to check how parameterized type AST nodes are visited, and inspect the related tests for when this bug might have been introduced.
- Assistant
Next I'll scan the tests and git history for this behavior.
- Assistant
The recent commits look relevant — I'll inspect those changes next.
- Assistant
I'll check how class declarations visit their extends clauses and parameterized type structures.
- Assistant
Let me look at visitClassDeclaration and visitParameterizedType
- Assistant
The extends is visited via visitLeftPadded which should visit ParameterizedType and its nested types. Let me also look at visitParameterizedType.
- Assistant
I need to understand how TypesInUse collects types — that may explain the nested generic miss.
- Assistant
Let me read TypesInUse.java
- Assistant
Let me look at FindTypesInUse
- Assistant
Let me read that section
- Assistant
I'll look at existing tests to see how to reproduce this cleanly.
- Assistant
I'll check the recent test cases next and try to reproduce with a temporary test.
- Assistant
I'll create a temporary test to reproduce the BloomFilter generics import issue.
- Assistant
Checking NoMissingTypes and similar edge cases to pinpoint why the import is dropped.
- Assistant
Let me also check whether there's a simpler case - like with just java.util List used in generics only.
- Assistant
I'll add a temporary test and run the existing suite setup to reproduce the issue.
- Assistant
I'll add a temporary reproduction test and run it.
- Assistant
Checking whether the test file was written and picked up correctly.
- Assistant
The create command left an empty file — I'll write the test with a shell heredoc instead.
- Assistant
The basic case passed—trying variants without type info and with OrderImports like the reported setup.
- Assistant
I'll fix the constructor call and keep exploring related cases.
- Assistant
I'll compare the current repo version and look for related fixes after 8.31.1.
- Assistant
The unknown-type case is a real failure — next I'll inspect how FindMissingTypes handles type arguments.
- Assistant
Let me read FindMissingTypes
- Assistant
Let me read more of FindMissingTypes visitor, particularly how it handles identifiers and Parameterized types.
- Assistant
There's
isAllowedToHaveNullType- maybe identifiers in generics type positions are allowed to have null type? - Assistant
There it is!
private boolean isParameterizedType(J.Identifier ident) { - Assistant
The root cause is clear: generic type identifiers without type attribution slip past NoMissingTypes, so imports get dropped. I'll identify type-reference positions more precisely next.
- Assistant
Let me look at how RemoveImport handles this - it might have similar logic.
- Assistant
I need to verify how
JavaType.Parameterizedexposes package and owning class information. - Assistant
Checking how
isWellFormedTypetreatsUnknowntype parameters. - Assistant
I'll add a debug dump for TypesInUse on the reproduction case to see what's attributed.
- Assistant
I'll pull the test output from the reports next.
- Assistant
I'll search the repo and docs for anything related to this specific issue.
- Assistant
Let me look at the #1128 test that was added.
- Assistant
I'll look for existing helpers that identify type-reference identifiers.
- Assistant
Checking the static field qualifier fix for how source identifiers are used.
- Assistant
Checking whether
JavaType.Unknownis treated asFullyQualifiedand how that's handled. - Assistant
Let me elfind Unknown class
- Assistant
I'll try fixing FindMissingTypes so NoMissingTypes blocks removal when generics have unknown types — that matches the recipe's stated precaution.
- Assistant
Cleaning up the shell state and writing the test file carefully.
- Assistant
I'll use Perl to write the test file cleanly.
- Assistant
Checking what FQN
JavaType.Unknownreports: - Assistant
Implementing the fix: track type-reference simple names and treat those imports as used.
- Assistant
Implementing type-reference simple-name tracking in
RemoveUnusedImports. - Assistant
Updating the unused-import checks to honor type-reference simple names.
- Assistant
I need to update lines 233 and 275-280 to also check typeReferenceSimpleNames.
- Assistant
Now fix the second check at the end.
- Assistant
Next I'll add the
collectTypeReferenceSimpleNamesmethod. - Assistant
Need to goto that line first
- Assistant
I'll implement a simpler version of the type-reference collector.
- Assistant
Cleaning up the implementation and verifying it compiles.
- Assistant
Need to goto line 588 first
- Assistant
I notice that when parent is ParameterizedType, we return true for BOTH clazz and... wait, for type argument Identifier, parent Іs NOT ParameterizedType - the type arguments are in a container. The cl...
- Assistant
There's a leftover fragment to remove.
- Assistant
Compiling and running the reproduction tests.
- Assistant
BloomX is kept correctly; I'll update the test so the unused
Listimport is expected to be removed. - Assistant
Running the full RemoveUnusedImportsTest suite to check for regressions.
- Assistant
All tests passed. Removing the temporary test file before submitting.
- Assistant
There's an unintended change in settings.gradle.kts — reverting it.
- Assistant
Replacing any non-ASCII characters in comments with ASCII.
- Assistant
Verifying the final diff once more, then submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.