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
Now let's run this test to see if it reproduces the issue.
Assistant
There's a guava jar available in test resources classpath. Let's use
dependsOnor classpath with guava.Assistant
Good, restored. Now let's test simple extends without generics but explicit import to see if combinedTypes gets populated correctly.
Assistant
Good, it's clean already. Let's add debug print again.
Assistant
Let's remove the debug prints from source first, then run these new tests.
Assistant
Now let's refactor this block to recursively process nested
Parameterizedtype parameters, using a helper method.Assistant
Now let's add the helper method. Let's find a good place, e.g., near
getAmbiguousStaticImportNamesor right aftervisitCompilationUnit.Assistant
Duplicate line at 352/353. Let me fix.
Assistant
Good. Now let's check
@Nullableimport is available and compile.Assistant
I'll just remove that annotation since it's unimported, keeping the method simple.
Assistant
Compiles fine. Now let's run the full RemoveUnusedImportsTest suite (including my temp tests) to make sure nothing broke.
Assistant
Duplicate leftover lines 358-359. Let's remove.
Assistant
Need a blank line before
getAmbiguousStaticImportNames.Assistant
Good, now looks clean. Let's compile and run the full test suite again.
Assistant
Only my own incorrect test (issue9 — which was a false expectation on my part since unfolding is correct behavior) fails now.
keepInnerClassImportspasses again. Let's remove all my temporary test additions and re-run the full test suite to confirm everything is clean.Assistant
Let's remove everything from line 2545 (blank line before
@Test) to end, and close the class properly.Assistant
Now let's verify the diff on the test file is empty (fully reverted) and check the git diff overall.
Assistant
Test file is fully reverted to original. Now let's view the full diff of the actual fix.
Assistant
This is a clean, defensive refactor that recursively captures nested generic type parameters. Now let's run the full test suite for this module to make sure everything passes.
Assistant
All tests pass now with the original test file (2544 lines, no leftovers) plus this defensive fix. Let's also run the broader
rewrite-java-testsuite to be safe, and check for the other repo-modified file (settings.gradle.kts) status.Assistant
Good, builds fine without needing that settings.gradle.kts change (since gradle configuration was already evaluated/cached). Now let's run the broader
rewrite-java-testandrewrite-javatest suites to make sure nothing else broke.Assistant
Full
rewrite-java-testmodule test suite passes. Let's also runrewrite-javamodule's own tests (if any) for good measure.Assistant
Good, all passes. Let's do a final review of the diff.
Assistant
All
org.openrewrite.java.*tests pass. Let's do a final check of the complete diff and clean up any leftover temp files.Assistant
The diff is clean, containing only the fix to
RemoveUnusedImports.java. Let's go through the checklist:Assistant
Summary
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.