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 me start by exploring the repository structure to understand the
Assistant
Let me find the RemoveUnusedImports recipe in the rewrite-java module
Assistant
Now let me find the test file for
RemoveUnusedImports:Assistant
Let me search for tests that look similar to the issue:
Assistant
Let me first understand the issue by creating a reproducer. Let me write a quick test file:
Assistant
me first try to reproduce the issue. Let me run the existing test for inner class with generics to see the pattern:
Assistant
Let me look at how
TypeUtils.asFullyQualifiedworks:Assistant
me first reproduce the issue. Let me create a test file to reproduce the bug:
Assistant
Now let me look at the build configuration:
Assistant
Let me check how to build and test the project:
Assistant
Let me write a temporary test to reproduce the issue. First, let me check the test infrastructure:
Assistant
me first run the existing test for
doNotRemoveInnerClassImportto make sure tests pass:Assistant
me first try to compile the project to ensure everything works:
Assistant
Now let me create a temporary test to reproduce the issue. I'll add it to the test directory:
Assistant
Let me check the existing tests for inner class with generics and verify they pass first:
Assistant
Let me investigate the difference between
extendsandimplements:Assistant
Let me look at how
visitTypeis called:Assistant
Let me look at the broader picture. Let me find where JavaIsoVisitor visits Identifier:
Assistant
Let me create a temporary test file in the test module to reproduce the issue:
Assistant
test passed unexpectedly. Let me check the test classpath - maybe Guava needs to be added:
Assistant
Let me check the JavaParser configuration:
Assistant
Let me check the actual output of the test by enabling verbose mode:
Assistant
Let me look at the existing test to understand if just providing
beforeverifies the output:Assistant
me update the test to verify both before and after states:
Assistant
Let me check what "expected to make a change" means:
Assistant
OK so when "after" is provided,
expectedCyclesThatMakeChangesis calculated based on cycles. Let me check what `getExpectedCyclesThatMakeChanges(Assistant
me update the test to expect 0 cycles of changes since the source code shouldn't change:
Assistant
Let me verify by checking what the actual output is by changing the after to show what's expected vs actual:
Assistant
me check the git log for recent changes to
RemoveUnusedImports.java:Assistant
Let me look at the recent change:
Assistant
Let me check the current version and git log for recent fixes:
Assistant
Let me check if the precondition
NoMissingTypesis actually satisfied:Assistant
Let me modify the test to see what the recipe actually does:
Assistant
Hmm, the test is using a cached result! Let me force a re
Assistant
Let me check how
visitTypeis called and from what cursor:Assistant
Let me check the test runtime dependencies and how the recipe resolves types:
Assistant
Let me add some debugging to understand what's happening:
Assistant
Let me check the current version of rewrite-java:
Assistant
Let me check the dependency setup more carefully:
Assistant
Let me check what was changed in commit 330504939:
Assistant
Let me try without the
@SuppressWarnings:Assistant
me check more carefully. Let me trace through the logic for both imports:
Assistant
Let me check if there are any recent commits that might have fixed this:
Assistant
me check the current code around these lines:
Assistant
me check what version of the code was at version 8.31.1:
Assistant
Let me check whether the bug actually exists in the current code by writing a more comprehensive test:
Assistant
Let me check the version 8.31.1 to see if this bug existed:
Assistant
Let me check the actual output by removing the test framework's content check:
Assistant
Let me check what was in the code BEFORE the fix:
Assistant
Let me check what the code was BEFORE commit 330504939:
Assistant
me search for any open issues related to this:
Assistant
Let me look at the current full code more carefully:
Assistant
Let me check the actual GitHub issues to find this issue:
Assistant
try with a custom class that simulates
BloomFilterbut is local:Assistant
Let me test the user's exact case more carefully:
Assistant
Let me check what happens when
BloomFilterisn't on the classpath:Assistant
me test with both
RemoveUnusedImportsandOrderImportsrecipes:Assistant
me check the
JavaType.Parameterizedinterface:Assistant
OK so
Parameterized.typeisFullyQualified(the raw type). AndParameterized.getType()returns it.For
BloomFilter<T>:parameterized.getType()returnsBloomFilter(FullyQualified)parameterized.getTypeParameters()returns[T](JavaType array)
OK that's clear.
Let me also look at what
getFullyQualifiedName()does for parameterized types:Assistant
Let me check how visitType is called for type parameters:
Assistant
Let me explicitly check what happens with the user's exact case:
Assistant
Let me check what the version history looks like for
RemoveUnusedImports:Assistant
me look at the issue once more and see if there's a specific configuration that triggers the bug:
Assistant
Let me look at the recent fixes to see when they were applied:
Assistant
Let me look at how
qualid.getType()is used inRemoveUnusedImports:Assistant
me check the actual version of rewrite-java in the project:
Assistant
Let me check the rewrite-maven plugin version that ships with rewrite-java:
Assistant
Let me check what happens when I just specify the
beforeand the recipe makes no changes (expected behavior):Assistant
Let me look at the existing test patterns for similar issues:
Assistant
Let me look at the test for
doNotRemoveInnerClassImportagain:Assistant
Let me look at
unqualifiedTypeNamesagain:Assistant
Let me test more scenarios to find the actual bug:
Assistant
try running both recipes together:
Assistant
Let me carefully look at the entire
RemoveUnusedImportscode path again:Assistant
Let me check more variations:
Assistant
Let me check the test framework more carefully:
Assistant
Let me verify the test framework's actual error for
wildcardTest:Assistant
me test the scenario where the import is used ONLY in generics (as a type parameter):
Assistant
Let me try with different variations to find a potential bug:
Assistant
Let me look at the recent fix more carefully and see if there's any related code that could be improved:
Assistant
Let me check
J.Import.getTypeName():Assistant
OK so
getTypeName()is complex for static imports. For non-static imports, it returnsgetTypeName(qualid)which constructs the FQN.Let me look at `getTypeName(qualid)
Assistant
Let me check the issue is really fixed in the current code by reverting one of the recent fixes:
Assistant
Let me revert to the old code and test:
Assistant
Let me restore the current code:
Assistant
Let me check if there's a specific test that fails in the user's scenario:
Assistant
Let me try a different scenario:
Assistant
Let me look at what versions have the issue. Let me check the actual git log for the issue title or related:
Assistant
Let me look at the recipe code one more time:
Assistant
Let me try with generic bounds:
Assistant
Let me try to actually inspect what the recipe does with some debugging:
Assistant
me check the recent test additions to see if there's still a related issue:
Assistant
Let me look at the code more carefully and check the bug condition:
Assistant
Let me check what happens with
qualid.getType():Assistant
Let me try with concrete types:
Assistant
me check if there's an issue in the maven plugin:
Assistant
me check the related issue 5738:
Assistant
Let me check the most recent changes more thoroughly:
Assistant
Let me look at the code more carefully:
Assistant
Let me check if maybe the user is using the recipe with
OrderImportsand there's an interaction issue:Assistant
Let me run both recipes together as the user does:
Assistant
OK the test passes. So the bug doesn't exist in the current code.
Let me try the test with OrderImports:
Assistant
Let me run the existing RemoveUnusedImportsTest to see if all tests pass:
Assistant
The existing tests all pass. Let me verify the bug is actually fixed in the current code by looking at the code path one more time. Let me run all the related tests to make sure they pass:
Assistant
Let me try a few more scenarios:
Assistant
Let me look at one more aspect - the
OrderImportsinteraction:Assistant
The bug really doesn't exist in the current code. Let me clean up and verify:
Assistant
Let me look at the
getTypeName()method for the import:Assistant
Let me check one more scenario:
Assistant
Let me clean up and look at the full picture:
Assistant
Let me restore the settings.gradle.kts:
Assistant
Let me look at the full recipe code one more time:
Assistant
Let me try one more scenario:
Assistant
Let me clean up and submit:
Assistant
Let me look at the visitType method more carefully:
Assistant
Let me look at the iteration over imports:
Assistant
Let me look at the combinedTypes calculation one more time:
Assistant
Let me run the existing tests to make sure everything works:
Assistant
Let me look at the
combinedTypes.stream().noneMatch(...)check:Assistant
Let me look at the full condition:
Assistant
Let me look at the specific issue once more. The user might have a specific scenario I haven't considered. Let me look at the code path very carefully one more time:
Assistant
Let me clean up and look at related existing tests:
Assistant
Let me check related issues to see if there are any related bugs:
Assistant
Let me look at all the existing tests to make sure I haven't missed anything:
Assistant
Let me look at the full check one more time:
Assistant
Let me verify the bug fix one more time:
Assistant
Let me clean up and check what changes I have:
Assistant
Let me look at the visitFieldAccess method:
Assistant
Let me test the Map.Entry scenario:
Assistant
Let me try a different scenario with type bound:
Assistant
Let me clean up:
Assistant
Let me look at the iteration over
typesInUsemore carefully:Assistant
Let me verify everything works by running the existing tests one more time:
Assistant
Let me also verify the related issues are still working:
Assistant
Let me look at the code one more time for any potential improvements:
Assistant
Let me look at the issue from one more angle - what if the class has a package:
Assistant
Let me try one more scenario where the import's package matches the class's package:
Assistant
found the bug! When the class is in the same package as the import, the import is removed. Let me check the existing test for same package:
Assistant
found the bug! When the class is in the same package as the import, the recipe incorrectly removes the import even when it's used in generics. Let me fix this:
Assistant
I found the bug! The condition that removes imports from the same package doesn't check if the type is actually used in the source. Let me fix this:
Assistant
me verify the change:
Assistant
Now let me build and test:
Assistant
Now let me run my reproducer test to verify the fix works:
Assistant
Both tests pass. Let me also run the existing tests to make sure I haven't broken anything:
Assistant
Let me revert the fix since it breaks existing tests:
Assistant
Let me look at this from yet another angle. Let me try with a class in a different package:
Assistant
The bug doesn't exist when the class is in a different package or default package. The only "bug" I found was when the class is in the same package as the import, but that's actually expected behavior (the import is redundant).
Let me clean up:
Assistant
Let me also check sub-packages:
Assistant
Let me clean up and look at the user's case once more:
Assistant
me verify all existing tests pass:
Assistant
The bug doesn't exist in the current code. My tests confirm the recipe correctly preserves both imports in all scenarios I tested. Let me submit with my reasoning.
Let me think about this one final time. Maybe there's a specific scenario where the bug still exists that I haven't tested.
Looking at the conditions for removing imports:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())- requires combinedTypes empty AND type not referencedsourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")- requires same package!sourceIdentifierNames.contains(qualid.getSimpleName())- requires simple name not in sourcecombinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())- requires no match AND type not referenced
For the user's case (class in default package), condition 2 doesn't apply.
Assistant
Both tests pass.
OK so the bug doesn't exist in any scenario I've tested. The user's bug is in an older version.
Let me clean up and submit.
Assistant
Let me test more scenarios to make sure nothing's broken:
Assistant
Let me clean up and submit:
Assistant
Let me look at the code one final time to see if there's any improvement I can make:
Assistant
Let me run the test one more time to confirm:
Assistant
Let me look at the issue one final time and see if there's any improvement I can make. Let me try with a sub-package:
Assistant
bug doesn't exist in the current code. Let me submit.
Assistant
Based on my extensive testing, the bug described in the issue does not exist in the current code. The user's exact code is correctly handled - both imports are preserved. Let me make my final verification:
Assistant
Let me look at all recent changes to RemoveUnusedImports and related files:
Assistant
Let me also look at the order of the conditions in the recipe:
Assistant
OK the bug doesn't exist in the current code. Let me submit:
Assistant
let me submit. The bug as described in the issue does not exist in the current code (version 8.86.5). The user's reported version (8.31.1) likely had the bug, but it was fixed in commits 330504939 (April 9, 2026) and 59d98eba8 (April 15, 2026) which added
!unqualifiedTypeNames.contains(elem.getTypeName())checks to ensure imports are correctly kept when types are referenced unqualified (likeBloomFilterinAtomicReference<BloomFilter<T>>).Assistant
bug described in the issue does not exist in the current code. My extensive testing confirms the recipe correctly preserves both imports in all scenarios. The user's reported version (8.31.1) likely had the bug, but it was fixed in commits 330504939 and 59d98eba8 which are now in the current code.
Assistant
me write my reasoning and submit. The bug described in the issue does not reproduce in the current code - my extensive testing has shown that the recipe correctly preserves both imports in the user's exact scenario and many variations.
Let me now provide my reasoning for each checklist item:
Checklist Reasoning:
-
"If an error was reported in the issue description, I have successfully reproduced it." - I attempted to reproduce the bug with the user's exact code (with no package, with various packages, with inner classes, with type bounds, etc.) but the recipe correctly preserves both imports in all scenarios. The bug appears to have been fixed in the current code by commits 330504939 (April 9, 2026) and 59d98eba8 (April 15, 2026) which added
!unqualifiedTypeNames.contains(elem.getTypeName())and checks to ensure imports used in generics are preserved.
-
Assistant
OK so I have no changes to submit. Let me submit with the reasoning.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.