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.
xml<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?
javaimport 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?
javaimport 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?
java Assistant
Let me start by understanding the issue. The problem is that
RemoveUnusedImportsis removing imports that are used in generics only. In the example:javaimport com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }The import for
BloomFilteris being removed even though it's used inBloomFilter<T>within the generic type parameter. The expected behavior is that this import should be kept.Let me explore the repository structure and find the relevant code.
Assistant
Let me find the RemoveUnusedImports recipe in the codebase.
Assistant
Let me look at the relevant source files.
Assistant
Let me try the command properly.
Assistant
Let me look at the
TypesInUseclass to understand how types are collectedAssistant
Let me look at the TypesInUse class.
Assistant
Let me look at the test file for RemoveUnusedImports:
Assistant
Let me search for test cases related to generics/parameterized types in the test file
Assistant
Let me look at the
DoesNotUseTypeTest.javaand also search for relevant patterns.Assistant
Let me look at the specific test "leaveImportIfRemovedTypeIsStillReferredTo" and see what it tests:
Assistant
Let me search for relevant test cases and understand the issue better.
Assistant
Let me first try to reproduce the issue. I'll create a temporary test file
Assistant
Let me look at if there are tests involving generic type parameters in extends/implements clauses
Assistant
Let me look at the test context around line 1723
Assistant
Let me check the git log for the RemoveUnusedImports file
Assistant
Let me look at the commit that might be related
Assistant
Let me look at the diff for this commit
Assistant
Let me run the existing tests to understand the test infrastructure
Assistant
Let me write a quick test to reproduce the issue
Assistant
Let me look at the build configuration to understand the project
Assistant
Let me try to run the existing RemoveUnusedImports tests
Assistant
Let me look at the git history for the
collectUnqualifiedTypeNamesmethodAssistant
Let me check the commit that introduced `collectUnqualifiedTypeNames
Assistant
Let me try to write a unit test that reproduces the issue and run it. First let me figure out the right test structure
Assistant
Let me run this test to see what happens.
Assistant
Let me check the version of the codebase
Assistant
The test passes. Let me try with a simpler, JDK-only scenario that shouldn't need any external definitions
Assistant
Let me create a more accurate reproduction by using an inner class import used only in a generic parameter
Assistant
Let me run this test.
Assistant
Let me look at the J.java tree structure for ClassDeclaration
Assistant
Let me look at the ClassDeclaration structure
Assistant
Let me look at
J.ParameterizedTypestructureAssistant
Let me look at ParameterizedType
Assistant
Let me check the git tags to find version 8.31.1
Assistant
Let me look at the version
Assistant
Let me look at the full non-static import condition chain
Assistant
Let me look at the file around the relevant section
Assistant
Let me use cat with line numbers instead.
Assistant
Let me look at the non-static import logic
Assistant
Let me look at the
TypesInUseTestto understand how types are collectedAssistant
Let me check the JavaType.Parameterized class
Assistant
Let me check what
getTypeName()returns for an importAssistant
Let me look at
getTypeName()in J.ImportAssistant
Let me check git log for when
collectUnqualifiedTypeNameswas introducedAssistant
Let me look at how the visitor handles ParameterizedType
Assistant
Let me look at the visitParameterizedType implementation
Assistant
Let me look at how
visitTypeis implementedAssistant
Let me check
visitTypeimplementation in JavaVisitorAssistant
Let me search for related issues in the codebase
Assistant
Let me look at the test file more carefully for relevant test cases.
Assistant
Let me look at the test for issue 7380 (the most recent fix)
Assistant
Let me look at the test for 7380
Assistant
Let me check what
NoMissingTypesdoesAssistant
Let me look at the NoMissingTypes file
Assistant
Let me clean up and create a more targeted test. Let me first remove my test files
Assistant
Let me search more specifically for tests that use
<T>inside extends/implementsAssistant
Let me look for more test patterns
Assistant
Let me look at how JavaVisitor handles
Assistant
Let me look at the visitClassDeclaration method
Assistant
Let me write a more specific test for types used only as type arguments
Assistant
Let me run this test.
Assistant
Let me check what
getFullyQualifiedNamereturns for Parameterized typesAssistant
Let me check how
getFullyQualifiedNameworks for ParameterAssistant
Let me check the TypeUtils.asFullyQualified method
Assistant
Let me check the asFullyQualified method
Assistant
Let me look at the recent test added by commit 330504939
Assistant
Let me check
getSimpleName()on JAssistant
Let me check J.FieldAccess.getSimpleName()
Assistant
Let me check if the
visitExpressioninFindTypesInUsemight be relevantAssistant
Let me look at getFieldType in J.Identifier
Assistant
Let me look at J.Identifier's fieldType
Assistant
Let me check the JavaType hierarchy to see if Unknown extends FullyQualified
Assistant
Let me try a more detailed test that checks typesInUse to understand what's happening
Assistant
Let me run this debug test.
Assistant
Let me check when
collectUnqualifiedTypeNameswas first introducedAssistant
Let me try git blame to find when the method was introduced
Assistant
Let me run more targeted tests to check if there's a case where the import is wrongly removed
Assistant
Let me run this test
Assistant
Let me clean up my test files
Assistant
Let me test a case where type is used only as a type bound in
Assistant
Let me run this test
Assistant
Let me run the full RemoveUnusedImports test suite to see if all tests pass
Assistant
Let me look at the DoesNotUseType test for reference
Assistant
Let me check what
getFullyQualifiedNamereturns for inner classesAssistant
Let me check
getFullyQualifiedNamefor the base FullyQualified classAssistant
Let me check how
getFullyQualifiedNameis implemented for JavaType.ClassAssistant
Let me check
getFullyQualifiedNameforJavaType.ClassAssistant
Let me check if Class overrides getFullyQualified
Assistant
Let me check Class for getFullyQualifiedName method
Assistant
Let me look at the full Class definition for getFullyQualifiedName
Assistant
Let me check for @Getter on Class
Assistant
Let me do one more search to see if there are any clues
Assistant
Let me run the full test suite one more time to confirm
Assistant
Let me check the most recent commit to RemoveUnusedImports
Assistant
Let me see the diff for this commit
Assistant
After extensive analysis, I've found that the current codebase already correctly handles the reported issue. The tests I created all pass, and the existing test suite for
RemoveUnusedImportsalso passes completely.Let me verify one more time that the specific scenario from the issue is handled:
Assistant
Let me run this test.
Assistant
The test passes, confirming the issue scenario works correctly. Let me clean up and check if there are any remaining issues to fix
Assistant
Let me check git status to confirm no unintended changes
Assistant
Let me check what changed in settings.gradle.kts
Assistant
Let me restore the original settings.gradle.k
Assistant
The codebase already handles this issue correctly. Let me verify the git status is clean and submit.
Assistant
After thorough analysis, I've confirmed that the issue described (imports being removed when used only in generics) is already handled correctly by the current codebase. The recent commits (particularly
59d98eba8and330504939) added proper checks viacollectUnqualifiedTypeNamesandcollectSourceIdentifierNamesthat verify types actually appear in source code before removing their imports.My reproduction tests all pass, confirming:
- Types used only as generic type arguments in extends clauses are correctly detected
- Types used only as type arguments to parameterized types are correctly detected
- Types used only in type bounds are correctly detected
The code is clean and the existing test suite passes. Since there are no changes needed, I'll submit.
Assistant
Let me check the current version
Assistant
Let me check the build file for version
Assistant
Let me check for version information
Assistant
I've thoroughly investigated this issue. After extensive analysis and testing, I can confirm that the current codebase already correctly handles the reported scenario. The issue "RemoveUnusedImports removes imports when used in Generics only" appears to have been resolved by recent commits, particularly:
- Commit
59d98eba8- AddedunqualifiedTypeNameschecks to prevent false removal of imports - Commit
330504939- AddedsourceIdentifierNamescheck to prevent incorrect import retention/removal
My tests confirm that types used only in generic type arguments (like
BloomFilter<T>inextends AtomicReference<BloomFilter<T>>) are correctly detected and their imports are preserved. Let me submit the current- Commit
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.