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's add a temporary test method to reproduce, then run it with gradle.
Assistant
Need to fix missing closing brace at 2543.
Assistant
Now let's run this test using gradle.
Assistant
Now let's rewrite the debug test using actual guava classpath via JavaParser.dependenciesFromClasspath.
Assistant
Fine without the change too (settings didn't need modifying, was probably a red herring / not required for this module). Let's drop that stash entirely to avoid including unrelated changes in submission.
Assistant
Now let's revert the DebugTypesInUseTest to the original repro test and run it against this older version.
Assistant
Now let's rebuild and run the debug test to see if bug reproduces without the type-parameter recursion.
Assistant
It compiled. Now let's run the exact repro test against this ~2024-09 version.
Assistant
Let me look at the complete relevant section once more and design a more robust recursive type-parameter walker rather than relying on
TypesInUsehaving already flattened nested generics.Assistant
Now let's replace this block with a fully recursive walker, and add a helper method that recurses through nested generics (Parameterized type params, GenericTypeVariable bounds, Array element types) with cycle protection.
Assistant
Now let's add the helper method. Let's place it near
collectUnqualifiedTypeNames.Assistant
Let me add a blank line before the next method's javadoc for readability.
Assistant
Now let's fix the indentation of the method parameters (alignment) and compile.
Assistant
That's fine. Now let's check
@Nullableimport is present in this file (jspecify or org.openrewrite).Assistant
Need to add the import. Let's use
org.jspecify.annotations.Nullableas used elsewhere in the codebase.Assistant
Now let's compile.
Assistant
Compiles fine. Now let's run the full RemoveUnusedImportsTest suite plus my debug test to make sure everything passes.
Assistant
All 76 tests (including my added
reproduceGenericIssue) pass. Now let's remove my test additions fromRemoveUnusedImportsTest.javasince I shouldn't modify/add tests, then re-verify the suite still passes.Assistant
Now only the source fix remains as a change. Let's run the full RemoveUnusedImportsTest suite once more to make sure it's clean without the extra test method, plus the broader rewrite-java-test suite to check nothing else broke.
Assistant
All 75 tests pass. Now let's run the broader
rewrite-java-testfull suite to check for any regressions in other recipes that might interact withRemoveUnusedImports(e.g.,OrderImports,UnwrapParentheses, or other composite tests).Assistant
All tests in
rewrite-java-testpass. Let's also runrewrite-javamodule tests to be safe, and checkrewrite-java-tckif it has related tests.Assistant
All good. Let's do a final check to reproduce the original issue scenario with a throwaway script (not committed) to confirm the fix works, then view the final diff.
Assistant
Now let's confirm the final git status/diff, and clean up any leftover temp files.
Assistant
Clean — only the source fix remains. Let's view the final diff.
Assistant
Final verification of build and tests:
Assistant
Everything compiles cleanly with no leftover test artifacts. Let's do one last full test run of the affected test module to be thorough before submitting.
Assistant
All tests pass (cached from previous successful runs, confirming consistency). Let's review the checklist explicitly:
Assistant
Let's do one final comprehensive check before submitting - run the entire
rewrite-java-testsuite fresh (not cached) to be absolutely certain.Assistant
The full
rewrite-java-testsuite passes cleanly with a fresh run. Let's confirm the final state of the repository before submitting.Assistant
Summary
Checklist review:
- Reproduced the error: I extensively attempted to reproduce the exact issue (import of used only inside a generic type argument of an clause being incorrectly removed) using ~30 variations — the literal reproduction from the issue with the real Guava jar on the classpath, nested/deep generics, wildcards, bounds, arrays, records, enums, inner classes, static imports, and even an ~2024-09 snapshot of close to the reported version (8.31.1). None of these reproduced a live failure in this repository's current state — the historical fix for Parameterized-type handling (2021, issue #1128) combined with 's automatic recursive AST traversal already covers the exact scenario from the issue. However, this investigation exposed a real structural gap: the code that walks a type's type arguments to register them in only unwrapped one level (via ) and had no explicit handling for bounds or element types — meaning it relied entirely on happening to flatten every nested generic shape as its own top-level entry, which is not a documented/guaranteed invariant of that class, and is exactly the pattern that caused the historical "generics only" bug.
Assistant
This is my final change. Let's submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.