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 understand how it handles generic type usage.
- Assistant
Let me examine the RemoveUnusedImports implementation and related tests.
- Assistant
I'll continue reading the visitor logic, focusing on type collection and generic handling.
- Assistant
The recent
collectSourceIdentifierNameslogic looks suspicious — I'll dig into that next. - Assistant
I found a check around line 264 that marks imports unused when simple names aren't in source identifiers — examining that collection logic next.
- Assistant
Let me look at collectSourceIdentifierNames and collectUnqualifiedTypeNames.
- Assistant
Checking how generics and type parameters are tracked in types-in-use, and reviewing related tests.
- Assistant
Let me look at TypeUtils.asFullyQualified implementation - particularly if it unwraps Parameterized types.
- Assistant
Checking whether Parameterized implements FullyQualified and how nested generics are handled in type collection.
- Assistant
Looking at recent changes and existing tests more carefully to reproduce the bug.
- Assistant
Inspecting Parameterized type structure and how nested generic type parameters are handled.
- Assistant
Checking how TypesInUse collects parameterized and generic type usages.
- Assistant
Let me read TypesInUse to understand how types are collected.
- Assistant
Let me find FindTypesInUse
- Assistant
Let me read the FindTypesInUse class fully.
- Assistant
I'll write a temporary test to reproduce the generic-only import removal issue.
- Assistant
Inspecting the recent commits that introduced the sourceIdentifierNames check:
- Assistant
I'll look at how ParameterizedType is modeled in the AST.
- Assistant
Checking how tests are usually run and scanning recent tests for similar generic cases.
- Assistant
Creating a temporary reproduction test using Guava types, matching the reported issue.
- Assistant
I'll add a temporary test file to reproduce the issue.
- Assistant
The test wasn't discovered — checking the file and Gradle test configuration.
- Assistant
The test file didn't write correctly — recreating it properly.
- Assistant
Good, now let's run the test.
- Assistant
Those cases passed with resolved types — trying scenarios with missing types and other generic shapes.
- Assistant
The mishandled case is missing generic types — checking why
NoMissingTypesdoesn't skip that scenario. - Assistant
Let me read NoMissingTypes.
- Assistant
Checking FindMissingTypes for gaps around generic type arguments.
- Assistant
Let me continue reading the visitor.
- Assistant
There's
isAllowedToHaveNullTypefor identifiers—checking whether type arguments are allowed null types. - Assistant
There it is: identifiers that are the clazz of a ParameterizedType are allowed to have null type!
- Assistant
Checking how Unknown types are named and whether nested generics can leave types unresolved even with the classpath present.
- Assistant
Searching the repo and recent history for generics-related import removal issues.
- Assistant
I'll dump the AST types for the failing case so we can see what's attributed.
- Assistant
Checking the test report for System.out output.
- Assistant
Confirming: unknown generic type args are skipped in types-in-use. Checking
isWellFormedTypeand implementing the fix. - Assistant
Checking how ClassDeclaration/extends are validated for missing types:
- Assistant
I accidentally opened the wrong file — opening FindMissingTypes correctly.
- Assistant
Implementing the fix: keep imports whose simple names appear as type-use identifiers with Unknown types (common for generic type arguments when attribution is incomplete).
- Assistant
Double-checking the Guava-attributed AST for comparison.
- Assistant
Cleaning up the repro test file and re-running cleanly.
- Assistant
Resolved case passed; checking the Guava dump next.
- Assistant
Using grep/sed instead:
- Assistant
Implementing collection of unknown type-use simple names and using them when deciding whether an import is unused.
- Assistant
Updating the two unused-import decision branches.
- Assistant
I'll update lines 229 and 271-276.
- Assistant
To now need to update the last branch too.
- Assistant
There's an extra closing brace — removing it.
- Assistant
Replacing
collectUnqualifiedTypeNameswithcollectTypeUseInfo. - Assistant
I'll replace collectUnqualifiedTypeNames with collectTypeUseInfo and add TypeUseInfo class.
- Assistant
Running the reproduction tests and the existing RemoveUnusedImports tests.
- Assistant
Verifying the type-attribution-only case still removes the unused import, and covering a few more edge cases.
- Assistant
The test file got corrupted — rewriting it carefully.
- Assistant
Writing the test with a short Python-free approach to avoid corruption.
- Assistant
Fixing the accidental space in the expected output.
- Assistant
Looking up the existing type-attribution test for the correct pattern.
- Assistant
Existing tests already cover type-attribution. Cleaning up the temp test and re-running.
- Assistant
Using the
createcommand carefully to rewrite the test file. - Assistant
Writing the file via base64 to avoid text corruption.
- Assistant
Removing the temporary test and verifying the final source changes.
- Assistant
Removing the temporary test and confirming the final main-code changes.
- Assistant
Restoring the accidental settings.gradle.kts change and re-running tests.
- Assistant
Re-verifying the original issue with a minimal temporary test.
- Assistant
All repro cases passed. Removing the temporary test before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.