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 callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /rewrite, current file: none) bash-$
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 a Java recipe
Assistant
let me look at the test file to understand the testing patterns:
Assistant
Let me first create a reproduction script to confirm the issue:
Assistant
Now let me find an example of a test case to understand how to write a test reproducer:
Assistant
Let me check the JavaIsoVisitor structure to better understand how the type attribution works:
Assistant
Let me trace through this issue more carefully:
Assistant
write a test case to reproduce the issue:
Assistant
Let me first set up a reproduction script:
Assistant
Let me first build the project to see if everything works:
Assistant
Let me write a reproduction test directly using the test framework. First, let me write a test case to add to the existing test file to verify the bug:
Assistant
Let me write a reproduction script that uses the existing test infrastructure:
Assistant
Let me look at how other tests are structured:
Assistant
Let me build the project first:
Assistant
Let me create a temporary test that reproduces the issue:
Assistant
test passed, which means the imports are kept. But wait, let me check more carefully. Let me check the test results to make sure:
Assistant
Let me check what actually happens by adding an "after" parameter that expects the imports to remain:
Assistant
Let me look at how the test framework checks the changes. The "before" and "after" are exactly the same in my test - maybe there's some normalization:
Assistant
OK, so the issue is that
expectedCyclesThatMakeChangesis set based on whether the source files differ before/after. Let me check:Assistant
Let me check the SourceSpec comparison logic:
Assistant
Let me modify the test to make sure the behavior is correct - if no change is expected, we don't provide an
after:Assistant
Let me write a custom test that prints the actual transformed source to confirm the bug:
Assistant
The
afterRecipemethod might be on a different class. Let me check:Assistant
The
SourceSpecs.javainterface doesn't haveafterRecipe. Let me look atAssertions.javato see how it returns the `SourceSpecsAssistant
OK so
java()returns aSourceSpecactually. Let me update the test:Assistant
Let me look at the actual structure more carefully:
Assistant
Let me update the test - the consumer should be modifying the SourceSpec, not consuming a J.CompilationUnit:
Assistant
The recipe is keeping the BloomFilter import! Let me confirm by writing a test that expects the import to be removed (this should FAIL because the current behavior is correct):
Assistant
Let me check the test framework logic more carefully:
Assistant
me check the git log for changes to RemoveUnusedImports.java:
Assistant
me check if this bug is already fixed:
Assistant
Let me look at the test that was added for the bug fix in 330504939:
Assistant
OK so the recent fix added a new check
!sourceIdentifierNames.contains(qualid.getSimpleName())that removes an import if the simple name doesn't appear in source identifiers.For our case:
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }The
BloomFilteridentifier IS in the source code (inBloomFilter<T>). SosourceIdentifierNames.contains("BloomFilter")istrue. So the import should be kept.But the test I wrote fails with "Recipe was expected to make a change but made no changes" - meaning the recipe didn't make any changes (i.e., the import was kept). This is the CORRECT behavior!
So why is the test failing? Because I provided an
afterparameter that's different frombefore. The framework expects at least one cycle of changes.But wait, the test logic should also check that the final result equals the
after. Let me check this:Assistant
me write a clearer test that verifies the bug IS fixed:
Assistant
The test passed! The bug seems to be fixed in the current code. But let me think about edge cases and verify the fix handles them properly:
Assistant
Let me verify by exploring the AST structure for this case:
Assistant
me run the test with debug output to actually see what's happening:
Assistant
Let me trace through the logic more carefully to understand if there's any edge case:
Assistant
me look at all recent fixes related to RemoveUnusedImports:
Assistant
Let me look at PR #7383 which was a recent fix:
Assistant
Let me check if there have been releases between v8.31.1 and now:
Assistant
The bug seems to be fixed. Let me verify by running the existing test suite to make sure nothing is broken:
Assistant
Let me check the GitHub issue tracker for related issues by looking at the recent test cases added:
Assistant
Let me check if there's still an issue with similar cases that aren't fully handled:
Assistant
Let me think about edge cases. Let me write a more comprehensive test that covers various scenarios where imports might be used only in generics:
Assistant
All test cases passed. So the bug IS fixed for all these cases in the current code.
Let me check the test results:
Assistant
Let me look at how ParameterizedType is parsed and what its
clazzfield looks like:Assistant
Let me look at how existing tests are structured to understand the right approach:
Assistant
me test more edge cases to find if there's a remaining issue:
Assistant
check what happens if
BloomFilteris not on the classpath:Assistant
I found the bug! When the type is not on the classpath (e.g.,
some.unknown.BloomFilter), the type attribution is missing, causing the import to be incorrectly removed. Let me look at the code more carefully:Assistant
Now I understand the bug. Let me check the second test case more carefully:
Assistant
Now I'll apply the fix to
RemoveUnusedImports.java:Assistant
Let me apply the fix to RemoveUnusedImports.java:
Assistant
The fix is applied. Let me run the test to confirm:
Assistant
me check the error:
Assistant
Let me check the file to see if my edit was applied:
Assistant
Let me look at the second check that also fires:
Assistant
I need to fix check 3 as well. Let me update it to also check
sourceIdentifierNames:Assistant
Still failing. Let me check what other code path might be triggering the removal. Let me check the
getAmbiguousStaticImportNamespart - oh wait, that's only for static imports.Let me look at the full code path again:
Assistant
Let me check the
NoMissingTypesprecondition:Assistant
me add some debug output to trace what's happening:
Assistant
me try modifying the recipe to add debug output:
Assistant
Now I see the issue!
sourceIdentifierNamescontains "BloomFilter" (simple name), butelem.getTypeName()returns "some.unknown.BloomFilter" (FQN). Let me checkelem.getTypeName():Assistant
issue is that
elem.getTypeName()returns the FQN, butsourceIdentifierNamescontains simple names. Let me fix this:Assistant
Now let me remove the debug line:
Assistant
The test passes! Both imports are now correctly kept. Let me run all the original RemoveUnusedImports tests to make sure I didn't break anything:
Assistant
me check the failing test:
Assistant
The test
removeImportIfFullyQualifiedInJavaDocexpects theDateimport to be removed (only referenced in JavaDoc). My fix is keeping it becauseDateappears in the source (in JavaDoc). Let me check if JavaDoc identifiers are visited:Assistant
The JavaDoc test is failing with my fix. The issue is that
Dateappears in JavaDoc, and the visitor is includingDateas an identifier. SosourceIdentifierNamescontainsDate, and my fix prevents the import from being removed.I need to check if JavaDoc identifiers are visited. Let me think about this differently.
The visitor is
JavaIsoVisitor, which visits J nodes. JavaDoc comments are typically not represented as J nodes - they're stored asJavadocmarkers. So the visitor shouldn't visit them.But wait, the test fails! So
DateIS being kept. Let me check whatsourceIdentifierNamescontains for this case.Actually, I think I need to check if the JavaDoc comment is visited or not. Let me also see what
unqualifiedTypeNamescontains.Let me check the original test logic for
removeImportIfFullyQualifiedInJavaDoc:import java.util.Date;is the import.{@link java.util.Date}is in JavaDoc.List list;is the field.
Without my fix:
typesByPackagewould havejava.util.List(from the field type).combinedTypeswould havejava.util.List.- For
java.util.Dateimport:target = "java.util".types = typesByPackage.get("java.util") = {List, Date}? Wait, if Date is not used in the code, would it be intypesByPackage?
Hmm, let me check what
cu.getTypesInUse()includes. If the JavaDoc has{@link java.util.Date}, the type attribution forDatemight be present.Actually, looking at the commit
87ba1d25c Remove fully qualified JavaDoc references from TypesInUse for RemoveUnusedImports (#5738), this suggests that JavaDoc references WERE in TypesInUse before, and this commit removed them.So with that commit, JavaDoc references are NOT in TypesInUse. So for our case:
typesByPackageforjava.utilwould only containList.combinedTypeswould only containList.- For
java.util.Dateimport:combinedTypesis NOT empty (has List), but doesn't containDate. So noneMatch is true. !unqualifiedTypeNames.contains("Date")is true (because Date is not in source identifiers or unqualified type names).- So check 4 fires, and the import is removed.
Wait but that doesn't make sense - the original test passed before my fix. Let me re-check.
Actually, I see. The original test PASSED before my fix. So:
combinedTypeswould be{List}.- For
Dateimport:combinedTypes.stream().noneMatch(...)is true (Date not in {List}). !unqualifiedTypeNames.contains("Date")is true.- Check 4 fires, import removed.
With my fix:
- Check 4 also has
!sourceIdentifierNames.contains(elem.getClassName()). sourceIdentifierNames.contains("Date")- does it?- If the visitor visits identifiers in JavaDoc, then yes.
- If not, then no.
The test fails with my fix, so
sourceIdentifierNamesdoes containDate. So the visitor IS visiting identifiers in JavaDoc? Or maybe JavaDoc is converted to AST nodes?Let me investigate:Let me investigate how JavaDoc is represented:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[find /rewrite -name "Javadoc.java" -path "/main/" | head -5]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me investigate how JavaDoc is
Assistant
So JavaDoc has its own AST (separate from the main J AST). The JavaIsoVisitor only visits J nodes, not Javadoc nodes. So JavaDoc identifiers should NOT be visited.
But the test fails. So either:
- The JavaDoc references are being included in
sourceIdentifierNamessomehow. - My understanding of the code path is wrong.
Let me re-check by adding debug output to see what
sourceIdentifierNamescontains.Actually, wait. Let me re-read the JavaVisitor more carefully. Maybe the Javadoc nodes ARE visited via a different mechanism.
Actually, looking at the Javadoc interface, it has
acceptJavadoc(JavadocVisitor<P>). So it uses a different visitor (JavadocVisitor). The JavaIsoVisitor would not visit Javadoc nodes directly, but they might be visited through some other path.Let me check if Javadoc is stored as part of the J AST:
- The JavaDoc references are being included in
Assistant
So the JavaVisitor uses
getJavadocVisitor()to visit JavaDoc comments in spaces. The JavadocVisitor is used, but only when space is being visited. The default JavadocVisitor would just visit the Javadoc nodes but not the identifiers within them.Let me check the default JavadocVisitor:
Assistant
So
visitReferencecallsjavaVisitorVisitwhich invokesjavaVisitor.visit(tree, p)on the inner tree (which is a J.Identifier for{@link java.util.Date}).So when JavaVisitor walks the AST, it visits Javadoc, which has Reference, which contains a J tree (e.g., J.FieldAccess for
java.util.Date). The J.Identifier nodes inside Javadoc are visited by the JavaVisitor.That's why
Dateends up insourceIdentifierNames. Let me verify by adding debug output:Assistant
Dateends up insourceIdentifierNamesbecause JavaDoc references are visited. I need to differentiate between "type reference in source" and "JavaDoc reference". Let me modifycollectSourceIdentifierNamesto exclude identifiers inside Javadoc:Assistant
So I can override
getJavadocVisitor()to return a custom visitor. Let me modifycollectSourceIdentifierNames:Assistant
me remove the debug output and try a different approach. I'll override
getJavadocVisitor()incollectSourceIdentifierNamesto exclude JavaDoc identifiers:Assistant
Good, the debug output is removed. Now let me modify the visitor to exclude JavaDoc identifiers. I'll override
getJavadocVisitor()to return a no-op visitor:Assistant
Wait, let me think about this differently. The current approach uses
sourceIdentifierNameswhich includes ALL identifiers in the source (including in JavaDoc). This is too aggressive because JavaDoc references shouldn't be considered as "using" the import.I should NOT use
sourceIdentifierNamesfor this fix. Instead, I should modifycollectUnqualifiedTypeNamesto also include identifiers that are in type positions even when type attribution is missing.But that's more complex. Let me think about the simplest approach.
Actually, looking at this more carefully, the issue is that
unqualifiedTypeNamesis too restrictive (only includes types with full type attribution). When type attribution is missing (e.g., type not on classpath), the type name is not added.Let me modify
collectUnqualifiedTypeNamesto also include identifiers in type positions. The simplest way is to use the type tree structure:In Java's AST, types are represented by:
J.Identifier(simple type name)J.FieldAccess(qualified type name likejava.util.List)J.ParameterizedType(parameterized type likeList<String>)J.ArrayType(array type likeString[])
The visitor should look at these to identify type references.
Actually, the simplest fix is to override
visitParameterizedTypeand add theclazztounqualifiedTypeNamesif it's aJ.Identifierwith a FullyQualified type (or missing type attribution).Wait, but
J.ParameterizedType.clazzcould be aJ.Identifier(simple name) or aJ.FieldAccess(qualified name).Let me take a different approach. I'll modify
collectUnqualifiedTypeNamesto include identifiers that appear in type positions. The way to identify type positions is to look at the AST structure.Actually, the cleanest approach is to add identifiers when they're in the following positions:
J.ParameterizedType.clazz(when it's a J.Identifier)J.VariableDeclarations.type(or similar - field/method parameter type)J.MethodDeclaration.returnTypeJ.ClassDeclaration.extendsJ.ClassDeclaration.implements- etc.
But this requires visiting many different AST nodes.
Let me try a simpler approach: in
visitIdentifier, also add identifiers that are in type positions. A type position can be detected by checking the parent node type:- Parent is
J.ParameterizedType(asclazz) - Parent is
J.VariableDeclarations(astype) - Parent is
J.MethodDeclaration(asreturnTypeor parameter type) - etc.
Let me write this:Let me take a different approach. I'll modify
collectUnqualifiedTypeNamesto also include identifiers in type positions even when type attribution is missing:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[sed -n '465,525p' /rewrite/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
take a different approach. I'll modify
collectSourceIdentifierNamesto exclude identifiers inside JavaDoc by overridinggetJavadocVisitor():Assistant
Wait, I need to think about this differently. The issue is that
sourceIdentifierNamesincludes JavaDoc identifiers. The simplest fix is to NOT usesourceIdentifierNamesfor the check, and instead use a different mechanism.Actually, let me revert my changes and think about this more carefully.
The original bug: When
BloomFilter<T>is used as a generic type argument, the import is incorrectly removed. This happens when type attribution is missing.Looking at the code path:
combinedTypesis empty (because no type attribution forBloomFilter).unqualifiedTypeNamesdoesn't containBloomFilter(because no type attribution).
The check fires:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName()).The fix should ensure that when an identifier is used as a type reference in source code, the type name is added to
unqualifiedTypeNames(or some equivalent set).But I don't want to add identifiers that happen to be the same as a type name but aren't actually type references. For example,
String s = "hello";- heresis a variable, not a type.Hmm. Let me think about what makes a "type reference" in Java's AST.
In the AST:
J.Identifiercan be a type reference or a value reference.- The context (parent) determines which.
For our case
BloomFilter<T>:BloomFilteris theclazzofJ.ParameterizedType.- This is a type reference.
For
Date d;:Dateis the type ofJ.VariableDeclarations.- This is a type reference.
For
Date.something:Dateis the target ofJ.FieldAccess.- This is a value reference (or a type reference if it's a static method/field).
The current visitor
collectUnqualifiedTypeNamescheckstype instanceof JavaType.FullyQualified. So it only adds if the type attribution is present.Let me modify the visitor to also add identifiers in type positions even when type attribution is missing.
A simple way: check if the parent is a
J.ParameterizedType(asclazz), orJ.VariableDeclarations(as type), etc.Actually, I think the simplest fix is to check if the parent contains the identifier as a "type" by looking at the type attribution. If the parent has a type (e.g.,
J.VariableDeclarations.type), and that type's FQN matches the identifier's name, then it's a type reference.But this is getting complex. Let me try yet another approach.
Actually, looking at the original test
removeImportIfFullyQualifiedInJavaDoc, the expected behavior is:import java.util.Date;is removed becauseDateis only referenced in JavaDoc.
So
Dateshould NOT be inunqualifiedTypeNamesbecause it's only in JavaDoc.With my current fix:
sourceIdentifierNamesincludesDate(because JavaDoc references are visited).unqualifiedTypeNamesdoes NOT includeDate(because no type attribution).
So
!sourceIdentifierNames.contains("Date")is false, and my fix prevents the import from being removed.If I exclude JavaDoc from
sourceIdentifierNames:sourceIdentifierNameswould NOT includeDate.- My fix would NOT prevent the import from being removed.
So the fix is to exclude JavaDoc identifiers from
sourceIdentifierNames. Let me implement this.To exclude JavaDoc identifiers, I'll override
getJavadocVisitor()in the visitor to return a no-op visitor that doesn't visit the inner tree. This way, when the JavaVisitor encounters a Javadoc comment, it will use the no-op visitor instead of the default one.Actually, the default JavadocVisitor calls
javaVisitorVisit(r.getTree(), p)which calls back into the JavaVisitor. If we override this to do nothing, then the identifiers inside Javadoc won't be visited.Let me write this:Let me modify the visitor to exclude JavaDoc identifiers:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cat > /tmp/visitor_replacement.java << 'EOF' private Set collectSourceIdentifierNames(J.CompilationUnit cu) { return new JavaIsoVisitor<Set>() { @Override public J.Import visitImport(J.Import import_, Set names) { return import_; }
@Override public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> names) { names.add(identifier.getSimpleName()); return super.visitIdentifier(identifier, names); } @Override protected JavadocVisitor<Set<String>> getJavadocVisitor() { // Return a no-op JavadocVisitor that does not descend into the inner J tree, // so that identifiers referenced only from Javadoc are not collected. return new JavadocVisitor<Set<String>>(this) { @Override public org.openrewrite.java.tree.Javadoc visitReference(org.openrewrite.java.tree.Javadoc.Reference reference, Set<String> names) { return reference; } }; } }.reduce(cu, new HashSet<>()); }EOF echo "done"]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me take a step back and reconsider. The issue is:
- When the type is on the classpath with proper type attribution, the existing checks work correctly - the import is kept.
- When the type is NOT on the classpath (type attribution missing), the existing checks incorrectly remove the import.
- When the type is only in JavaDoc, the existing checks correctly remove the import.
My current fix using
sourceIdentifierNames.contains(elem.getClassName())breaks case 3 because JavaDoc identifiers are included insourceIdentifierNames.So I need to either:
- Exclude JavaDoc identifiers from
sourceIdentifierNames. - Use a different approach (not
sourceIdentifierNames).
Let me try option 1 - exclude JavaDoc identifiers by overriding
getJavadocVisitor():Let me take a different approach and modify the visitor to exclude JavaDoc identifiers:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[sed -n '525,545p' /rewrite/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me first look at the current state of the file:
Assistant
Let me modify
collectSourceIdentifierNamesto exclude JavaDoc identifiers by overridinggetJavadocVisitor():Assistant
Good, no Javadoc imports yet. I need to add the imports. Let me check the current imports first.
Assistant
I need to add the import for
JavadocVisitor:Assistant
Now let me update the
collectSourceIdentifierNamesmethod:Assistant
Now let me replace the method using a Python script:
Assistant
Python. Let me use perl to do the replacement:
Assistant
Now let me build and run the tests:
Assistant
Two tests are failing because they expect imports referenced in JavaDoc to be kept. Let me look at these tests:
Assistant
Let me check what
unqualifiedTypeNamescontains for the JavaDoc case:Assistant
The syntax error is because I'm printing before the second variable is defined. Let me fix the debug:
Assistant
unqualifiedTypeNamescontainsDatefrom the JavaDoc reference. Let me check why the test is failing:Assistant
me check which check is firing. The combinedTypes has Date, so check 4 shouldn't fire:
Assistant
Let me revert the
getJavadocVisitorchange and keep my original fix:Assistant
me remove the unused import:
Assistant
Let me check the
removeImportIfFullyQualifiedInJavaDoctest more carefully:Assistant
Let me add debug output to see what combinedTypes contains for the JavaDoc case:
Assistant
me check the order of code:
Assistant
The debug prints
typesByPackageBEFORE it's populated. Let me move it to after:Assistant
The DEBUG was removed by the sed. Let me re-add it.
Assistant
The test fails but no DEBUG output is shown. Let me try a different approach to see the output:
Assistant
me check if the debug line is actually in the file:
Assistant
The debug is inside the if block, so it only prints when removing. Let me move it to the top:
Assistant
me just delete the misplaced debug and add it correctly:
Assistant
Now I can see the actual data:
For
removeImportIfFullyQualifiedInJavaDoc(with my fix):combinedTypes = [java.util.List](only List, no Date - PR #5738 stripped Date)unqualifiedTypeNames = [java.util.List, <unknown>, Test2](no Date)sourceIdentifierNames = [java, util, List, list, Date, Test2](Date IS here from JavaDoc)
For
Dateimport:- Check 1:
combinedTypes.isEmpty()= false. → Doesn't fire. - Check 3 (line 266):
!sourceIdentifierNames.contains("Date")- "Date" IS in sourceIdentifierNames, so this is false. → Doesn't fire. - Check 4:
combinedTypes.stream().noneMatch(Date)= true (Date not in combinedTypes).!unqualifiedTypeNames.contains("java.util.Date")= true.!sourceIdentifierNames.contains("Date")= false (Date IS in sourceIdentifierNames).- Overall: true && true && false = false. → Doesn't fire.
- So no check fires. Import is KEPT.
But the test expects the import to be REMOVED!
So my fix is too aggressive. It keeps the Date import (which is only referenced in JavaDoc with FQN).
Now I understand the issue. Let me think about the right fix.
The original behavior (without my fix):
- For
Dateimport in JavaDoc FQN case:- Check 1:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains("java.util.Date")= true && true = true. → FIRES. Import removed.
- Check 1:
For my case (BloomFilter without classpath):
- Check 1:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains("some.unknown.BloomFilter")= true && true = true. → FIRES. Import removed. (BUG)
So both cases fire check 1. The difference is:
- JavaDoc FQN case: PR #5738 stripped Date from TypesInUse. So combinedTypes doesn't have Date.
- Generic type argument case: type attribution is missing. So combinedTypes doesn't have BloomFilter.
Both look the same. So we can't distinguish them with the existing checks.
Hmm. Let me think about this differently. The fundamental issue is:
- When type attribution is missing, we don't know if the type is actually used or not.
The JavaDoc FQN case "lies" - the type attribution is stripped, but the type IS referenced in the AST (in JavaDoc).
The generic type argument case also "lies" - the type attribution is missing, but the type IS referenced in the AST.
So we can't distinguish these cases without additional information.
But wait, the original test
removeImportIfFullyQualifiedInJavaDocexpects the import to be REMOVED. This is the test as it exists today. So the existing behavior is to remove the import for this case.The user's issue expects the import to be KEPT for the generic type argument case.
These are conflicting requirements! Unless...
Wait, maybe the issue is that the JavaDoc case is FQN reference and the generic case is short reference. Let me check:
{@link java.util.Date}- FQN reference. The type isjava.util.Date(FQN).extends AtomicReference<BloomFilter<T>>- short reference. The type isBloomFilter<T>(parameterized with type var).
Maybe the rule is:
- FQN reference in JavaDoc: REMOVE (because the FQN is sufficient).
- Short reference in source (including generic type argument): KEEP.
For my case (BloomFilter):
- The identifier is "BloomFilter" (short).
- It's used in a generic type argument.
For JavaDoc FQN case:
- The reference is
{@link java.util.Date}(FQN). - The inner J tree is J.FieldAccess.
So the difference might be:
- The reference is in Javadoc and uses FQN → REMOVE.
- The reference is in source code and uses short name → KEEP.
To distinguish:
- If the import is referenced in JavaDoc with a short name → KEEP.
- If the import is referenced in JavaDoc with FQN → REMOVE.
- If the import is referenced in source code → KEEP.
So we need to know:
- Whether the simple name is in source code (not just in JavaDoc).
- Whether the simple name is in JavaDoc.
For (1), we want to KEEP the import if the simple name is in source code. For (2), we want to KEEP the import if the simple name is in JavaDoc AND the simple name is referenced (not the FQN).
Hmm, this is getting complex. Let me think about it differently.
Actually, the simplest approach might be:
- If the import is referenced in source code (anywhere, including JavaDoc) by its simple name → KEEP.
For my case: "BloomFilter" is in source code. KEEP. For
{@link Date}: "Date" is in source code (in JavaDoc). KEEP. For{@link java.util.Date}: "Date" is in source code (in JavaDoc). But should REMOVE.Hmm, so the last case is the conflict.
Wait, maybe the rule is:
- If the import is referenced in source code (excluding JavaDoc) by its simple name → KEEP.
- If the import is only referenced in JavaDoc → depends on the reference style.
For my case: "BloomFilter" is in source code. KEEP. For
{@link Date}: "Date" is only in JavaDoc. Need to check reference style. For{@link java.util.Date}: "Date" is only in JavaDoc. Need to check reference style.The reference style:
{@link Date}- short reference. KEEP.{@link java.util.Date}- FQN reference. REMOVE.
So the rule becomes:
- If the import's simple name appears in source code (NOT in JavaDoc) → KEEP.
- If the import's simple name appears ONLY in JavaDoc:
- If referenced as short name
{@link Date}→ KEEP. - If referenced as FQN
{@link java.util.Date}→ REMOVE.
- If referenced as short name
OK this is the logic. Let me see how to implement it.
For the JavaDoc check:
- If we collect identifiers from JavaDoc, we can see which ones appear.
- If the identifier appears as a short reference (e.g.,
{@link Date}), keep the import. - If the identifier appears as a FQN reference (e.g.,
{@link java.util.Date}), remove the import.
Actually, the simpler way:
- If the import's simple name appears in source code (NOT in JavaDoc) → KEEP.
- If the import's simple name appears in JavaDoc:
- We need to check if the import's simple name appears as a standalone identifier (not just as part of FQN).
Wait, let me think about this differently. The
sourceIdentifierNamesI have includes JavaDoc identifiers. So if I want to know if the name is in source code (excluding JavaDoc), I need a separate set.Let me create two sets:
sourceCodeIdentifierNames- identifiers in source code (NOT in JavaDoc).javaDocIdentifierNames- identifiers in JavaDoc.
If the import's simple name is in
sourceCodeIdentifierNames, KEEP. If the import's simple name is injavaDocIdentifierNamesAND it's used as a short reference, KEEP. Otherwise, REMOVE.Hmm, but this requires distinguishing between short and FQN references in JavaDoc.
Actually, let me look at this from a different angle. The current code uses
unqualifiedTypeNameswhich collects FQNs from type-attributed identifiers. For JavaDoc references:- Short reference
{@link Date}: The J tree isJ.Identifier("Date"). The type might bejava.util.Date. SounqualifiedTypeNamescontains "java.util.Date". - FQN reference
{@link java.util.Date}: The J tree isJ.FieldAccess("java.util.Date"). The type attribution is stripped (PR #5738). SounqualifiedTypeNamesmight NOT contain "java.util.Date".
Wait, let me check the actual data:
DEBUG ... unqualifiedTypeNames=[java.util.List, <unknown>, Test2] ...So
unqualifiedTypeNamescontains "java.util.List" (from the field declaration), "" (maybe from the parameterized type), and "Test2" (the class name). It does NOT contain "java.util.Date".So for the FQN JavaDoc reference,
unqualifiedTypeNamesdoesn't contain "java.util.Date". This is because:- PR #5738 stripped Date from TypesInUse.
- The
collectUnqualifiedTypeNamesvisitor only adds iftype instanceof JavaType.FullyQualified. If the type is stripped, it might be null or something else.
Hmm, but the test passes without my fix, so check 1 fires:
combinedTypes.isEmpty()= true (Date not in typesByPackage).!unqualifiedTypeNames.contains("java.util.Date")= true.- Check 1 fires. Import removed. ✓
For my case (BloomFilter without classpath):
combinedTypes.isEmpty()= true (BloomFilter not in typesByPackage).!unqualifiedTypeNames.contains("some.unknown.BloomFilter")= true.- Check 1 fires. Import removed. ✗
Both cases look the same. The difference is in the user's intent.
OK, let me think about this from a higher level. The issue is that when type attribution is missing, we don't know if the type is used. The original code's heuristic is to remove the import (because missing type attribution usually means the type isn't used).
But for the generic type argument case, the type IS used (in a generic position). We just don't have type attribution.
To fix this, we need to check if the type is referenced in source code as a type (not just in JavaDoc).
Let me think about how to detect "type references in source code":
- The
J.Identifierin a generic type argument position. - The
J.Identifierin a variable type declaration. - The
J.Identifierin a method return type. - etc.
A simple way: check if the identifier's parent is a
J.ParameterizedType(asclazz).Actually, let me look at
unqualifiedTypeNamesmore carefully. It only adds iftype instanceof JavaType.FullyQualified. The issue is that this excludes identifiers with missing type attribution.What if I modify
collectUnqualifiedTypeNamesto also add identifiers in type positions (e.g., asclazzofJ.ParameterizedType)? Then it would include "BloomFilter" for my case.But this would also include identifiers in JavaDoc references, which we don't want for the FQN case.
Hmm. OK let me think about this more carefully.
Actually, wait. Let me re-read the original test
retainImportIfUsedInJavaDoc:import java.util.Date; import java.util.List; /** * referencing {@link Date} only in doc */ class Test { List list; }Expected behavior: keep
Dateimport (don't remove).Without my fix:
- Check 1:
combinedTypes- depends on whether Date is in typesByPackage. - Looking at my debug output:
combinedTypes = [java.util.List](no Date). !unqualifiedTypeNames.contains("java.util.Date")- depends.- Looking at my debug output:
unqualifiedTypeNames = [java.util.List, Test, java.util.Date](Date IS here from short JavaDoc ref). - So
!unqualifiedTypeNames.contains("java.util.Date")= false. - Check 1 doesn't fire.
- Check 4:
combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(...)- first part true, second part false. Doesn't fire. - So no check fires. Import kept. ✓
OK so for short JavaDoc ref,
unqualifiedTypeNamescontains the FQN (because the J.Identifier in JavaDoc has type attribution). For FQN JavaDoc ref,unqualifiedTypeNamesdoesn't contain the FQN (because the J.FieldAccess in JavaDoc has stripped type attribution).So the difference between the two JavaDoc cases is:
- Short ref: type attribution present on J.Identifier, so it's in unqualifiedTypeNames.
- FQN ref: type attribution stripped from J.FieldAccess, so it's NOT in unqualifiedTypeNames.
Now for my case (generic type argument without classpath):
- The type attribution is missing for the J.Identifier (because type not on classpath).
- So it's NOT in unqualifiedTypeNames.
So both my case and the FQN JavaDoc case have
unqualifiedTypeNamesnot containing the type. So check 1 fires for both.The difference: in FQN JavaDoc case, we WANT the import removed. In generic type argument case, we WANT the import kept.
Hmm. So we need additional logic to distinguish these cases.
One approach: check if the identifier is in a type position (e.g., as
clazzofJ.ParameterizedType).Let me try this. I'll modify
collectUnqualifiedTypeNamesto include identifiers in type positions even when type attribution is missing.Actually wait, I just realized something. Let me re-check the FQN JavaDoc case.
For
{@link java.util.Date}:- The J tree is
J.FieldAccess("java.util.Date"). - This is NOT a
J.Identifier. It's aJ.FieldAccess.
So if I check for
J.Identifieronly, the FQN JavaDoc case wouldn't match. Let me see.For
extends AtomicReference<BloomFilter<T>>:- The inner J tree of
BloomFilter<T>isJ.ParameterizedTypewithclazz = J.Identifier("BloomFilter"). - The J.Identifier "BloomFilter" is in a type position.
So if I check for J.Identifier in type positions (as
clazzofJ.ParameterizedType), this would match the generic type argument case but NOT the FQN JavaDoc case (because FQN ref uses J.FieldAccess, not J.Identifier).This is the key insight! Let me implement this.
The fix:
- Modify
collectUnqualifiedTypeNamesto include identifiers in type positions even when type attribution is missing. - For the type argument case, "BloomFilter" would be added to unqualifiedTypeNames.
- For the FQN JavaDoc case, "java.util.Date" would NOT be added (because it's a J.FieldAccess, not a J.Identifier).
Wait, but the FQN JavaDoc case uses J.FieldAccess. So I should also consider J.FieldAccess in type positions. But then the FQN JavaDoc case would have "Date" added (the simple name part).
Hmm. OK let me think more carefully.
Actually, let me approach this from yet another angle. The issue is that:
- For
{@link java.util.Date}(FQN JavaDoc), the import should be removed because the FQN is sufficient. - For
BloomFilter<T>(type argument), the import should be kept because the simple name is used.
The difference:
- FQN JavaDoc: the reference is fully qualified, so the import is redundant.
- Type argument: the reference uses the simple name, so the import is needed.
So the rule is: if the simple name is used in source code (not just in FQN references), keep the import.
For my case: "BloomFilter" is used as a simple name. KEEP. For
{@link Date}: "Date" is used as a simple name. KEEP. For{@link java.util.Date}: only the FQN is used. REMOVE.So I need to check if the simple name is used as a simple name (not just as part of FQN).
How to detect this:
- If the identifier appears as a standalone identifier in source code (not just as part of J.FieldAccess chain).
For
BloomFilter<T>:BloomFilteris a J.Identifier, not part of a J.FieldAccess chain. → Simple name used.
For
{@link java.util.Date}:Dateis part of a J.FieldAccess chain. → Not a simple name use.- But
Dateis also the J.Identifier at the end of the chain.
Hmm, this is tricky.
Actually, looking at the original behavior:
- For
{@link java.util.Date}: the J.FieldAccess "java.util.Date" has the inner tree as a J.FieldAccess. The visitorcollectUnqualifiedTypeNameschecksisPartOfQualifiedReference. ForDateat the end of the chain, is it "part of a qualified reference"?
Let me check
isPartOfQualifiedReference:private boolean isPartOfQualifiedReference(J.Identifier identifier) { // Walk up the cursor to see if this identifier is part of a J.FieldAccess chain Cursor cursor = getCursor(); while (cursor != null) { Object value = cursor.getValue(); // This identifier is the name part of a field access // Check if the target is a package/class reference (not a field) if (value instanceof J.FieldAccess && ((J.FieldAccess) value).getName() == identifier && ((J.FieldAccess) value).getTarget() instanceof J.FieldAccess && ((J.FieldAccess) value).getTarget().getType() instanceof JavaType.FullyQualified) { return true; // This is a qualified reference } cursor = cursor.getParent(); } return false; }So
isPartOfQualifiedReferencereturns true if:- The parent is a J.FieldAccess.
- The current identifier is the
nameof the J.FieldAccess. - The target of the J.FieldAccess is ALSO a J.FieldAccess.
- The target's type is FullyQualified.
So for
Datein{@link java.util.Date}:- The parent is J.FieldAccess "java.util.Date".
Dateis thenameof the J.FieldAccess.- The target is J.FieldAccess "java.util" - which is ALSO a J.FieldAccess.
- The target's type - is it FullyQualified?
If the type is stripped (PR #5738), the target's type might not be FullyQualified. In that case,
isPartOfQualifiedReferencereturns false, andDatewould be added tounqualifiedTypeNames.Wait, but my debug showed
unqualifiedTypeNames = [java.util.List, <unknown>, Test2](no Date). SoDatewas NOT added. SoisPartOfQualifiedReferencereturned true (or the type check failed).Let me look at the test more carefully:
{@link java.util.Date}- the inner tree is J.FieldAccess.- The target is J.FieldAccess "java.util".
- The target's name is "util", target is J.Identifier "java".
- The target's type - J.Identifier "java" might have type "java" as a package (or might not have type).
Hmm, this is getting complex. Let me just focus on the fix.
The fix should:
- For my case (generic type argument without classpath): keep the import.
- For FQN JavaDoc case: remove the import.
The difference between these cases is that:
- In my case, the simple name "BloomFilter" is used as a type.
- In FQN JavaDoc, the simple name "Date" is part of an FQN.
If I check
sourceIdentifierNames.contains(elem.getClassName()), both cases have "BloomFilter"/"Date" in sourceIdentifierNames. So my check doesn't distinguish them.But wait, my earlier debug showed:
unqualifiedTypeNames=[java.util.List, <unknown>, Test2] sourceIdentifierNames=[java, util, List, list, Date, Test2]So
sourceIdentifierNamesincludes "Date" (from JavaDoc identifier). My check!sourceIdentifierNames.contains("Date")is false. So my check at line 274 prevents the removal. But the test expects removal!So I need to either:
- Not use
sourceIdentifierNamesin my check. - Modify
sourceIdentifierNamesto exclude certain cases.
Let me think about option 1. The original check 1 is:
if (combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) {This fires when:
- combinedTypes is empty (no type attribution for the import's package).
- elem.getTypeName() is not in unqualifiedTypeNames.
For my case:
- combinedTypes is empty.
- "some.unknown.BloomFilter" not in unqualifiedTypeNames.
- Fires. Import removed.
For FQN JavaDoc case:
- combinedTypes is empty (Date stripped).
- "java.util.Date" not in unqualifiedTypeNames.
- Fires. Import removed. ✓
For short JavaDoc case:
- combinedTypes = [java.util.List] (just List).
- For Date import: combinedTypes.isEmpty() is false. Doesn't fire.
- Check 4 fires later? Let me check.
- Actually wait, my debug showed combinedTypes = [java.util.List] for both Date and List imports.
Wait, that's weird. The combinedTypes is the same for both imports because the package "java.util" is the same for both. But the imports are different.
Actually,
combinedTypesis computed per-import based on the package of the import. Forjava.util.Dateimport,target = "java.util", socombinedTypesis computed fromtypesByPackage.get("java.util")= {List}.For my case (BloomFilter):
target = "some.unknown".typesByPackage.get("some.unknown")= empty (no type attribution for this package).combinedTypes= empty.
OK so for my case, combinedTypes is empty, and check 1 fires.
The fix should:
- For my case: don't fire check 1.
- For FQN JavaDoc case: fire check 1.
How to distinguish?
- In my case, the type IS used in source code (in a generic type argument).
- In FQN JavaDoc case, the type is ONLY referenced in JavaDoc with FQN.
So if I check "is the type referenced in source code (NOT JavaDoc) by simple name", I can distinguish.
Let me create a new set
sourceCodeIdentifierNamesthat excludes JavaDoc, and use it in my check.Actually, the easiest way is to override
getJavadocVisitor()to NOT add identifiers tosourceCodeIdentifierNames. Let me revert my original approach but ONLY modifycollectSourceIdentifierNames(notcollectUnqualifiedTypeNames).Wait, I already tried that. Let me think about why it didn't work.
Actually, I realize my earlier analysis was wrong. Let me re-check.
With
getJavadocVisitoroverride (excludes JavaDoc from sourceIdentifierNames):sourceIdentifierNames = [Test, List, list](no Date).- For Date import:
- Check 1 (with my fix):
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains("java.util.Date") && !sourceIdentifierNames.contains("Date")combinedTypes.isEmpty()= false (has List).- Overall: false. Doesn't fire.
- Check 4 (with my fix):
... && !unqualifiedTypeNames.contains("java.util.Date") && !sourceIdentifierNames.contains("Date")combinedTypes.stream().noneMatch(Date)= true.!unqualifiedTypeNames.contains("java.util.Date")= true.!sourceIdentifierNames.contains("Date")= true (Date NOT in sourceIdentifierNames).- Overall: true. FIRES. Import removed. ✓
- Check 1 (with my fix):
OK so with the JavaDoc exclusion AND my fix, the FQN JavaDoc case works correctly.
Now for my case (BloomFilter without classpath):
sourceIdentifierNames = [BloomFilter, ...](includes BloomFilter from source code).- For BloomFilter import:
- Check 1 (with my fix):
... && !sourceIdentifierNames.contains("BloomFilter")!sourceIdentifierNames.contains("BloomFilter")= false.- Overall: false. Doesn't fire. ✓ Import kept.
- Check 1 (with my fix):
For short JavaDoc case (
{@link Date}):sourceIdentifierNames = [Test, List, list](no Date).- For Date import:
- Check 1 (with my fix):
combinedTypes.isEmpty()- depends.- Actually,
combinedTypesforjava.util.Dateimport with{@link Date}ref would include Date (because the J.Identifier has type attribution). SocombinedTypesmight not be empty. - Wait, my earlier debug showed
combinedTypes = [java.util.Date, java.util.List]for{@link Date}case. So combinedTypes is not empty.
- Actually,
- So check 1 doesn't fire.
- Check 3:
!sourceIdentifierNames.contains("Date")= true (Date NOT in sourceIdentifierNames). - Check 3 fires! Import removed. ✗ (Test expects import KEPT)
- Check 1 (with my fix):
So with my approach (JavaDoc exclusion), the short JavaDoc case fails!
Wait, but my earlier test showed
retainImportIfUsedInJavaDocwas PASSING with my original fix (no JavaDoc exclusion). Let me re-check.Actually, I had reverted the JavaDoc exclusion at one point. Let me check the current state.
Looking at the current code,
collectSourceIdentifierNamesis the original (includes JavaDoc). And my fix is in check 1 and check 4.Without JavaDoc exclusion:
sourceIdentifierNamesincludes JavaDoc identifiers.- For short JavaDoc case: "Date" IS in sourceIdentifierNames.
- For FQN JavaDoc case: "Date" IS in sourceIdentifierNames.
- For my case: "BloomFilter" IS in sourceIdentifierNames.
So my check
!sourceIdentifierNames.contains(elem.getClassName())is false for all cases. So my check doesn't fire for any case. So my fix doesn't help.Wait, but then my test
BloomFilterUsedOnlyInGenericTypeArgumentOfExtendsWithoutClasspathshould still fail!Let me re-check. Earlier the test passed... oh wait, did I revert the JavaDoc exclusion AND the fix didn't work?
Let me check the current state of the code:Let me check the current state of the code:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[grep -n "sourceIdentifierNames" /rewrite/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java | head -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me check the current state of the code:
Assistant
OK, so my current fix is in check 1 (line 228) and check 4 (line 275). I removed the JavaDoc exclusion from
collectSourceIdentifierNames. ThesourceIdentifierNamesincludes JavaDoc identifiers.Let me think about this more carefully. The problem is:
- My case (BloomFilter without classpath): want to KEEP import.
- FQN JavaDoc case: want to REMOVE import.
The check 1 fires for both cases. I added
!sourceIdentifierNames.contains(elem.getClassName())to prevent the removal, but this also prevents the FQN JavaDoc removal.The issue is that both cases have the simple name in
sourceIdentifierNames.But wait, looking at my earlier debug output more carefully:
- For
removeImportIfFullyQualifiedInJavaDoc:sourceIdentifierNames = [java, util, List, list, Date, Test2]. - The "Date" comes from the JavaDoc identifier.
If I exclude JavaDoc from
sourceIdentifierNames:- For
removeImportIfFullyQualifiedInJavaDoc:sourceIdentifierNames = [List, list, Test2](no Date). - For my case (BloomFilter without classpath):
sourceIdentifierNames = [BloomFilter, ...](BloomFilter is in source code, not JavaDoc).
So if I exclude JavaDoc, the FQN JavaDoc case would have
!sourceIdentifierNames.contains("Date")= true, and the check would fire (correctly removing the import).And my case would have
!sourceIdentifierNames.contains("BloomFilter")= false, and the check wouldn't fire (correctly keeping the import).So excluding JavaDoc IS the right approach! Let me re-verify why the short JavaDoc case failed.
For
retainImportIfUsedInJavaDoc(short ref{@link Date}):- With JavaDoc exclusion:
sourceIdentifierNames = [Test, List, list](no Date). - For Date import:
- Check 1:
combinedTypes.isEmpty()- depends. - For
retainImportIfUsedInJavaDoc, the J.Identifier "Date" in{@link Date}has type attribution. So Date would be intypesByPackage(or inunqualifiedTypeNames). - Actually, looking at my earlier debug for
retainImportIfUsedInJavaDoc:DEBUG target=java.util types=[java.util.Date, java.util.List] combinedTypes=[java.util.Date, java.util.List] unqualifiedTypeNames=[java.util.List, Test, java.util.Date] sourceIdentifierNames=[Test, List, list] - So
combinedTypes = [java.util.Date, java.util.List]. Date IS in combinedTypes. - For Date import:
combinedTypes.isEmpty()= false. Check 1 doesn't fire. - Check 3 (line 265):
!sourceIdentifierNames.contains("Date")= true (Date NOT in sourceIdentifierNames with JavaDoc exclusion). - Check 3 FIRES! Import removed. ✗
- Check 1:
So the issue is check 3 (line 265). This was the existing check from PR #7336. With JavaDoc exclusion, this check now fires incorrectly for short JavaDoc references.
Hmm. So the existing check 3 also has the same problem.
Let me think about this. The check 3 was added in PR #7336 to remove imports that are only referenced in type attribution. The example given was:
import org.example.a.Something; import static org.example.a.Something.DEFAULT_SOMETHING; import static org.example.a.Something.doSomething; public class Test { public static void main(String[] args) { doSomething(DEFAULT_SOMETHING); } }In this case,
Somethingis referenced in type attribution (as the type ofDEFAULT_SOMETHING), but not as an identifier in source code. So the import should be removed.The check is: if
qualid.getSimpleName()is NOT insourceIdentifierNames, remove the import.This makes sense for the original case. But it also fires for short JavaDoc references if we exclude JavaDoc.
Hmm. So the original check 3 has a subtle issue: it conflates "not in source code" with "only in JavaDoc".
OK let me think about this differently. The right approach is:
- For my case (BloomFilter without classpath): keep the import.
- For FQN JavaDoc case: remove the import.
- For short JavaDoc case: keep the import.
- For static method parameter type: remove the import (original PR #7336 case).
The differentiator is:
- For my case: the simple name is used in source code (not in JavaDoc).
- For FQN JavaDoc case: the simple name is only used in JavaDoc with FQN.
- For short JavaDoc case: the simple name is used in JavaDoc (short ref).
- For static method parameter type: the simple name is NOT used anywhere in source.
So the rule is:
- If the simple name is used in source code (not just in JavaDoc) → KEEP.
- If the simple name is used in JavaDoc (short ref) → KEEP.
- If the simple name is only in JavaDoc with FQN → REMOVE.
- If the simple name is not used anywhere → REMOVE.
So I need to distinguish between these cases. Let me think about how to implement this.
The simplest way:
- Collect two sets:
sourceCodeIdentifierNames- identifiers in source code (NOT in JavaDoc).javaDocShortReferenceNames- simple names referenced in JavaDoc (short refs only).
If the import's simple name is in
sourceCodeIdentifierNames, KEEP. If the import's simple name is injavaDocShortReferenceNames, KEEP. Otherwise, REMOVE.To collect
javaDocShortReferenceNames:- Visit Javadoc.Reference nodes.
- If the inner tree is J.Identifier (short ref), add the simple name to the set.
- If the inner tree is J.FieldAccess (FQN ref), don't add.
Let me implement this.
Actually, there's a simpler way. The existing
unqualifiedTypeNamesalready handles the type-attributed cases. Let me see:- For short JavaDoc ref
{@link Date}: the J.Identifier has type attribution. SounqualifiedTypeNamescontains "java.util.Date". With my check!unqualifiedTypeNames.contains("java.util.Date")= false. Check 1 doesn't fire. ✓ - For FQN JavaDoc ref
{@link java.util.Date}: the J.FieldAccess has type attribution stripped. SounqualifiedTypeNamesdoesn't contain "java.util.Date". Check 1 fires. ✓ - For my case (BloomFilter without classpath): the J.Identifier has no type attribution. So
unqualifiedTypeNamesdoesn't contain "some.unknown.BloomFilter". Check 1 fires. ✗
So the difference between my case and the FQN JavaDoc case is:
- In my case, the J.Identifier has no type attribution.
- In FQN JavaDoc, the J.FieldAccess has stripped type attribution.
But from the visitor's perspective, both have null/missing type attribution. So we can't distinguish them with
unqualifiedTypeNames.But we CAN distinguish them by the structure:
- In my case, the J.Identifier is in a type position (e.g., as
clazzofJ.ParameterizedType). - In FQN JavaDoc, the J.FieldAccess is in a reference position (e.g., as
treeofJavadoc.Reference).
So if I check "is the identifier in a type position", I can distinguish.
Let me implement this. I'll modify
collectUnqualifiedTypeNamesto include identifiers in type positions even when type attribution is missing.Specifically, I'll override:
visitParameterizedTypeto add theclazzif it's a J.Identifier.visitVariableDeclarationsto add the type if it's a J.Identifier.visitMethodDeclarationto add the return type if it's a J.Identifier.- etc.
But this is complex. Let me try a simpler approach: in
visitIdentifier, check if the parent is a J.ParameterizedType (asclazz).Actually, an even simpler approach: I can check if the identifier is a "type" by looking at the parent's type. If the parent's type is a JavaType.FullyQualified or JavaType.Parameterized, then the identifier is likely a type reference.
Hmm, this is still complex. Let me just try the approach of checking the parent.
Wait, I have another idea. The
J.ParameterizedType.clazzis the simple name of the type. ForBloomFilter<T>,clazzisJ.Identifier("BloomFilter"). The parent ofclazzisJ.ParameterizedType.So if I check the parent in
visitIdentifier:- If the parent is a J.ParameterizedType, then the identifier is a type reference.
- Add the simple name to the set.
For
{@link Date}(short JavaDoc ref):- The J.Identifier "Date" is the
treeofJavadoc.Reference. - The parent is
Javadoc.Reference, notJ.ParameterizedType. - Don't add. (OK, but we need to add it from elsewhere.)
For
{@link java.util.Date}(FQN JavaDoc ref):- The J.FieldAccess is the
treeofJavadoc.Reference. - The "Date" J.Identifier is the
nameof the J.FieldAccess. - The parent is J.FieldAccess.
- Don't add. ✓
For my case (BloomFilter without classpath):
- The J.Identifier "BloomFilter" is the
clazzofJ.ParameterizedType. - The parent is
J.ParameterizedType. - Add to the set. ✓
This approach seems to work! Let me implement it.
But wait, I also need to handle other type positions:
- Variable type:
List<String> list;- the type isJ.ParameterizedTypewithclazz = J.Identifier("List"). Same as above. - Method return type:
List<String> method() { ... }- the type isJ.ParameterizedType. Same. - Type parameter bound:
<T extends BloomFilter<String>>- the bound isJ.ParameterizedType(or justJ.IdentifierforT extends BloomFilter). - Cast type:
(BloomFilter) obj- the type isJ.Identifier("BloomFilter")orJ.ParameterizedType. The parent would beJ.TypeCast. new BloomFilter<>()- the type isJ.ParameterizedType. The parent isJ.NewClass.- etc.
The simplest is to check the parent type:
- If the parent is
J.ParameterizedType,J.NewClass,J.TypeCast,J.VariableDeclarations, etc. → type reference.
But this requires checking many parent types. Let me just focus on
J.ParameterizedTypefor now and see if that covers the cases.Actually, looking at the visitor pattern, the parent's type is available in the cursor. I can check the parent in
visitIdentifier.Let me implement this:
Assistant
Now I understand the issue better. Let me think about this more carefully.
The simplest fix is to:
- Add a separate set
sourceCodeIdentifierNames(excludes JavaDoc). - Modify my added checks to use this new set.
- Keep the existing
sourceIdentifierNames(includes JavaDoc) for the existing check 3.
This way:
- My case: "BloomFilter" is in
sourceCodeIdentifierNames. My check 1 doesn't fire. ✓ - FQN JavaDoc case: "Date" is NOT in
sourceCodeIdentifierNames. My check 1 fires (original behavior). ✓ - Short JavaDoc case: "Date" is NOT in
sourceCodeIdentifierNames. But unqualifiedTypeNames has Date, so original check 1 doesn't fire. ✓ - Static method parameter type: original check 3 handles it. ✓
Let me implement this. First, let me remove the debug output and revert my changes:
- Add a separate set
Assistant
Now let me implement the fix properly. I'll add a new set
sourceCodeIdentifierNames(excludes JavaDoc) and use it in my added checks:Assistant
Let me fix the perl script:
Assistant
Let me try a different approach - I'll do this in multiple steps:
Assistant
Now let me add the new method:
Assistant
Let me use a different approach with file-based replacement:
Assistant
Now let me modify the checks to use the new set:
Assistant
Now let me compile and test:
Assistant
All tests pass! Let me also run the existing tests to make sure I didn't break anything:
Assistant
All 74 RemoveUnusedImports tests pass and 2 BloomFilterReproTest tests pass. Let me also test more edge cases:
Assistant
All 5 edge case tests pass! Let me also run all the related tests:
Assistant
All tests pass. Let me now delete my temp test file since I'm not supposed to add new tests:
Assistant
Now let me verify the final state of the changes:
Assistant
The diff looks clean. Let me run the full test suite one more time to confirm everything works:
Assistant
All 74 tests pass (2 skipped which are pre-existing
@Disabledtests). Let me also run the broader test suite to make sure nothing else is broken:Assistant
All tests in rewrite-java-test pass! Let me view the final diff once more:
Assistant
There's an unexpected change in
settings.gradle.kts. Let me check:Assistant
This is unrelated to my changes. Let me revert it:
Assistant
Now the only change is to
RemoveUnusedImports.java. Let me run a final comprehensive test to confirm:Assistant
All 74 tests pass. Let me do one final test to make sure my fix works for the original issue's example:
Assistant
The fix works correctly. Let me clean up temporary files and verify the final state:
Assistant
The only change is to
RemoveUnusedImports.java. Let me view the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →