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 exploring the repository structure to understand the
Assistant
Let me explore the rewrite-java directory since the issue is about Java imports.
Assistant
Let me see more of the file
Assistant
Now let me look at the test for RemoveUnusedImports to understand the test structure:
Assistant
Let me look for similar tests that use generics:
Assistant
Let me write a script to reproduce the issue first:
Assistant
Let me look at how tests are organized and look at the OrderImports for context:
Assistant
Now let me see how the existing test cases use generics:
Assistant
Let me look at the existing test cases for generics:
Assistant
Let me first write a reproduction test to understand the issue better. Let me find a related test for the issue:
Assistant
me look at the ParameterizedType structure:
Assistant
Let me create a temporary test script to reproduce the issue. First, let me check the Java test format more carefully:
Assistant
Let me create a temporary test file to reproduce the issue:
Assistant
I need to put the test file in the right location to be compiled with the existing project. Let me copy it:
Assistant
me fix the compilation error:
Assistant
confirmed the bug. The recipe is removing the
BloomFilterimport even though it's used in generics. Let me update the test to show the actual behavior:Assistant
The test environment may not have the BloomFilter class on classpath. Let me check the test infrastructure:
Assistant
me debug by adding some test prints to see what's happening:
Assistant
Let me see the full test output:
Assistant
go back to the original test and add a debug println to understand the flow:
Assistant
Hmm, the test reports "Recipe was expected to make a change but made no changes." which means the recipe did NOT make a change. So with the latest code, the test environment doesn't reproduce the issue. But the user reports the issue is occurring.
Wait, this is interesting. The test says "expected to make a change but made no changes" - which means the expected (after) state shows changes should occur, but no changes actually occurred. The test expected the import to be removed, but the recipe did NOT remove it.
Hmm... wait, the expected
afterstate I provided was the broken state (import removed). The test is saying it expected to make a change, but no change was made. So the current code does NOT remove the import. But the user reported it IS removing the import.This could mean:
- The bug has been fixed since v8.31.1
- The bug doesn't reproduce in the test environment for some reason
Let me check the git log to see what's been done recently:Let me check if the issue was recently fixed:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /rewrite && git log --oneline --all -- rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java | head -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try again with proper format
Assistant
Let me re-check the AST structure for ParameterizedType:
Assistant
me check the latest commits and look at the issue more carefully:
Assistant
Let me check what version corresponds to the recent fix:
Assistant
Let me look at the issue more carefully. Maybe the test environment is not exactly the same as production. Let me check the test that was working before my fix to see if I can reproduce the original bug:
Assistant
Let me look at existing tests that exercise the same logic:
Assistant
Let me add debug prints to understand what's happening:
Assistant
Let me add debug printing to understand the flow:
Assistant
me add some debugging to the visitor:
Assistant
Let me revert and add proper debug:
Assistant
me fix the order:
Assistant
DEBUG prints didn't appear. Let me force recompile:
Assistant
Let me check OrderImports:
Assistant
Let me remove the debug statements and try a more focused test:
Assistant
Let me check the recent commits and look for related issues:
Assistant
Let me look at how the type attribution happens for parameterized types. Let me check what happens when the type is unknown:
Assistant
Let me look more carefully at the test output. Maybe I'm not understanding the bug correctly:
Assistant
Let me check the current state of the code to see if there are any other edge cases that might be problematic:
Assistant
Let me check the NoMissingTypes precondition:
Assistant
Let me test with different orderings or scenarios:
Assistant
fix my test - I incorrectly used
HashMapas an unused import. Let me write a proper test:Assistant
me try a more complex case to try to reproduce the bug:
Assistant
Let me look at the test that was added for the similar bug:
Assistant
Let me search for related issues by looking at the github issue tracker:
Assistant
check if the recent fix might have introduced a regression. Let me try reverting that fix temporarily:
Assistant
Let me check if there are any related issues by searching for generic-related tests:
Assistant
me test with both OrderImports and RemoveUnusedImports together:
Assistant
me see the full output to understand:
Assistant
test the scenario when the type is not on classpath:
Assistant
Let me check what typesByPackage actually contains in the test:
Assistant
me fix the order:
Assistant
Let me revert and use a cleaner approach:
Assistant
Let me debug with OrderImports too:
Assistant
Let me restore the original code and try more scenarios:
Assistant
Let me search for issue 7407 and similar:
Assistant
Let me look at the test added for #7380 (the recent fix):
Assistant
me check what changed before the recent fixes:
Assistant
Let me look at the version tags to understand when v8.31.1 was released:
Assistant
Let me look at the code more carefully and check edge cases:
Assistant
Let me look at TypesInUse:
Assistant
Let me check the visitor for ParameterizedType:
Assistant
Let me look at how JavaVisitor handles ParameterizedType:
Assistant
Let me test some edge cases:
Assistant
FOUND IT! The test is failing. The expected state was:
Assistant
Let me try again with proper function call format
Assistant
Let me look at the GitHub issues to find a related issue:
Assistant
Let me check the issue more thoroughly. Let me look at what specific conditions might trigger the bug:
Assistant
Let me verify the code is the latest version:
Assistant
Good, the code is the original version. No debug statements. Let me also check what HEAD points to
Assistant
Let me look at what the OrderImports recipe does and how it interacts with RemoveUnusedImports:
Assistant
look at the orderImports function:
Assistant
Let me try a different scenario - maybe the issue is with class type parameter:
Assistant
Let me look at the full OrderImports test:
Assistant
Let me run the existing test suite to make sure nothing is broken:
Assistant
Let me clean up my test files and add a proper test to the existing test file:
Assistant
Let me look at the test file structure:
Assistant
Let me look at the user's specific use case more carefully. Let me try with both
OrderImportsandRemoveUnusedImports:Assistant
Let me check the actual code one more time and look for any edge cases:
Assistant
Let me check if there are any related issues in the codebase:
Assistant
Let me try the exact scenario from the issue and verify the fix:
Assistant
Let me clean up the test files:
Assistant
Let me revert the settings.gradle.kts change:
Assistant
Let me re-think this. The user reports a bug. Maybe I'm missing something. Let me look at the code more carefully and see if there's a subtle issue:
Assistant
Let me run all the RemoveUnusedImports tests one more time to confirm everything passes:
Assistant
Let me look at the actual code state and check if the fix is in place:
Assistant
Let me create one more test to verify the user's exact scenario:
Assistant
Let me check the code BEFORE the recent fixes:
Assistant
Let me try the old code to see if the bug existed:
Assistant
I need to restore my test file. Let me restore everything.
Assistant
Let me force recompile to make sure I'm using the right code:
Assistant
Let me test with old code:
Assistant
Let me restore the current code and test more scenarios:
Assistant
My tests have unused imports. Let me fix:
Assistant
Let me also test the scenario where the type is unknown:
Assistant
Found the bug! Let me look at the
NoMissingTypesprecondition:Assistant
check the isWellFormedType function:
Assistant
re-test the user's exact scenario with the classpath:
Assistant
Now let me confirm the user's scenario works and the missing type scenario is the bug:
Assistant
Now I have identified the bug! Let me apply the fix:
Assistant
Now let me apply the fix. I need to add
&& !sourceIdentifierNames.contains(qualid.getSimpleName())to the first condition:Assistant
Now let me also apply the same fix to the second check at line 269:
Assistant
I also need to apply the same fix to the fourth check (line 269):
Assistant
Now let me test the fix:
Assistant
Both tests pass! Let me run the existing RemoveUnusedImportsTest to make sure I didn't break anything:
Assistant
me check what the test expects and what the actual behavior is:
Assistant
I need to refine
collectSourceIdentifierNamesto exclude Javadoc identifiers:Assistant
Let me look at the visit pattern for Javadoc:
Assistant
Let me update
collectSourceIdentifierNamesto exclude Javadoc identifiers:Assistant
Let me look at how the JavadocVisitor is integrated:
Assistant
Let me update
collectSourceIdentifierNamesto skip Javadoc:Assistant
Let me refactor
collectSourceIdentifierNamesto skip Javadoc:Assistant
Let me change the approach and modify
collectSourceIdentifierNamesto only collect non-Javadoc identifiers. First let me see what class I need to extend:Assistant
Let me modify the approach. I'll create a named helper class or extend the visitor:
Assistant
me try overriding
getJavadocVisitorin the anonymous class:Assistant
Let me check if this compiles. The issue is the return type of
visit- let me check the JavadocVisitor.visitAssistant
The
visitmethod comes from the parent classTreeVisitor. Let me check that.Assistant
Let me compile and check:
Assistant
All 3 tests pass! Let me run all the RemoveUnusedImports tests:
Assistant
Let me look at these failing tests:
Assistant
Let me look at my current code:
Assistant
debug the Javadoc override:
Assistant
The test
retainImportIfUsedInJavaDocis failing. The expected was the import kept, but the recipe removed it.Looking at the test:
javaimport java.util.Date; import java.util.List; /** * referencing {@link Date} only in doc */ class Test { List list; }Expected: Both imports kept (Date used in Javadoc).
With my fix:
- sourceIdentifierNames - should NOT contain Date (because we excluded Javadoc)
- combinedTypes - contains Date (Date is on classpath, used in Javadoc)
- First check: combinedTypes empty → false → import kept ✓
Wait, but the test is failing. Let me check the actual output.
Let me check the full output
Assistant
Let me debug by adding a print statement:
Assistant
made a mistake. Let me revert and try again.
Assistant
Let me revert all changes and try a different approach:
Assistant
Let me think about this more carefully and try a different approach:
Assistant
Let me check the JavadocVisitor's
visitmethod signature:Assistant
Let me try overriding
visitSpaceto skip Javadoc:Assistant
The formatting is messed up. Let me revert and try again:
Assistant
Let me rewrite the whole method:
Assistant
me use a simpler approach with
replace:Assistant
Now let me apply the fix to the first check and the fourth check:
Assistant
Now let me apply the same fix to the fourth check:
Assistant
Hmm, my perl command didn't work as expected. The pattern is on multiple lines. Let me look at the actual line
Assistant
Now let me compile and run tests:
Assistant
Let me check what's happening with debug:
Assistant
Let me check by adding debug:
Assistant
me look at the test setup:
Assistant
Let me add more debug to see what's happening:
Assistant
Let me revert and use a different approach:
Assistant
Let me re-apply my fixes carefully:
Assistant
The perl multi-line replacement didn't work. Let me use a different approach. Let me use sed:
Assistant
Now let me apply the fix to the first and fourth checks:
Assistant
awk script messed things up. Let me revert:
Assistant
Let me apply my changes step by step:
Assistant
The test is still failing. Let me see the actual vs expected
Assistant
me verify my fix is in place:
Assistant
Let me revert and try a different approach - I'll add a new method to collect non-Javadoc identifiers and use that only in the first check:
Assistant
me apply the fix:
Assistant
Now let me update the first check and add the new method:
Assistant
awk is messing up. Let me revert:
Assistant
Let me use sed for precise replacement:
Assistant
Let me use the edit command:
Assistant
Now let me update the first check:
Assistant
Now let me add the new method:
Assistant
There's an extra
}at line 542. Let me fix this:Assistant
me fix this with sed:
Assistant
me apply the changes step by step using the
replacecommand:Assistant
The closing braces need fixing. Let me fix it:
Assistant
Now let me compile and test:
Assistant
RemoveUnusedImports tests pass! Let me verify the bug case is fixed:
Assistant
Let me debug:
Assistant
Let me revert and try simpler:
Assistant
check if the existing tests pass:
Assistant
All existing tests pass! Let me debug the bug case:
Assistant
FOURTH check is removing the import! Let me also add the check there:
Assistant
Now let me test:
Assistant
All IssueTest tests pass! Let me also run all RemoveUnusedImportsTest to make sure nothing is broken:
Assistant
All tests pass! Let me clean up my temporary test files:
Assistant
Let me run the full test suite for RemoveUnusedImports related tests:
Assistant
me verify my changes one more time:
Assistant
The diff looks clean and the fix works correctly. Let me check the final state once more:
Assistant
run all related tests one more time to confirm:
Assistant
All tests pass! Let me submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.