Skip to content
TheChatArchiveThe Chat Archive
MiniMaxminimax/minimax-m3· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION RemoveUnusedImports removes imports when used in Generics only <!--…

  1. 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 here
    

    Are 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to 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 submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. MiniMax Assistant

    Let me start by exploring the repository structure to understand the

  3. MiniMax Assistant

    Let me explore the rewrite-java directory since the issue is about a Java recipe

  4. MiniMax Assistant

    let me look at the test file to understand the testing patterns:

  5. MiniMax Assistant

    Let me first create a reproduction script to confirm the issue:

  6. MiniMax Assistant

    Now let me find an example of a test case to understand how to write a test reproducer:

  7. MiniMax Assistant

    Let me check the JavaIsoVisitor structure to better understand how the type attribution works:

  8. MiniMax Assistant

    Let me trace through this issue more carefully:

  9. MiniMax Assistant

    write a test case to reproduce the issue:

  10. MiniMax Assistant

    Let me first set up a reproduction script:

  11. MiniMax Assistant

    Let me first build the project to see if everything works:

  12. MiniMax 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:

  13. MiniMax Assistant

    Let me write a reproduction script that uses the existing test infrastructure:

  14. MiniMax Assistant

    Let me look at how other tests are structured:

  15. MiniMax Assistant

    Let me build the project first:

  16. MiniMax Assistant

    Let me create a temporary test that reproduces the issue:

  17. MiniMax 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:

  18. MiniMax Assistant

    Let me check what actually happens by adding an "after" parameter that expects the imports to remain:

  19. MiniMax 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:

  20. MiniMax Assistant

    OK, so the issue is that expectedCyclesThatMakeChanges is set based on whether the source files differ before/after. Let me check:

  21. MiniMax Assistant

    Let me check the SourceSpec comparison logic:

  22. MiniMax Assistant

    Let me modify the test to make sure the behavior is correct - if no change is expected, we don't provide an after:

  23. MiniMax Assistant

    Let me write a custom test that prints the actual transformed source to confirm the bug:

  24. MiniMax Assistant

    The afterRecipe method might be on a different class. Let me check:

  25. MiniMax Assistant

    The SourceSpecs.java interface doesn't have afterRecipe. Let me look at Assertions.java to see how it returns the `SourceSpecs

  26. MiniMax Assistant

    OK so java() returns a SourceSpec actually. Let me update the test:

  27. MiniMax Assistant

    Let me look at the actual structure more carefully:

  28. MiniMax Assistant

    Let me update the test - the consumer should be modifying the SourceSpec, not consuming a J.CompilationUnit:

  29. MiniMax 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):

  30. MiniMax Assistant

    Let me check the test framework logic more carefully:

  31. MiniMax Assistant

    me check the git log for changes to RemoveUnusedImports.java:

  32. MiniMax Assistant

    me check if this bug is already fixed:

  33. MiniMax Assistant

    Let me look at the test that was added for the bug fix in 330504939:

  34. MiniMax 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 BloomFilter identifier IS in the source code (in BloomFilter<T>). So sourceIdentifierNames.contains("BloomFilter") is true. 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 after parameter that's different from before. 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:

  35. MiniMax Assistant

    me write a clearer test that verifies the bug IS fixed:

  36. MiniMax 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:

  37. MiniMax Assistant

    Let me verify by exploring the AST structure for this case:

  38. MiniMax Assistant

    me run the test with debug output to actually see what's happening:

  39. MiniMax Assistant

    Let me trace through the logic more carefully to understand if there's any edge case:

  40. MiniMax Assistant

    me look at all recent fixes related to RemoveUnusedImports:

  41. MiniMax Assistant

    Let me look at PR #7383 which was a recent fix:

  42. MiniMax Assistant

    Let me check if there have been releases between v8.31.1 and now:

  43. MiniMax Assistant

    The bug seems to be fixed. Let me verify by running the existing test suite to make sure nothing is broken:

  44. MiniMax Assistant

    Let me check the GitHub issue tracker for related issues by looking at the recent test cases added:

  45. MiniMax Assistant

    Let me check if there's still an issue with similar cases that aren't fully handled:

  46. MiniMax 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:

  47. MiniMax Assistant

    All test cases passed. So the bug IS fixed for all these cases in the current code.

    Let me check the test results:

  48. MiniMax Assistant

    Let me look at how ParameterizedType is parsed and what its clazz field looks like:

  49. MiniMax Assistant

    Let me look at how existing tests are structured to understand the right approach:

  50. MiniMax Assistant

    me test more edge cases to find if there's a remaining issue:

  51. MiniMax Assistant

    check what happens if BloomFilter is not on the classpath:

  52. MiniMax 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:

  53. MiniMax Assistant

    Now I understand the bug. Let me check the second test case more carefully:

  54. MiniMax Assistant

    Now I'll apply the fix to RemoveUnusedImports.java:

  55. MiniMax Assistant

    Let me apply the fix to RemoveUnusedImports.java:

  56. MiniMax Assistant

    The fix is applied. Let me run the test to confirm:

  57. MiniMax Assistant

    me check the error:

  58. MiniMax Assistant

    Let me check the file to see if my edit was applied:

  59. MiniMax Assistant

    Let me look at the second check that also fires:

  60. MiniMax Assistant

    I need to fix check 3 as well. Let me update it to also check sourceIdentifierNames:

  61. MiniMax Assistant

    Still failing. Let me check what other code path might be triggering the removal. Let me check the getAmbiguousStaticImportNames part - oh wait, that's only for static imports.

    Let me look at the full code path again:

  62. MiniMax Assistant

    Let me check the NoMissingTypes precondition:

  63. MiniMax Assistant

    me add some debug output to trace what's happening:

  64. MiniMax Assistant

    me try modifying the recipe to add debug output:

  65. MiniMax Assistant

    Now I see the issue! sourceIdentifierNames contains "BloomFilter" (simple name), but elem.getTypeName() returns "some.unknown.BloomFilter" (FQN). Let me check elem.getTypeName():

  66. MiniMax Assistant

    issue is that elem.getTypeName() returns the FQN, but sourceIdentifierNames contains simple names. Let me fix this:

  67. MiniMax Assistant

    Now let me remove the debug line:

  68. MiniMax 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:

  69. MiniMax Assistant

    me check the failing test:

  70. MiniMax Assistant

    The test removeImportIfFullyQualifiedInJavaDoc expects the Date import to be removed (only referenced in JavaDoc). My fix is keeping it because Date appears in the source (in JavaDoc). Let me check if JavaDoc identifiers are visited:

  71. MiniMax Assistant

    The JavaDoc test is failing with my fix. The issue is that Date appears in JavaDoc, and the visitor is including Date as an identifier. So sourceIdentifierNames contains Date, 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 as Javadoc markers. So the visitor shouldn't visit them.

    But wait, the test fails! So Date IS being kept. Let me check what sourceIdentifierNames contains for this case.

    Actually, I think I need to check if the JavaDoc comment is visited or not. Let me also see what unqualifiedTypeNames contains.

    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:

    • typesByPackage would have java.util.List (from the field type).
    • combinedTypes would have java.util.List.
    • For java.util.Date import: target = "java.util". types = typesByPackage.get("java.util") = {List, Date}? Wait, if Date is not used in the code, would it be in typesByPackage?

    Hmm, let me check what cu.getTypesInUse() includes. If the JavaDoc has {@link java.util.Date}, the type attribution for Date might 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:

    • typesByPackage for java.util would only contain List.
    • combinedTypes would only contain List.
    • For java.util.Date import: combinedTypes is NOT empty (has List), but doesn't contain Date. 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:

    • combinedTypes would be {List}.
    • For Date import: 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 sourceIdentifierNames does contain Date. 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>

  72. MiniMax Assistant

    Let me investigate how JavaDoc is

  73. MiniMax 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:

    1. The JavaDoc references are being included in sourceIdentifierNames somehow.
    2. My understanding of the code path is wrong.

    Let me re-check by adding debug output to see what sourceIdentifierNames contains.

    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:

  74. MiniMax 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:

  75. MiniMax Assistant

    So visitReference calls javaVisitorVisit which invokes javaVisitor.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 Date ends up in sourceIdentifierNames. Let me verify by adding debug output:

  76. MiniMax Assistant

    Date ends up in sourceIdentifierNames because JavaDoc references are visited. I need to differentiate between "type reference in source" and "JavaDoc reference". Let me modify collectSourceIdentifierNames to exclude identifiers inside Javadoc:

  77. MiniMax Assistant

    So I can override getJavadocVisitor() to return a custom visitor. Let me modify collectSourceIdentifierNames:

  78. MiniMax Assistant

    me remove the debug output and try a different approach. I'll override getJavadocVisitor() in collectSourceIdentifierNames to exclude JavaDoc identifiers:

  79. MiniMax 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:

  80. MiniMax Assistant

    Wait, let me think about this differently. The current approach uses sourceIdentifierNames which 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 sourceIdentifierNames for this fix. Instead, I should modify collectUnqualifiedTypeNames to 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 unqualifiedTypeNames is 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 collectUnqualifiedTypeNames to 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 like java.util.List)
    • J.ParameterizedType (parameterized type like List<String>)
    • J.ArrayType (array type like String[])

    The visitor should look at these to identify type references.

    Actually, the simplest fix is to override visitParameterizedType and add the clazz to unqualifiedTypeNames if it's a J.Identifier with a FullyQualified type (or missing type attribution).

    Wait, but J.ParameterizedType.clazz could be a J.Identifier (simple name) or a J.FieldAccess (qualified name).

    Let me take a different approach. I'll modify collectUnqualifiedTypeNames to 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.returnType
    • J.ClassDeclaration.extends
    • J.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 (as clazz)
    • Parent is J.VariableDeclarations (as type)
    • Parent is J.MethodDeclaration (as returnType or parameter type)
    • etc.

    Let me write this:Let me take a different approach. I'll modify collectUnqualifiedTypeNames to 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>

  81. MiniMax Assistant

    take a different approach. I'll modify collectSourceIdentifierNames to exclude identifiers inside JavaDoc by overriding getJavadocVisitor():

  82. MiniMax Assistant

    Wait, I need to think about this differently. The issue is that sourceIdentifierNames includes JavaDoc identifiers. The simplest fix is to NOT use sourceIdentifierNames for 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:

    1. combinedTypes is empty (because no type attribution for BloomFilter).
    2. unqualifiedTypeNames doesn't contain BloomFilter (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"; - here s is a variable, not a type.

    Hmm. Let me think about what makes a "type reference" in Java's AST.

    In the AST:

    • J.Identifier can be a type reference or a value reference.
    • The context (parent) determines which.

    For our case BloomFilter<T>:

    • BloomFilter is the clazz of J.ParameterizedType.
    • This is a type reference.

    For Date d;:

    • Date is the type of J.VariableDeclarations.
    • This is a type reference.

    For Date.something:

    • Date is the target of J.FieldAccess.
    • This is a value reference (or a type reference if it's a static method/field).

    The current visitor collectUnqualifiedTypeNames checks type 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 (as clazz), or J.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 because Date is only referenced in JavaDoc.

    So Date should NOT be in unqualifiedTypeNames because it's only in JavaDoc.

    With my current fix:

    • sourceIdentifierNames includes Date (because JavaDoc references are visited).
    • unqualifiedTypeNames does NOT include Date (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:

    • sourceIdentifierNames would NOT include Date.
    • 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>

  83. MiniMax Assistant

    Let me take a step back and reconsider. The issue is:

    1. When the type is on the classpath with proper type attribution, the existing checks work correctly - the import is kept.
    2. When the type is NOT on the classpath (type attribution missing), the existing checks incorrectly remove the import.
    3. 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 in sourceIdentifierNames.

    So I need to either:

    1. Exclude JavaDoc identifiers from sourceIdentifierNames.
    2. 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>

  84. MiniMax Assistant

    Let me first look at the current state of the file:

  85. MiniMax Assistant

    Let me modify collectSourceIdentifierNames to exclude JavaDoc identifiers by overriding getJavadocVisitor():

  86. MiniMax Assistant

    Good, no Javadoc imports yet. I need to add the imports. Let me check the current imports first.

  87. MiniMax Assistant

    I need to add the import for JavadocVisitor:

  88. MiniMax Assistant

    Now let me update the collectSourceIdentifierNames method:

  89. MiniMax Assistant

    Now let me replace the method using a Python script:

  90. MiniMax Assistant

    Python. Let me use perl to do the replacement:

  91. MiniMax Assistant

    Now let me build and run the tests:

  92. MiniMax Assistant

    Two tests are failing because they expect imports referenced in JavaDoc to be kept. Let me look at these tests:

  93. MiniMax Assistant

    Let me check what unqualifiedTypeNames contains for the JavaDoc case:

  94. MiniMax Assistant

    The syntax error is because I'm printing before the second variable is defined. Let me fix the debug:

  95. MiniMax Assistant

    unqualifiedTypeNames contains Date from the JavaDoc reference. Let me check why the test is failing:

  96. MiniMax Assistant

    me check which check is firing. The combinedTypes has Date, so check 4 shouldn't fire:

  97. MiniMax Assistant

    Let me revert the getJavadocVisitor change and keep my original fix:

  98. MiniMax Assistant

    me remove the unused import:

  99. MiniMax Assistant

    Let me check the removeImportIfFullyQualifiedInJavaDoc test more carefully:

  100. MiniMax Assistant

    Let me add debug output to see what combinedTypes contains for the JavaDoc case:

  101. MiniMax Assistant

    me check the order of code:

  102. MiniMax Assistant

    The debug prints typesByPackage BEFORE it's populated. Let me move it to after:

  103. MiniMax Assistant

    The DEBUG was removed by the sed. Let me re-add it.

  104. MiniMax Assistant

    The test fails but no DEBUG output is shown. Let me try a different approach to see the output:

  105. MiniMax Assistant

    me check if the debug line is actually in the file:

  106. MiniMax Assistant

    The debug is inside the if block, so it only prints when removing. Let me move it to the top:

  107. MiniMax Assistant

    me just delete the misplaced debug and add it correctly:

  108. MiniMax 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 Date import:

    • 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 Date import in JavaDoc FQN case:
      • Check 1: combinedTypes.isEmpty() && !unqualifiedTypeNames.contains("java.util.Date") = true && true = true. → FIRES. Import removed.

    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 removeImportIfFullyQualifiedInJavaDoc expects 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 is java.util.Date (FQN).
    • extends AtomicReference<BloomFilter<T>> - short reference. The type is BloomFilter<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:

    1. Whether the simple name is in source code (not just in JavaDoc).
    2. 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.

    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 sourceIdentifierNames I 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:

    1. sourceCodeIdentifierNames - identifiers in source code (NOT in JavaDoc).
    2. javaDocIdentifierNames - identifiers in JavaDoc.

    If the import's simple name is in sourceCodeIdentifierNames, KEEP. If the import's simple name is in javaDocIdentifierNames AND 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 unqualifiedTypeNames which collects FQNs from type-attributed identifiers. For JavaDoc references:

    • Short reference {@link Date}: The J tree is J.Identifier("Date"). The type might be java.util.Date. So unqualifiedTypeNames contains "java.util.Date".
    • FQN reference {@link java.util.Date}: The J tree is J.FieldAccess("java.util.Date"). The type attribution is stripped (PR #5738). So unqualifiedTypeNames might NOT contain "java.util.Date".

    Wait, let me check the actual data:

    DEBUG ... unqualifiedTypeNames=[java.util.List, <unknown>, Test2] ...
    

    So unqualifiedTypeNames contains "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, unqualifiedTypeNames doesn't contain "java.util.Date". This is because:

    1. PR #5738 stripped Date from TypesInUse.
    2. The collectUnqualifiedTypeNames visitor only adds if type 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.Identifier in a generic type argument position.
    • The J.Identifier in a variable type declaration.
    • The J.Identifier in a method return type.
    • etc.

    A simple way: check if the identifier's parent is a J.ParameterizedType (as clazz).

    Actually, let me look at unqualifiedTypeNames more carefully. It only adds if type instanceof JavaType.FullyQualified. The issue is that this excludes identifiers with missing type attribution.

    What if I modify collectUnqualifiedTypeNames to also add identifiers in type positions (e.g., as clazz of J.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 Date import (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, unqualifiedTypeNames contains the FQN (because the J.Identifier in JavaDoc has type attribution). For FQN JavaDoc ref, unqualifiedTypeNames doesn'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 unqualifiedTypeNames not 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 clazz of J.ParameterizedType).

    Let me try this. I'll modify collectUnqualifiedTypeNames to 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 a J.FieldAccess.

    So if I check for J.Identifier only, the FQN JavaDoc case wouldn't match. Let me see.

    For extends AtomicReference<BloomFilter<T>>:

    • The inner J tree of BloomFilter<T> is J.ParameterizedType with clazz = J.Identifier("BloomFilter").
    • The J.Identifier "BloomFilter" is in a type position.

    So if I check for J.Identifier in type positions (as clazz of J.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:

    1. Modify collectUnqualifiedTypeNames to include identifiers in type positions even when type attribution is missing.
    2. For the type argument case, "BloomFilter" would be added to unqualifiedTypeNames.
    3. 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>:

    • BloomFilter is a J.Identifier, not part of a J.FieldAccess chain. → Simple name used.

    For {@link java.util.Date}:

    • Date is part of a J.FieldAccess chain. → Not a simple name use.
    • But Date is 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 visitor collectUnqualifiedTypeNames checks isPartOfQualifiedReference. For Date at 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 isPartOfQualifiedReference returns true if:

    • The parent is a J.FieldAccess.
    • The current identifier is the name of the J.FieldAccess.
    • The target of the J.FieldAccess is ALSO a J.FieldAccess.
    • The target's type is FullyQualified.

    So for Date in {@link java.util.Date}:

    • The parent is J.FieldAccess "java.util.Date".
    • Date is the name of 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, isPartOfQualifiedReference returns false, and Date would be added to unqualifiedTypeNames.

    Wait, but my debug showed unqualifiedTypeNames = [java.util.List, <unknown>, Test2] (no Date). So Date was NOT added. So isPartOfQualifiedReference returned 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:

    1. For my case (generic type argument without classpath): keep the import.
    2. 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 sourceIdentifierNames includes "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:

    1. Not use sourceIdentifierNames in my check.
    2. Modify sourceIdentifierNames to 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, combinedTypes is computed per-import based on the package of the import. For java.util.Date import, target = "java.util", so combinedTypes is computed from typesByPackage.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 sourceCodeIdentifierNames that excludes JavaDoc, and use it in my check.

    Actually, the easiest way is to override getJavadocVisitor() to NOT add identifiers to sourceCodeIdentifierNames. Let me revert my original approach but ONLY modify collectSourceIdentifierNames (not collectUnqualifiedTypeNames).

    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 getJavadocVisitor override (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. ✓

    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.

    For short JavaDoc case ({@link Date}):

    • sourceIdentifierNames = [Test, List, list] (no Date).
    • For Date import:
      • Check 1 (with my fix): combinedTypes.isEmpty() - depends.
        • Actually, combinedTypes for java.util.Date import with {@link Date} ref would include Date (because the J.Identifier has type attribution). So combinedTypes might not be empty.
        • Wait, my earlier debug showed combinedTypes = [java.util.Date, java.util.List] for {@link Date} case. So combinedTypes is not empty.
      • 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)

    So with my approach (JavaDoc exclusion), the short JavaDoc case fails!

    Wait, but my earlier test showed retainImportIfUsedInJavaDoc was 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, collectSourceIdentifierNames is the original (includes JavaDoc). And my fix is in check 1 and check 4.

    Without JavaDoc exclusion:

    • sourceIdentifierNames includes 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 BloomFilterUsedOnlyInGenericTypeArgumentOfExtendsWithoutClasspath should 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>

  109. MiniMax Assistant

    Let me check the current state of the code:

  110. MiniMax Assistant

    OK, so my current fix is in check 1 (line 228) and check 4 (line 275). I removed the JavaDoc exclusion from collectSourceIdentifierNames. The sourceIdentifierNames includes 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 in typesByPackage (or in unqualifiedTypeNames).
      • 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. ✗

    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, Something is referenced in type attribution (as the type of DEFAULT_SOMETHING), but not as an identifier in source code. So the import should be removed.

    The check is: if qualid.getSimpleName() is NOT in sourceIdentifierNames, 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 in javaDocShortReferenceNames, 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 unqualifiedTypeNames already handles the type-attributed cases. Let me see:

    • For short JavaDoc ref {@link Date}: the J.Identifier has type attribution. So unqualifiedTypeNames contains "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. So unqualifiedTypeNames doesn't contain "java.util.Date". Check 1 fires. ✓
    • For my case (BloomFilter without classpath): the J.Identifier has no type attribution. So unqualifiedTypeNames doesn'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 clazz of J.ParameterizedType).
    • In FQN JavaDoc, the J.FieldAccess is in a reference position (e.g., as tree of Javadoc.Reference).

    So if I check "is the identifier in a type position", I can distinguish.

    Let me implement this. I'll modify collectUnqualifiedTypeNames to include identifiers in type positions even when type attribution is missing.

    Specifically, I'll override:

    • visitParameterizedType to add the clazz if it's a J.Identifier.
    • visitVariableDeclarations to add the type if it's a J.Identifier.
    • visitMethodDeclaration to 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 (as clazz).

    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.clazz is the simple name of the type. For BloomFilter<T>, clazz is J.Identifier("BloomFilter"). The parent of clazz is J.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 tree of Javadoc.Reference.
    • The parent is Javadoc.Reference, not J.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 tree of Javadoc.Reference.
    • The "Date" J.Identifier is the name of the J.FieldAccess.
    • The parent is J.FieldAccess.
    • Don't add. ✓

    For my case (BloomFilter without classpath):

    • The J.Identifier "BloomFilter" is the clazz of J.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 is J.ParameterizedType with clazz = J.Identifier("List"). Same as above.
    • Method return type: List<String> method() { ... } - the type is J.ParameterizedType. Same.
    • Type parameter bound: <T extends BloomFilter<String>> - the bound is J.ParameterizedType (or just J.Identifier for T extends BloomFilter).
    • Cast type: (BloomFilter) obj - the type is J.Identifier("BloomFilter") or J.ParameterizedType. The parent would be J.TypeCast.
    • new BloomFilter<>() - the type is J.ParameterizedType. The parent is J.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.ParameterizedType for 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:

  111. MiniMax Assistant

    Now I understand the issue better. Let me think about this more carefully.

    The simplest fix is to:

    1. Add a separate set sourceCodeIdentifierNames (excludes JavaDoc).
    2. Modify my added checks to use this new set.
    3. 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:

  112. MiniMax Assistant

    Now let me implement the fix properly. I'll add a new set sourceCodeIdentifierNames (excludes JavaDoc) and use it in my added checks:

  113. MiniMax Assistant

    Let me fix the perl script:

  114. MiniMax Assistant

    Let me try a different approach - I'll do this in multiple steps:

  115. MiniMax Assistant

    Now let me add the new method:

  116. MiniMax Assistant

    Let me use a different approach with file-based replacement:

  117. MiniMax Assistant

    Now let me modify the checks to use the new set:

  118. MiniMax Assistant

    Now let me compile and test:

  119. MiniMax Assistant

    All tests pass! Let me also run the existing tests to make sure I didn't break anything:

  120. MiniMax Assistant

    All 74 RemoveUnusedImports tests pass and 2 BloomFilterReproTest tests pass. Let me also test more edge cases:

  121. MiniMax Assistant

    All 5 edge case tests pass! Let me also run all the related tests:

  122. MiniMax Assistant

    All tests pass. Let me now delete my temp test file since I'm not supposed to add new tests:

  123. MiniMax Assistant

    Now let me verify the final state of the changes:

  124. MiniMax Assistant

    The diff looks clean. Let me run the full test suite one more time to confirm everything works:

  125. MiniMax Assistant

    All 74 tests pass (2 skipped which are pre-existing @Disabled tests). Let me also run the broader test suite to make sure nothing else is broken:

  126. MiniMax Assistant

    All tests in rewrite-java-test pass! Let me view the final diff once more:

  127. MiniMax Assistant

    There's an unexpected change in settings.gradle.kts. Let me check:

  128. MiniMax Assistant

    This is unrelated to my changes. Let me revert it:

  129. MiniMax Assistant

    Now the only change is to RemoveUnusedImports.java. Let me run a final comprehensive test to confirm:

  130. MiniMax Assistant

    All 74 tests pass. Let me do one final test to make sure my fix works for the original issue's example:

  131. MiniMax Assistant

    The fix works correctly. Let me clean up temporary files and verify the final state:

  132. MiniMax Assistant

    The only change is to RemoveUnusedImports.java. Let me view the final diff:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.