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?
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 understanding the issue:
RemoveUnusedImportsis removing imports that are used in Generics only. In the example,BloomFilteris used inAtomicReference<BloomFilter<T>>but the import is being removed because the recipe thinks it's unused.
Let me explore the repository structure first to understand how it's organized and find the relevant code.
The issue is about
org.openrewrite.java.RemoveUnusedImports- this is likely in therewrite-javamodule.Let me start by exploring the repository structure.
Assistant
Let me look at the main source file for RemoveUnusedImports and the test file.
Assistant
Now let me look at the test file and understand how types are collected. Let me also check the
DoesNotUseTypesearchAssistant
Let me look at
TypeUtils.asFullyQualifiedand understand howgetTypesInUseworks.Assistant
Let me look at the asFullyQualified method.
Assistant
Let me also look at the DoesNotUseType test and related code.
Assistant
Let me look for tests related to generics in the test file.
Assistant
Let me reproduce the issue by creating a simple test. First, let me understand the build system
Assistant
Now let me look at the exact code section that handles type parameters in `RemoveUnusedImports.java
Assistant
Let me just look at the relevant section of the code using sed.
Assistant
Now I understand the issue. Let me verify by looking at how
JavaTypetypes are structured in this codebaseAssistant
Let me look at how
getTypesInUseworks to understand what types are collectedAssistant
Let me look at the
FindTypesInUseclassAssistant
Let me verify the class hierarchy of
JavaType.ParameterizedAssistant
Let me look at the
ParameterizedclassAssistant
Let me look more closely at how
visitTypeis called for type parameters. Let me check the Java visitor patternAssistant
Let me look at the
visitTypemethod and thevisitParameterizedTypemethod in the visitor.Assistant
Let me look at the
visitParameterizedTypemethod and also atvisitIdentifierto understand when types are visited.Assistant
Let me look at J.ParameterizedType
Assistant
Let me look at the
ParameterizedTypeclass inJ.java.Assistant
Let me look at
visitContainerto understand how it handles nested parameterized typesAssistant
Let me look at the
visitContainermethodAssistant
Let me look at the
visitContainermethod.Assistant
Let me trace
visitRightPadded:Assistant
Let me look at the
visitRightPaddedmethod definition.Assistant
Let me look at this method.
Assistant
Let me look at `visitAndCast
Assistant
Let me find the definition of
visitAndCast.Assistant
Let me look for visitAndCast in the parent classes
Assistant
Let me look at the visitAndCast method.
Assistant
Let me read the full non-static import checking code more carefully
Assistant
Let me read more of the code
Assistant
Let me try to actually reproduce the error by creating a test. First, let me check how tests are run
Assistant
Let me create a test to reproduce the issue. Let me first add a test to the test file.
Assistant
Assistant
Let me create a test script to reproduce the issue. First, let me check if I can create a test file in the test
Assistant
Let me look at the
NoMissingTypesprecondition and how unknown types affectAssistant
Let me look at
NoMissingTypes.Assistant
Let me look at the existing test for
List<Integer>patternAssistant
Let me check the git log for recent changes to `RemoveUnusedImports.java
Assistant
Let me look for tests that might already cover the nested generic pattern
Assistant
Let me add a test to reproduce the issue
Assistant
Let me add a test to reproduce the issue. Let me first look at the very end of the test file
Assistant
Let me add a test to reproduce the issue. I'll add it to the test file.
Assistant
Let me try the edit command with the correct syntax.
Assistant
Let me check the result
Assistant
The file is messed up. Let me restore it and try a different approach.
Assistant
Let me verify the file and use a better approach to add the test.
Assistant
Let me use the
replacecommand to add the test properly.Assistant
Let me use a more careful sed command to add the test.
Assistant
Let me try python instead.
Assistant
Let me use awk to add the test.
Assistant
Let me verify the result
Assistant
Let me run just this test to see if it passes or fails
Assistant
Let me check the current version of the repo
Assistant
Let me look at the gradle.properties file
Assistant
Let me check the version
Assistant
Let me check the build files
Assistant
Let me check the buildSrc for version information
Assistant
Let me check the git tag for version information
Assistant
Let me check the Unknown
Assistant
Let me create a more targeted test. Let me think about what might be different. Maybe the issue occurs when the import type appears only in a generic type argument position and not as a standalone reference or in extends/implements.
Let me also look more carefully at the code path. Let me look at
collectUnqualifiedTypeNamesand understand itsAssistant
Let me check if there are any existing issues or recent fixes related to this
Assistant
Let me look at
visitIdentifierin `JavaVisitorAssistant
Let me look at visitIdentifier in JavaVisitor.
Assistant
Let me search for any reference to issues about generics/unused imports in the codebase
Assistant
Let me look at
FindMissingTypesto understand the parameter.Assistant
Let me look at FindMissingTypes.
Assistant
Let me look at FindMissingTypesVisitor.
Assistant
Let me check if JavaIsoVisitor overrides visit
Assistant
Let me check
fullyQualifiedNamesAreEqualAssistant
Let me look at
fullyQualifiedNamesAreEqual:Assistant
Let me check
getTypeName()on J.ImportAssistant
Let me look at the import's
getTypeName():Assistant
Let me check
Unknown.getFullyQualifiedName()Assistant
Let me check if Parameterized overrides getPackageName()
Assistant
Let me check if Parameterized overrides getPackageName().
Assistant
Let me take a different approach. Let me look at whether there's an issue in how the code handles the
Parameterizedtype's type parameters. Let me look more closely at the inner for loop and see if there's a missing recursion.Assistant
Let me look at
FullyQualified.getTypeParameters()(line 565).Assistant
Let me verify the test infrastructure works by adding a test that expects imports to be removed. Let me also check the existing tests to confirm the pattern
Assistant
Let me look at the
removeNamedImportAssistant
Let me look at the commit
330504939which seems related to theAssistant
Let me check if JavaIsoVisitor overrides visitParameterizedType or has any
Assistant
Let me check visitExpression
Assistant
Let me look at visitExpression
Assistant
OK, I've spent a lot of time analyzing. Let me try a more practical approach. Let me look at whether there's a missing recursive handling of nested parameterized types in the type parameter processing loop. Even if the exact bug is hard to reproduce, I can see that the type parameter processing code doesn't recursively handle nested parameterized types.
Let me look at the original code more carefully once
Assistant
Let me search the test file for nested generics patterns more systematically
Assistant
Let me try another test scenario - type used only as a nested generic argument:
Assistant
Let me verify the test file and run both new tests
Assistant
The file is malformed. Let me restore it and try a cleaner approach.
Assistant
Let me use a cleaner approach. I'll create a separate test file to reproduce the issue.
Assistant
Now let me run this test
Assistant
Let me look at the most recent commit to RemoveUnusedImports to see what it fixed
Assistant
Let me see the diff for the latest RemoveUnusedImports commit
Assistant
Let me try to reproduce the exact issue scenario with
BloomFilterAssistant
Let me run this test
Assistant
Now I have a clearer picture. Let me look at potential issues. The key area is the type parameter processing loop. When a type parameter is itself a
Parameterizedtype, we should also add its base type totypesByPackage, not just theParameterizedtype itself. Let me also check if there's a recursive issue with nested parameterized types.Let me look at the full context of the types collection loop:
Assistant
Let me look at
toFullyQualifiedNameto understandAssistant
Let me look at `toFullyQualifiedName
Assistant
Based on my thorough analysis, I believe the issue is in the type parameter processing loop. When a type parameter is itself a
Parameterizedtype (likeBloomFilter<T>insideAtomicReference<BloomFilter<T>>), the code only adds theParameterizedtype itself totypesByPackage, but doesn't extract and add its base type. While theParameterized.getFullyQualifiedName()delegates to the base type, there can be edge cases where the type needs to be tracked in its base form.Let me also check an important thing - the
getTypeParameters()onParameterizedtypes. These are the type ARGUMENTS. Let me verify that the code handles the case where a type argument is itselfParameterizedand recursively add its base type.Let me make the fix:
Assistant
Let me check if JavaIsoVisitor overrides visitExpression
Assistant
Let me check how type parameters/bounds are visited
Assistant
Let me look at `visitTypeParameter
Assistant
Let me check ShallowClass which might be relevant
Assistant
Let me look at
ShallowClass:Assistant
Let me search for all test methods in the RemoveUnusedImportsTest file
Assistant
Let me look at the test for issue #7333 and nearby tests:
Assistant
Let me search for tests involving
Map.Entry:Assistant
Let me look at the
javaUtilMapEntrytestAssistant
Let me create a test for this potential bug with inner classes
Assistant
Let me run this test
Assistant
Let me understand the
getPackageName()behavior for inner classesAssistant
I found a potential bug: the
unqualifiedTypeNames.contains(elem.getTypeName())check usesSet.contains()which doesn't handle the$vs.difference in inner class FQNs. While this doesn't affect the specificBloomFiltercase (which is a top-level class), it could cause issues with inner classes. But more importantly, let me focus on the core issue.Let me clean up my test files first and then look at a more targeted fix.
Assistant
Let me look at the full RemoveUnusedImports file to see if there's something I missed
Assistant
Let me see the full file at once
Assistant
After extensive analysis, I believe the issue is in the type parameter processing loop. When a type parameter is itself a
Parameterizedtype (likeBloomFilter<T>insideAtomicReference<BloomFilter<T>>), the code adds only theParameterizedtype totypesByPackagebut doesn't also add its base type. While the outer loop typically processes the nested type separately when it's intypesInUse, there may be edge cases where this doesn't happen.Let me enhance the type parameter processing to also handle nested parameterized types recursively:
Assistant
Let me use replace with a simpler approach.
Assistant
Let me try using sed to make the change
Assistant
Let me use a bash script to make the change precisely
Assistant
Let me verify the change
Assistant
Good, the change looks correct. Now let me run the existing tests to make sure nothing is broken
Assistant
All tests pass. Let me clean up and verify the final state
Assistant
Let me verify the final diff
Assistant
There's an unintended change to settings.gradle.kts. Let me restore it.
Assistant
Now let me verify the final diff
Assistant
Let me run all RemoveUnusedImports tests one more time to confirm
Assistant
The change looks good. Let me run one more comprehensive test to make sure everything passes
Assistant
All tests pass. Let me now submit the change
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.