ISSUE DESCRIPTION RemoveUnusedImports removes imports when used in Generics only <!--…
- User
ISSUE DESCRIPTION
RemoveUnusedImports removes imports when used in Generics only
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.31.1
- Maven/Gradle plugin v6.40.0
How are you running OpenRewrite?
I am using the Maven plugin, and my project is a single module project.
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>6.40.0</version> <executions> <execution> <phase>process-sources</phase> <goals> <goal>run</goal> </goals> <configuration> <activeRecipes combine.children="append"> <recipe>org.openrewrite.java.RemoveUnusedImports</recipe> <recipe>org.openrewrite.java.OrderImports</recipe> </activeRecipes> </configuration> </execution> </executions> </plugin>What is the smallest, simplest way to reproduce the problem?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you expect to see?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you see instead?
import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What is the full stack trace of any errors you encountered?
stacktrace output hereAre you interested in contributing a fix to OpenRewrite?
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you 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 analyze this issue. The problem is that the
RemoveUnusedImportsrecipe in OpenRewrite is incorrectly removing imports that are used in generic type parameters. Specifically, when a class likeBloomFilterRef<T> extends AtomicReference<BloomFilter<T>>usesBloomFilter<T>as a generic type argument, the import forBloomFilteris being incorrectly removed.Let me start by exploring the repository structure to understand how it's organized and find the relevant code for the
RemoveUnusedImportsrecipe.Assistant
I found the relevant files. Let me examine the
RemoveUnusedImports.javasource file first.Assistant
Error from tool call - let me use the correct syntax
Assistant
Let me examine the test file and understand the AST structure for parameterized types:
Assistant
Let me search for any existing tests related to generics in the test file:
Assistant
Let me look at the
collectUnqualifiedTypeNamesmethod more closely and understand what it's doing. The issue is thatBloomFilterinAtomicReference<BloomFilter<T>>is not being detected as a used unqualified type name. Let me examine the code logic more carefully:Assistant
Now let me understand the issue better. The key is in the
collectUnqualifiedTypeNamesmethod. Looking at the logic around line 227, an import is considered unused when:if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || ...)The issue is likely that
BloomFilterused inside a generic type parameter likeBloomFilter<T>is not being properly detected as an unqualified type name. Let me look at thevisitIdentifiermethod and theisPartOfQualifiedReferencecheck:Assistant
Now I need to understand the structure better. Let me look at how types in generic type parameters are represented. Let me first create a test to reproduce the issue:
Assistant
Let me first create a simple test script to reproduce this issue. I'll look at how tests are structured:
Assistant
Let me create a simple test script to reproduce the issue and understand the problem better:
Assistant
Now let me look for how to run the existing tests and check the build system:
Assistant
The test is in a different module. Let me check:
Assistant
Good, tests are running. Now let me write a small test to reproduce the issue. First, let me understand the structure better - I'll look at how the AST represents generic type parameters:
Assistant
Let me look at the Java AST structure for type references to understand how type parameters in generics are represented:
Assistant
Now I understand - in Java's AST, a parameterized type like
BloomFilter<T>is represented asJ.ParameterizedTypewith aclazz(the type identifier) andtypeParameters. Let me look at the structure:Assistant
Now I understand the structure. A
J.ParameterizedTypehas:clazz- the name of the class (e.g.,BloomFilter)typeParameters- the type parameters (e.g.,T)
The issue is that when collecting unqualified type names, the visitor visits identifiers but may not be visiting the
clazzfield ofJ.ParameterizedTypecorrectly, or there's a condition that filters it out.Let me look at how the identifier in a parameterized type is being handled. The
clazzfield inParameterizedTypeis aNameTree. Let me check whatNameTreeis:Assistant
The command exited with code 1, let me fix the grep command - it seems the issue was with special characters. Let me escape them properly.
Assistant
Now let me trace through the issue. When visiting
AtomicReference<BloomFilter<T>>, the visitor will encounter:- A
J.ParameterizedTypeforAtomicReference<BloomFilter<T>> - Its
clazzis an identifierAtomicReference - Its
typeParameterscontains anotherJ.ParameterizedTypeforBloomFilter<T> - That nested
J.ParameterizedTypehasclazzas an identifierBloomFilter
The
collectUnqualifiedTypeNamesmethod visitsJ.Identifiernodes. So it should be visiting theBloomFilteridentifier. Let me check if there's something special about how it's being filtered out.Let me check if the identifier's type is set correctly for type parameters in generic types:
- A
Assistant
Let me look at the default
visitParameterizedTypeimplementation inJavaVisitor:Assistant
So the visitor visits
pt.getClazz()which should be theBloomFilteridentifier. This means thevisitIdentifiershould be called forBloomFilter. Let me check the issue more carefully by looking at the condition invisitIdentifier:Assistant
The condition on line 486 is:
if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {This requires
typeto beJavaType.FullyQualified. The problem might be that whenBloomFilterappears inside a type parameter likeBloomFilter<T>, its type might beJavaType.Parameterizedinstead ofJavaType.FullyQualified.Let me verify this theory by looking at how types are attributed for parameterized types. Let me search for how the type is set on the identifier in a parameterized type:
Assistant
Now I see it!
JavaType.ParameterizedextendsJavaType.FullyQualified. So the conditiontype instanceof JavaType.FullyQualifiedshould match for parameterized types too. Let me check this:Assistant
I see -
JavaType.ParameterizedextendsJavaType.FullyQualified, so theinstanceofcheck should pass. Let me write a test to confirm the issue and understand what's happening:Assistant
Looking at the existing tests, I see there's a test
doNotRemoveInnerClassImportat line 105 that testsEntry<K, V>. Let me look at it more closely:Assistant
These tests use
Entry<K, V>directly as animplementsclause. But the issue reports a different scenario -BloomFilter<T>is used inside another type parameterAtomicReference<BloomFilter<T>>. This is a nested parameterized type scenario.Let me now create a temporary test file to reproduce the issue and see if it fails:
Assistant
Actually, let me look at how the existing tests structure their test cases and run one of them as reference first. Let me look more carefully at the logic. I need to trace through what happens when
BloomFilteris used in the contextAtomicReference<BloomFilter<T>>.Looking at the code in
visitIdentifier:if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) { if (!isPartOfQualifiedReference(identifier)) { unqualifiedTypeNames.add(((JavaType.FullyQualified) type).getFullyQualifiedName()); } }The key condition here is
identifier.getFieldType() == null. IfBloomFilterhas afieldTypeset when used as a type parameter, it would be excluded.Let me check what
getFieldType()means and when it's set:Assistant
So
fieldTypeisJavaType.Variableand it's set when the identifier refers to a field/variable. For type references, it should benull. So this shouldn't be the issue.Let me think about this more. The problem could be that when
BloomFilteris used as a type parameter insideAtomicReference<BloomFilter<T>>, the type information on the identifierBloomFiltermight not be set correctly, or it might be a different scenario.Let me look at how
typesByPackageis populated - this is what determines if a type is actually used:Assistant
Now I see the logic. The
typesByPackageis populated fromcu.getTypesInUse().getTypesInUse(). Let me look at what happens when checking if an import is used:Assistant
Now I see the logic more clearly. For non-static imports (line 215 onwards), the check is:
-
Line 227:
if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || ...)- if no types are found intypesByPackageAND the type name is not inunqualifiedTypeNames, the import is marked unused. -
Line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName()))- if the simple name doesn't appear in source code, mark as unused. -
Line 269-276:
else if (combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName()))- if none of the combined types match the import AND the type is not inunqualifiedTypeNames, mark as unused.
The key insight is that the code relies on two mechanisms:
typesByPackage- populated fromcu.getTypesInUse().getTypesInUse()which contains type information gathered during type attributionunqualifiedTypeNames- collected by visiting identifiers in the source code
For
BloomFilter<T>insideAtomicReference<BloomFilter<T>>:AtomicReference<BloomFilter<T>>would be intypesInUseas a parameterized type- The type parameter
BloomFilter<T>should also be tracked
Let me look at how
TypesInUseworks:-
Assistant
I found the issue! Looking at the
FindTypesInUse.visitIdentifiermethod:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } else if (parent instanceof J.MethodDeclaration && ((J.MethodDeclaration) parent).getName() == identifier) { // skip method name } return super.visitIdentifier(identifier, p); }When visiting
BloomFilterinAtomicReference<BloomFilter<T>>, the parent isJ.ParameterizedType(the one representingBloomFilter<T>), not aJ.ClassDeclaration. So the type should be collected.Wait, let me re-examine. The
visitTypemethod is what adds types to thetypesset. When visiting theJ.ParameterizedTypeforBloomFilter<T>, it would callvisitTypeon its.typefield which would be aJavaType.ParameterizedforBloomFilter<T>.But the issue is that when we have
AtomicReference<BloomFilter<T>>:- The outer type
AtomicReference<BloomFilter<T>>is aJavaType.Parameterizedwith typejava.util.concurrent.atomic.AtomicReferenceand type parameters[BloomFilter<T>] - The type parameter
BloomFilter<T>is also aJavaType.Parameterizedwith typecom.google.common.hash.BloomFilterand type parameters[T]
In
RemoveUnusedImports.visitCompilationUnit, when processingJavaType.Parameterized:if (javaType instanceof JavaType.Parameterized) { JavaType.Parameterized parameterized = (JavaType.Parameterized) javaType; typesByPackage.computeIfAbsent(parameterized.getType().getPackageName(), f -> new HashSet<>()) .add(parameterized.getType()); for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { typesByPackage.computeIfAbsent( fq.getOwningClass() == null ? fq.getPackageName() : toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), f -> new HashSet<>()).add(fq); } } }So for
AtomicReference<BloomFilter<T>>:parameterized.getType()isjava.util.concurrent.atomic.AtomicReferenceparameterized.getTypeParameters()containsBloomFilter<T>which is aJavaType.ParameterizedTypeUtils.asFullyQualified(BloomFilter<T>)should return the underlyingcom.google.common.hash.BloomFiltertype
So
BloomFiltershould be added totypesByPackageundercom.google.common.hash.Now let me trace through what happens when checking if the import
com.google.common.hash.BloomFilteris used:target = qualid.getTarget().toString()- this iscom.google.common.hashtypes = typesByPackage.getOrDefault(target, new HashSet<>())- should containBloomFiltercombinedTypes- should containBloomFilter- Check at line 227:
if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || ...)combinedTypesis NOT empty, so this should be false
- Check at line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName()))qualid.getSimpleName()isBloomFiltersourceIdentifierNamesshould containBloomFilterbecause it appears in the source code
Wait, let me look at
collectSourceIdentifierNamesmore carefully. It collects all identifier simple names from the source. SoBloomFiltershould be in there.- Check at line 269-276: This checks if any type in
combinedTypesmatches the import's type name
Hmm, but wait. Let me look at this more carefully. The condition at line 269-274 is:
else if (combinedTypes.stream().noneMatch(c -> { if ("*".equals(elem.getQualid().getSimpleName())) { return elem.getPackageName().equals(c.getPackageName()); } return fullyQualifiedNamesAreEqual(c.getFullyQualifiedName(), elem.getTypeName()); }) && !unqualifiedTypeNames.contains(elem.getTypeName())) {This checks if
combinedTypescontains a type that matches the import. Forcom.google.common.hash.BloomFilter,combinedTypesshould containBloomFilterFQN.Actually, wait. I think the issue might be in how
typesByPackageis populated. Let me re-read the code:for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { typesByPackage.computeIfAbsent( fq.getOwningClass() == null ? fq.getPackageName() : toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), f -> new HashSet<>()).add(fq); } }For
BloomFilter<T>,TypeUtils.asFullyQualified()should return the underlyingBloomFilterclass. Thenfq.getPackageName()should becom.google.common.hashandfq.getOwningClass()should benull(sinceBloomFilteris a top-level class).So
typesByPackageshould have an entry with keycom.google.common.hashand value containingBloomFilter.Now when checking the import:
target = qualid.getTarget().toString()- Forimport com.google.common.hash.BloomFilter;, the target iscom.google.common.hashtypes = typesByPackage.getOrDefault(target, new HashSet<>())- This should getBloomFilter
Wait, I need to understand the structure of the import better. Let me look at what
qualid.getTarget()returns for different imports.Actually, I think I may have found the issue! Let me look at the check at line 264 again:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) {For the import
com.google.common.hash.BloomFilter,qualid.getSimpleName()isBloomFilter. AndsourceIdentifierNamesshould contain all identifier names from the source. But wait - in the case ofextends AtomicReference<BloomFilter<T>>, theBloomFilteridentifier is inside a type parameter, which is part of a class declaration'sextendsclause.Let me trace through more carefully. In
collectSourceIdentifierNames, it visits all identifiers (except those in imports). TheBloomFilterinAtomicReference<BloomFilter<T>>should be visited.Hmm, let me actually run the test to see what's happening. Let me first look at how to run the tests properly.
Actually, I think I should trace through the code more carefully by looking at what happens for the
BloomFilteridentifier specifically. The issue might be related to howisPartOfQualifiedReferenceworks.Wait, I think I found the actual issue! Let me re-read the condition in
isPartOfQualifiedReference:private boolean isPartOfQualifiedReference(J.Identifier identifier) { Cursor cursor = getCursor(); while (cursor != null) { Object value = cursor.getValue(); 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; } cursor = cursor.getParent(); } return false; }This checks if the identifier is the
namepart of aJ.FieldAccesswhere thetargetis anotherJ.FieldAccesswith aJavaType.FullyQualifiedtype. This is to detect qualified references likecom.google.common.hash.BloomFilter.But in our case,
BloomFilterinAtomicReference<BloomFilter<T>>is just a simple identifier, not part of a field access chain. So this check should returnfalse, andBloomFiltershould be added tounqualifiedTypeNames.Let me think about this differently. Maybe the issue is that the
typefield on theBloomFilteridentifier is not set correctly when it's inside a type parameter.Actually, I just realized something. When the parser processes
AtomicReference<BloomFilter<T>>, theBloomFilteridentifier'stypefield might be set toJavaType.Parameterized(representingBloomFilter<T>) instead of just the class type. SinceJavaType.Parameterized extends FullyQualified, theinstanceofcheck should still pass.Let me check if there's a case where the type might not be set at all, or might be
JavaType.Unknown.Actually, let me just write a simple debug test to understand what's happening. Let me run the existing test suite first to make sure things work.
Wait, I should look at the issue more carefully. Let me re-read the condition at line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { anImport.used = false; changed = true; }This says: if the simple name of the import (e.g.,
BloomFilter) doesn't appear anywhere in the source code (as an identifier), then the import is unused.Now,
collectSourceIdentifierNamesvisits all identifiers in the source code (except imports). So ifBloomFilterappears inAtomicReference<BloomFilter<T>>, it should be collected.Unless... the visitor doesn't visit the type parameters of a class declaration's extends/implements clause?
Let me check how the visitor traverses class declarations. Actually, looking at
collectSourceIdentifierNames, it extendsJavaIsoVisitorand only overridesvisitImport(to skip imports) andvisitIdentifier(to collect names). The default traversal should visit all parts of the AST including type parameters in extends/implements clauses.Hmm, let me check if there's something special about how class declaration extends/implements are visited.
Actually wait - I should check if the issue is specific to type parameters in
extendsclauses. Let me look at the AST structure for a class declaration with an extends clause.Let me just run the actual test and see what happens.
I think the actual problem may be different from what I initially thought. Let me look at the code flow again:
The key check is at line 264:
!sourceIdentifierNames.contains(qualid.getSimpleName())But wait, I see
sourceIdentifierNamesis collected but it's only used at line 264. The flow is:- Line 227: Check if
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(...)or source package check - Line 230: Handle wildcard imports
- Line 264: Check if simple name appears in source
- Line 269: Check if type matches in combinedTypes
For the import
com.google.common.hash.BloomFilter:combinedTypesshould containBloomFilter(if properly populated fromtypesByPackage)- So line 227 check should fail (combinedTypes is not empty)
- Line 230: not a wildcard import
- Line 264:
sourceIdentifierNamesshould containBloomFilter - Line 269:
combinedTypes.stream().noneMatch(...)- should find a match
Wait, I think I need to actually check what happens with
typesByPackage. The issue might be thatBloomFilter<T>(as a type parameter ofAtomicReference<BloomFilter<T>>) is not being properly added totypesByPackage.Let me trace through more carefully:
-
cu.getTypesInUse().getTypesInUse()returns all types used in the compilation unit -
For
BloomFilterRef<T> extends AtomicReference<BloomFilter<T>>:AtomicReference<BloomFilter<T>>is aJavaType.Parameterized- Its type parameters include
BloomFilter<T>, which is also aJavaType.Parameterized
-
In the loop at line 95-118:
- When processing
AtomicReference<BloomFilter<T>>:parameterized.getType()=java.util.concurrent.atomic.AtomicReferenceparameterized.getTypeParameters()=[BloomFilter<T>]- For each type parameter
BloomFilter<T>:TypeUtils.asFullyQualified(BloomFilter<T>)should return the underlyingcom.google.common.hash.BloomFilterclass- This should be added to
typesByPackagewith keycom.google.common.hash
- When processing
-
When checking the import
com.google.common.hash.BloomFilter:target = qualid.getTarget().toString()=com.google.common.hashtypes = typesByPackage.getOrDefault("com.google.common.hash", new HashSet<>())should containBloomFilter
So the logic seems correct. Let me check if maybe the type attribution is not capturing the nested type parameter correctly.
Actually, I think I should just look at what
getTypesInUse()returns for this specific case. The issue might be thatBloomFilter<T>is not intypesInUseat all, or the type parameters are not being tracked.Let me look at
FindTypesInUse.visitType:@Override public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { variables.add((JavaType.Variable) javaType); } else if (javaType instanceof JavaType.Method) { // ... } else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda) && !isFullyQualifiedJavaDocReference(cursor)) { types.add(javaType); } } return javaType; }This adds types to
typesInUsewhen visiting type annotations. But the types insideJavaType.Parameterized.getTypeParameters()are not visited by this method - they're only visited if there's an explicit type tree in the source that refers to them.So when we have
AtomicReference<BloomFilter<T>>, the visitor visits:- The
J.ParameterizedTypeforAtomicReference<BloomFilter<T>>- its.typeisAtomicReference<BloomFilter<T>> - The
J.IdentifierforAtomicReference- its.typeisjava.util.concurrent.atomic.AtomicReference - The
J.ParameterizedTypeforBloomFilter<T>(in type parameters) - its.typeisBloomFilter<T> - The
J.IdentifierforBloomFilter- its.typeiscom.google.common.hash.BloomFilter
Wait, but step 3 and 4 should happen because
visitParameterizedTypeinJavaVisitorvisits the type parameters:public J visitParameterizedType(J.ParameterizedType type, P p) { // ... if (pt.getPadding().getTypeParameters() != null) { pt = pt.getPadding().withTypeParameters(visitContainer(pt.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, p)); } // ... }So the inner
BloomFilter<T>parameterized type should be visited, and its.typeshould be added totypesInUse.Hmm, but wait - the issue might be in how
visitTypeis called. Let me check whatvisitTypeNamesdoes:pt.getPadding().withTypeParameters(visitTypeNames(pt.getPadding().getTypeParameters(), p));This calls
visitTypeNameswhich might visit the types without triggeringvisitTypefor the individual type parameters.Actually, I think the issue is different. Let me look at what
visitTypeactually does. ThevisitTypemethod inJavaVisitoris called when visiting the.typefield of a tree node. So when we visit theJ.ParameterizedTypeforBloomFilter<T>, we callvisitTypeon its.typefield, which isBloomFilter<T>(aJavaType.Parameterized).But the issue is that the
JavaType.ParameterizedforBloomFilter<T>has type parameters that are not visited byvisitType. The type parameters of aJavaType.Parameterizedare part of the type itself, not part of the AST tree.So when
RemoveUnusedImportsprocessestypesInUse, it should findBloomFilter<T>(as aJavaType.Parameterized). Then in the loop:if (javaType instanceof JavaType.Parameterized) { JavaType.Parameterized parameterized = (JavaType.Parameterized) javaType; typesByPackage.computeIfAbsent(parameterized.getType().getPackageName(), f -> new HashSet<>()) .add(parameterized.getType()); for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { typesByPackage.computeIfAbsent( fq.getOwningClass() == null ? fq.getPackageName() : toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), f -> new HashSet<>()).add(fq); } } }For
BloomFilter<T>:parameterized.getType()=com.google.common.hash.BloomFilterparameterized.getType().getPackageName()=com.google.common.hash- So
BloomFiltershould be added totypesByPackage["com.google.common.hash"]
This should work! So why is the import being removed?
Let me check if maybe the issue is with
FindTypesInUse.visitIdentifier:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } else if (parent instanceof J.MethodDeclaration && ((J.MethodDeclaration) parent).getName() == identifier) { // skip method name return identifier; } return super.visitIdentifier(identifier, p); }Ah wait, this skips identifiers whose parent is a
J.ClassDeclaration. But what about identifiers whose parent isJ.ParameterizedType? That should be fine - they'll callsuper.visitIdentifierwhich will eventually callvisitTypeon the identifier's type.Hmm, let me look at this from a different angle. Let me check if maybe the
BloomFilteridentifier's type is not set correctly. In some cases, the parser might not have type information available.Actually, let me look at a potential issue. In
FindTypesInUse.visitIdentifier:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } // ... }The comment says "skip type of class". This means if the identifier is the name of a class declaration (like
BloomFilterRefinclass BloomFilterRef<T>), it won't be added to types in use.But for
BloomFilterinAtomicReference<BloomFilter<T>>, the parent isJ.ParameterizedType, notJ.ClassDeclaration. So this check shouldn't affect it.Let me think about this more... The issue might be that
BloomFilteras a type parameter uses a different code path. Let me check if there's avisitTypeParametermethod that might be skipping things.Actually, I think the real issue might be simpler. Let me re-read
FindTypesInUse.visitIdentifier:Object parent = Objects.requireNonNull(getCursor().getParent()).getValue();This gets the parent of the current node. If the current node is the
BloomFilteridentifier, the parent would be theJ.ParameterizedTypeforBloomFilter<T>. This is not aJ.ClassDeclaration, so it should proceed tosuper.visitIdentifier.But wait, in
JavaIsoVisitor.visitIdentifier, it callsvisitTypeon the identifier's type. Let me trace through:FindTypesInUse.visitIdentifieris called forBloomFilteridentifier- Parent is
J.ParameterizedType, notJ.ClassDeclaration, so it callssuper.visitIdentifier(identifier, p) JavaIsoVisitor.visitIdentifiercallsvisitType(identifier.getType(), p)FindTypesInUse.visitTypeis called withBloomFiltertype- The cursor value is the
J.Identifier(notJ.ClassDeclarationorJ.Lambda) - So
types.add(javaType)is called
This should add
com.google.common.hash.BloomFiltertotypesInUse. Then inRemoveUnusedImports, it should be found intypesByPackage.I'm going to try running a test to confirm this behavior. Let me create a simple test file.
Actually, let me look more carefully at the structure. In
AtomicReference<BloomFilter<T>>, theclazzof the outerJ.ParameterizedTypeisAtomicReference(an identifier). The type parameters contain anotherJ.ParameterizedTypeforBloomFilter<T>, whoseclazzisBloomFilter(another identifier).When visiting the outer
J.ParameterizedType:visitParameterizedTypeis called- It visits
clazzwhich isAtomicReferenceidentifier - It visits type parameters, which includes
BloomFilter<T> - For
BloomFilter<T>,visitParameterizedTypeis called again - It visits
clazzwhich isBloomFilteridentifier - For
BloomFilteridentifier,visitIdentifieris called - In
visitIdentifier, the parent isJ.ParameterizedType(forBloomFilter<T>)
Wait, but the issue is that in step 7, the parent check is for
J.ClassDeclaration.J.ParameterizedTypeis not aJ.ClassDeclaration, so it should proceed.OK let me just run a test and debug this. I'll create a temporary test file.
Actually wait, I think I found a potential issue! Let me look at
FindTypesInUse.visitTypemore carefully:@Override public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { variables.add((JavaType.Variable) javaType); } else if (javaType instanceof JavaType.Method) { // ... } else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda) && !isFullyQualifiedJavaDocReference(cursor)) { types.add(javaType); } } return javaType; }The check
!(cursor.getValue() instanceof J.ClassDeclaration)- this means if the current cursor value is aJ.ClassDeclaration, the type won't be added. But when visiting theBloomFilteridentifier, the cursor value is theJ.Identifier, not aJ.ClassDeclaration. So this check should pass.Hmm, I'm running out of ideas. Let me just create a test and run it to see what's happening.
Wait, I just thought of something. In the case of
BloomFilterRef<T> extends AtomicReference<BloomFilter<T>>, theBloomFiltertype is used in a superclass declaration. Let me check if there's something special about how superclass types are handled.Actually, I think I need to check if the
BloomFilter<T>type parameter is even intypesInUse. TheFindTypesInUsevisitor might not be visiting the type parameters of a superclass's parameterized type correctly.Let me check how class declarations are visited:
// In JavaVisitor public J visitClassDeclaration(J.ClassDeclaration classDecl, P p) { // ... if (classDecl.getImplements() != null) { classDecl = classDecl.getPadding().withImplements(visitContainer(classDecl.getPadding().getImplements(), JContainer.Location.IMPLEMENTS, p)); } classDecl = classDecl.getPadding().withImplements(visitTypeNames(classDecl.getPadding().getImplements(), p)); // ... }Let me see if
withSuperclassis also visited...Actually, I think I should just look at the actual test output. Let me run the test.
Actually, I just realized something important. Let me look at
FindTypesInUse.visitIdentifieragain:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } else if (parent instanceof J.MethodDeclaration && ((J.MethodDeclaration) parent).getName() == identifier) { // skip method name return identifier; } return super.visitIdentifier(identifier, p); }When the parent is
J.ClassDeclaration, it skips the identifier. But this is for the case where the identifier is the class name itself (e.g.,BloomFilterRefinclass BloomFilterRef<T>). This is correct - we don't want to add the class name to types in use.For
BloomFilterinAtomicReference<BloomFilter<T>>, the parent isJ.ParameterizedType, notJ.ClassDeclaration. So it should callsuper.visitIdentifier.OK I think the issue might be somewhere else. Let me look at the condition in
RemoveUnusedImportsmore carefully. The import check logic at line 227:if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")) { anImport.used = false; changed = true; }Wait, I see a potential operator precedence issue! Let me check:
(combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName()))- this is grouped|| sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")- this is separate
So the condition is:
(A && B) || (C && D && E)If the source package equals the import's package (unlikely for
com.google.common.hash), the second part could be true. But more importantly, ifcombinedTypesis empty ANDunqualifiedTypeNamesdoesn't contain the type name, the import is marked unused.So the question is: is
combinedTypesempty when checking theBloomFilterimport?combinedTypesis derived fromtypesByPackage.getOrDefault(target, new HashSet<>())wheretarget = qualid.getTarget().toString(). For the importcom.google.common.hash.BloomFilter, the target would becom.google.common.hash.So
typesByPackageneeds to have an entry with keycom.google.common.hashthat containsBloomFilter.Let me trace through how
typesByPackageis populated:for (JavaType javaType : cu.getTypesInUse().getTypesInUse()) { if (javaType instanceof JavaType.Parameterized) { JavaType.Parameterized parameterized = (JavaType.Parameterized) javaType; typesByPackage.computeIfAbsent(parameterized.getType().getPackageName(), f -> new HashSet<>()) .add(parameterized.getType()); // ... } else if (javaType instanceof JavaType.FullyQualified) { JavaType.FullyQualified fq = (JavaType.FullyQualified) javaType; typesByPackage.computeIfAbsent( fq.getOwningClass() == null ? fq.getPackageName() : toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), f -> new HashSet<>()).add(fq); } }For
BloomFilter<T>(aJavaType.Parameterized):parameterized.getType()=com.google.common.hash.BloomFilterparameterized.getType().getPackageName()=com.google.common.hashBloomFilteris added totypesByPackage["com.google.common.hash"]
Wait, but this only happens if
BloomFilter<T>is intypesInUse. Let me check if it is.When
FindTypesInUsevisits theJ.ParameterizedTypeforBloomFilter<T>, it callsvisitTypeon its.typefield. The.typefield of theJ.ParameterizedTypeforBloomFilter<T>is theJavaType.ParameterizedforBloomFilter<T>.In
FindTypesInUse.visitType:} else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda) && !isFullyQualifiedJavaDocReference(cursor)) { types.add(javaType); }The cursor value when
visitTypeis called is the parent node of the type being visited. ForJ.ParameterizedType, whenvisitTypeis called, the cursor value should be theJ.ParameterizedTypeitself (or maybe the container?).Hmm, let me look at how
visitTypeis called inJavaVisitor.visitParameterizedType:public J visitParameterizedType(J.ParameterizedType type, P p) { // ... return pt.withType(visitType(pt.getType(), p)); }When
visitTypeis called, the cursor is still pointing at theJ.ParameterizedTypenode. Socursor.getValue()returns theJ.ParameterizedType, which is not aJ.ClassDeclarationorJ.Lambda. So the type should be added.I'm pretty confident the logic should work. Let me just run a test to see what's happening.
Let me think about this more carefully. When the visitor is at a specific node, the cursor points to that node. When
visitTypeis called, the cursor is at theJ.ParameterizedTypenode. ThevisitTypemethod inJavaIsoVisitordoes:public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { // It does NOT push a new cursor frame return javaType; }Wait, but
JavaIsoVisitorextendsJavaVisitorwhich extendsTreeVisitor. Let me check howvisitTypeis called and what the cursor looks like.Actually,
visitTypeis called directly by the visitor methods (likevisitParameterizedType), not through the cursor mechanism. So whenvisitTypeis called, the cursor is still pointing at the node that calledvisitType(e.g.,J.ParameterizedType).OK I think I've been overthinking this. Let me just create a test and run it to see the actual behavior.
Actually, wait. I just realized I should check what happens with the
clazzidentifier in aJ.ParameterizedType. When visitingAtomicReference<BloomFilter<T>>:visitParameterizedTypeis called forAtomicReference<BloomFilter<T>>- It calls
visitAndCast(pt.getClazz(), p)which visits theAtomicReferenceidentifier - For the
AtomicReferenceidentifier,visitIdentifieris called visitTypeis called onAtomicReference's type, which isjava.util.concurrent.atomic.AtomicReference
But wait, the
.typefield of theAtomicReferenceidentifier - what is it? Is itAtomicReference(the class) orAtomicReference<BloomFilter<T>>(the parameterized type)?Looking at how the Java parser works, the
.typefield of an identifier in a parameterized type should be the base class type, not the parameterized type. SoAtomicReferenceidentifier's type would bejava.util.concurrent.atomic.AtomicReference, not the parameterized type.But the
J.ParameterizedTypeitself has a.typefield that is the parameterized typeAtomicReference<BloomFilter<T>>. So whenvisitParameterizedTypecallsvisitType(pt.getType(), p), it's visiting the full parameterized type.OK I think I need to just run the code and debug it. Let me create a test file.
Hmm wait, I realize I should look at this from a different angle. The issue says the import IS being removed. So somewhere, the check is failing. Let me look at the specific checks again.
For the import
com.google.common.hash.BloomFilter:- Line 216:
target = qualid.getTarget().toString()=com.google.common.hash - Line 217:
types = typesByPackage.getOrDefault(target, new HashSet<>())- this should containBloomFilterifBloomFilter<T>was intypesInUse - Line 218:
typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>())-toFullyQualifiedName("com.google.common.hash")returns... hmm, what does this do?
Let me check
toFullyQualifiedName:<tool_call> <function=bash_terminal> <parameter=command> grep -n "toFullyQualifiedName" /rewrite/rewrite-java/src/main/java/org/openrewrite/java/tree/TypeUtils.java | head -10 </tool_call>
- The outer type
Assistant
Let me search for the
toFullyQualifiedNamemethod in TypeUtils.java to understand what it does.Assistant
OK, so
toFullyQualifiedNamejust replaces$with.. For a package name likecom.google.common.hash, this doesn't change anything.Now let me think about this differently. The issue is that the import is being removed. Let me trace through the exact conditions again.
For the import
com.google.common.hash.BloomFilter:target = qualid.getTarget().toString()=com.google.common.hashcombinedTypes= types fromtypesByPackage.getOrDefault("com.google.common.hash")merged withtypesByPackage.getOrDefault("com.google.common.hash")
If
combinedTypescontainsBloomFilter, then:- Line 227:
combinedTypes.isEmpty()is false, so the first part is false. IfsourcePackagedoesn't equalcom.google.common.hash, the second part is also false. So we don't mark as unused here. - Line 230: Not a wildcard import, so skip.
- Line 264:
sourceIdentifierNames.contains("BloomFilter")should be true, so we don't mark as unused here. - Line 269:
combinedTypes.stream().noneMatch(c -> fullyQualifiedNamesAreEqual(c.getFullyQualifiedName(), elem.getTypeName()))- this should findBloomFilterand return false, so we don't mark as unused.
So if everything is working correctly, the import should NOT be removed. But the bug report says it IS being removed. So something is not working as expected.
Let me think about what could go wrong:
BloomFilter<T>is not intypesInUse- This would meancombinedTypesis emptyBloomFilteridentifier is not visited - This would meansourceIdentifierNamesdoesn't containBloomFilterBloomFilteris not inunqualifiedTypeNames- This would fail the check at line 274
Let me look at option 2 more carefully. The
collectSourceIdentifierNamesvisitor visits all identifiers except those in imports. ForBloomFilterinAtomicReference<BloomFilter<T>>:- The
BloomFilteridentifier is part of aJ.ParameterizedTypewhich is inside anotherJ.ParameterizedTypewhich is in thesuperclassof a class declaration.
The visitor should traverse this correctly because it's a
JavaIsoVisitorthat doesn't skip any nodes (except imports).But wait! Let me check if there's something special about how the superclass of a class declaration is visited. Maybe the superclass type parameters are not being visited?
Actually, let me look at
JavaIsoVisitor.visitClassDeclaration:<tool_call> <function=bash_terminal> <parameter=command> grep -n "visitClassDeclaration" /rewrite/rewrite-java/src/main/java/org/openrewrite/java/JavaIsoVisitor.java </tool_call>
Assistant
Let me search for how class declarations are visited in the JavaIsoVisitor.
Assistant
The JavaIsoVisitor just delegates to the parent's visitClassDeclaration. Let me check the JavaVisitor's implementation to see how the superclass is visited.
Assistant
Let me check how the superclass is handled in
visitClassDeclaration.Assistant
Looking at the code, I need to understand how the extends clause is visited. Let me check how
visitLeftPaddedworks and trace through the logic more carefully.Assistant
Looking at line 498-500, I see that the
extendsclause is visited viavisitLeftPaddedfor the left-padded element, and thenvisitTypeNameis called. But I notice that the actual type expression (likeAtomicReference<BloomFilter<T>>) might not be getting visited properly.Let me look at what
visitLeftPaddeddoes for the EXTENDS location, and whether it actually visits the element inside the left-padded (which would be the superclass type expression).Assistant
The
visitLeftPaddedmethod does visit the element inside the JLeftPadded viavisitAndCast. So when visiting the extends clause, it should visit theAtomicReference<BloomFilter<T>>parameterized type, which should then recursively visit theBloomFilter<T>parameterized type inside it.So the visitor traversal should be working correctly. The issue must be somewhere else.
Let me think about this more carefully. The key question is: why is the
BloomFilterimport being removed?Let me look at the actual check logic again. The issue is at line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { anImport.used = false; changed = true; }This checks if the simple name of the import appears in the source code. For
BloomFilter, this should be true becauseBloomFilterappears inAtomicReference<BloomFilter<T>>.But wait - what if
collectSourceIdentifierNamesis not visiting the extends clause properly? Let me check the visitor...Actually,
collectSourceIdentifierNamesextendsJavaIsoVisitorand only overridesvisitImport(to skip imports) andvisitIdentifier(to collect names). The default traversal should visit all parts of the AST.Hmm, let me create a test to verify this behavior.
Actually, I just realized I should look at what the
qualidof an import looks like. Forimport com.google.common.hash.BloomFilter;, thequalidis aJ.FieldAccesstree representing the full path. Let me check whatqualid.getSimpleName()returns.<tool_call> <function=bash_terminal> <parameter=command> grep -n "getSimpleName" /rewrite/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java | head -20 </tool_call>
Assistant
Let me search for getSimpleName in J.java properly.
Assistant
Let me check the
getSimpleNamefor FieldAccess which is used for import qualid.Assistant
Let me create a minimal test to reproduce this issue and run it:
Assistant
I need to run the test to see if it fails as expected. Let me execute the gradle test command.
Assistant
The test file needs the RecipeSpec import. Let me fix the test file.
Assistant
Let me run the test again to see if it fails.
Assistant
Interesting - the test passed! This means my reproduction might not be exactly matching the issue. Let me investigate further to understand when the bug actually occurs. The difference might be related to how external library types are handled vs inline types.
Let me check the version of OpenRewrite in this repository:
Assistant
Let me try a test case using JDK types instead of custom inline types, since the issue might be related to how external library types are handled.
Assistant
Let me run the tests to see if the Map.Entry test case works.
Assistant
The tests pass. Let me check if this issue might have already been fixed in this version, or if there's something specific about the original issue that I'm missing. Let me check the git log for recent changes to RemoveUnusedImports.
Assistant
Let me check if this issue was already fixed in this version by looking at the recent changes.
Assistant
I see there was a fix for parameterized types. Let me check if the issue is already fixed in this repository version.
Assistant
This is the latest version of the repository. Let me check if the specific issue mentioned in the bug report still exists. The bug report mentions OpenRewrite v8.31.1. Let me see if this repository has a different version.
Assistant
Let me try the command again properly.
Assistant
Let me look at the historical fix for parameterized types to understand the pattern:
Assistant
Let me continue investigating the issue. I found that there was a previous fix for parameterized types (commit 7b99b434a). My tests pass, which suggests the issue might have been fixed already. But let me investigate more to understand if there's a specific case that's not covered.
Let me look at what the original fix did and see if there's still a gap.
Assistant
The fix from commit 7b99b434a was specifically for handling
JavaType.Parameterizedwhen building thetypesByPackagemap. The fix extracts the type parameters from parameterized types and adds their underlying types to the map.But my tests are passing, which suggests either:
- The issue was already fixed
- The test environment doesn't reproduce the exact conditions
Let me look more carefully at the current code and check if there's still a gap. The current code has a similar fix, but let me check the exact implementation.
Actually, wait - I see that the fix in commit 7b99b434a used a
packageKeyfunction, but the current code doesn't use that function. Let me check what the current code looks like and see if there's a difference.Let me also check if there's something specific about how the extends clause's type parameters are being handled. The issue might be related to how types in the superclass declaration's type parameters are being collected.
Actually, I think I need to trace through more carefully. The issue is that when we have:
public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }The
BloomFiltertype is used inside a type parameter of the superclass. The question is whetherBloomFilter<T>(as a type parameter) is being properly added totypesInUseand then totypesByPackage.Let me look at how
FindTypesInUsehandles the extends clause of a class declaration. The extends clause containsAtomicReference<BloomFilter<T>>, which is aJ.ParameterizedType. When visiting this:- The visitor visits the
J.ParameterizedTypeforAtomicReference<BloomFilter<T>> - The
.typeof this isAtomicReference<BloomFilter<T>>(aJavaType.Parameterized) visitTypeadds this totypesInUse
But the type parameters of
AtomicReference<BloomFilter<T>>(which includesBloomFilter<T>) are part of theJavaType.Parameterizedtype, not part of the AST. So they won't be visited by the AST visitor.However, the fix in commit 7b99b434a handles this by iterating over the type parameters of parameterized types when building
typesByPackage:for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { typesByPackage.computeIfAbsent(...).add(fq); } }So the question is: why isn't this working in the bug report's case?
Wait, let me re-check my test. My test uses an inline definition of
BloomFilter<T>in the same test, which means the type information is available. The bug report might be about a case where the type information is not available (e.g., when using an external library without type attribution).Actually, looking at the RemoveUnusedImports recipe more carefully, I see it has a precondition:
return Preconditions.check(new NoMissingTypes(), new RemoveUnusedImportsVisitor());This means the recipe only runs if there are no missing types. So type information should be available.
Let me think about this more... The issue might be that when
BloomFilteris used as a type parameter inside another type parameter (i.e.,AtomicReference<BloomFilter<T>>), theBloomFilter<T>type parameter is not being visited by the AST visitor because it's nested inside the type parameters of another parameterized type.Wait, but I already checked that the
JavaVisitor.visitParameterizedTypemethod visits the type parameters:if (pt.getPadding().getTypeParameters() != null) { pt = pt.getPadding().withTypeParameters(visitContainer(pt.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, p)); }So the inner
BloomFilter<T>parameterized type should be visited, and its.typeshould be added totypesInUse.Hmm, but wait. Let me check if the type information is properly attributed for the inner parameterized type. In some cases, the parser might not have complete type information for nested type parameters.
Actually, let me re-read the issue description more carefully:
The user is using:
- OpenRewrite v8.31.1
- Maven plugin v6.40.0
The code is:
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }And the
BloomFilterimport is being removed, leaving:import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }This is definitely a bug. But my test passes, so either:
- The bug was fixed after v8.31.1
- The test environment doesn't reproduce the exact conditions
Let me check if there's a specific condition that causes the bug. Maybe it's related to how the type is used in the extends clause specifically.
Actually, I just realized something. Let me check the
FindTypesInUse.visitIdentifiermethod again:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } else if (parent instanceof J.MethodDeclaration && ((J.MethodDeclaration) parent).getName() == identifier) { // skip method name return identifier; } return super.visitIdentifier(identifier, p); }The parent of the
BloomFilteridentifier (inAtomicReference<BloomFilter<T>>) is aJ.ParameterizedType. This is not aJ.ClassDeclaration, so it should callsuper.visitIdentifier(identifier, p), which will visit the identifier's type.But wait, what if the parent is actually something else? Let me trace through the cursor more carefully.
When visiting the
J.ParameterizedTypeforBloomFilter<T>:visitParameterizedTypeis called- It calls
pt.withClazz(visitAndCast(pt.getClazz(), p))which visits theBloomFilteridentifier - When visiting the identifier, the cursor is at the identifier
- The parent cursor is at the
J.ParameterizedTypeforBloomFilter<T>
So the parent should be
J.ParameterizedType, notJ.ClassDeclaration. This means the identifier should be visited normally.OK so my tests pass. Let me try to understand if there's a different scenario that triggers the bug. Maybe it's related to the specific Java parser version or some edge case with type attribution.
Let me also check if there are any recent changes related to this issue in the repository.
Actually, wait - I just realized I should check if the test framework is actually running the recipe correctly. Maybe the test passes because of some default behavior in the test framework that doesn't apply in the real Maven plugin execution.
Let me look at the test more carefully and check if the recipe is actually being run.
Actually, I think my tests are passing because the types are properly attributed when using inline type definitions. The bug might occur when the types are from external libraries that are not fully attributed.
Let me check if there's a way to test with type attribution disabled or with missing type information.
Hmm, actually, looking at the recipe again, I see:
return Preconditions.check(new NoMissingTypes(), new RemoveUnusedImportsVisitor());This means if there are missing types, the recipe won't run at all. So the bug must be happening when types are properly attributed but the import is still being removed.
Let me look at the code flow again. The key checks are:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())- import is unused if no types from this package are used and the type name isn't found in unqualified references!sourceIdentifierNames.contains(qualid.getSimpleName())- import is unused if the simple name doesn't appear in sourcecombinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())- import is unused if no matching type is found and the type name isn't in unqualified references
For the
BloomFilterimport to be incorrectly removed, one of these conditions must be true. Let me think about which one could be true:-
If
combinedTypesis empty, that meanstypesByPackage["com.google.common.hash"]is empty. This could happen ifBloomFilter<T>is not intypesInUseor if the type parameter extraction is not working. -
If
sourceIdentifierNamesdoesn't containBloomFilter, that means the identifier visitor didn't find theBloomFilteridentifier. This could happen if the extends clause is not being visited. -
If
unqualifiedTypeNamesdoesn't containcom.google.common.hash.BloomFilter, that means the identifier visitor didn't find the type reference. This could happen if the type on the identifier is not set correctly.
I think the most likely issue is #1 -
BloomFilter<T>is not being added totypesInUseortypesByPackage.Let me check if there's something specific about how the extends clause is handled. In
FindTypesInUse.visitType:} else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda) && !isFullyQualifiedJavaDocReference(cursor)) { types.add(javaType); }When
visitTypeis called for theAtomicReference<BloomFilter<T>>type (from the extends clause), the cursor value is theJ.ParameterizedTypeforAtomicReference<BloomFilter<T>>. This is not aJ.ClassDeclaration, so the type should be added.And then in
RemoveUnusedImports.visitCompilationUnit, when processingAtomicReference<BloomFilter<T>>(aJavaType.Parameterized):parameterized.getType()=java.util.concurrent.atomic.AtomicReferenceparameterized.getTypeParameters()=[BloomFilter<T>]- For
BloomFilter<T>,TypeUtils.asFullyQualified()should returncom.google.common.hash.BloomFilter
This should work. Let me verify that
TypeUtils.asFullyQualifiedhandlesJavaType.Parameterizedcorrectly.Actually, I think I need to just run the test and add some debug output to understand what's happening. But since my tests pass, let me check if there's a specific edge case that my tests don't cover.
Wait, I just noticed something. In my test, I define
BloomFilter<T>inline:java( """ package com.google.common.hash; public class BloomFilter<T> {} """ )But in the real case,
BloomFilteris from an external library (Google Guava). The difference might be in how type attribution works with external vs inline types.Let me try a test that doesn't define the type inline and see if it still passes.
Actually, looking at the test framework, when you provide multiple
java()sources, they're all compiled together with type attribution. So my test should be equivalent to using an external library.Let me try running the test with explicit type validation to make sure type attribution is working correctly.
Actually, let me re-check the issue. Maybe the problem is more subtle. Let me look at what happens when the
BloomFiltertype is used in a nested type parameter likeAtomicReference<BloomFilter<T>>.The AST structure for the extends clause would be:
J.ParameterizedType (AtomicReference<BloomFilter<T>>) - clazz: J.Identifier (AtomicReference) - typeParameters: - J.ParameterizedType (BloomFilter<T>) - clazz: J.Identifier (BloomFilter) - typeParameters: - J.Identifier (T)When the visitor visits this structure:
- Visit
J.ParameterizedTypeforAtomicReference<BloomFilter<T>> - Visit
J.IdentifierforAtomicReference - Visit type parameters
- Visit
J.ParameterizedTypeforBloomFilter<T> - Visit
J.IdentifierforBloomFilter - Visit type parameters
- Visit
J.IdentifierforT
At step 5, when visiting the
BloomFilteridentifier:- Its
.typeshould becom.google.common.hash.BloomFilter(aJavaType.FullyQualified) - Its parent is
J.ParameterizedType(forBloomFilter<T>)
In
FindTypesInUse.visitIdentifier:- The parent is
J.ParameterizedType, notJ.ClassDeclaration - So it calls
super.visitIdentifier(identifier, p) - This eventually calls
visitTypeonBloomFilter's type - The cursor value is the
J.IdentifierforBloomFilter visitTypeaddsBloomFiltertotypes
So
BloomFiltershould be intypesInUse. Then inRemoveUnusedImports, it should be added totypesByPackage.OK I think I need to actually look at the code and find the bug. Let me look at the code again and trace through more carefully.
Actually, I just realized something important. Let me look at
FindTypesInUse.visitType:@Override public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { variables.add((JavaType.Variable) javaType); } else if (javaType instanceof JavaType.Method) { // ... } else if (!(cursor.getValue() instanceof J.ClassDeclaration) && !(cursor.getValue() instanceof J.Lambda) && !isFullyQualifiedJavaDocReference(cursor)) { types.add(javaType); } } return javaType; }The key check is
!(cursor.getValue() instanceof J.ClassDeclaration). When visiting theBloomFilteridentifier, the cursor value is theJ.IdentifierforBloomFilter. This is not aJ.ClassDeclaration, so the type should be added.But wait, when is
visitTypecalled for theBloomFilteridentifier? It's called fromvisitIdentifier:In
JavaVisitor.visitIdentifier:public J visitIdentifier(J.Identifier identifier, P p) { J.Identifier i = identifier; i = i.withPrefix(visitSpace(i.getPrefix(), Space.Location.IDENTIFIER_PREFIX, p)); i = i.withMarkers(visitMarkers(i.getMarkers(), p)); Expression temp = (Expression) visitExpression(i, p); if (!(temp instanceof J.Identifier)) { return temp; } else { i = (J.Identifier) temp; } i = i.withAnnotations(ListUtils.map(i.getAnnotations(), a -> visitAndCast(a, p))); i = i.withSimpleName(visitString(i.getSimpleName(), Space.Location.IDENTIFIER_NAME, p)); return i.withType(visitType(i.getType(), p)); }When
visitType(i.getType(), p)is called, the cursor is still at theJ.Identifiernode (becausevisitIdentifierdoesn't push a new cursor frame forvisitType).Wait, actually, let me check how the cursor works. When
visitIdentifieris called, does it push a new cursor frame?Looking at
JavaIsoVisitor.visitIdentifier:public J.Identifier visitIdentifier(J.Identifier identifier, P p) { return (J.Identifier) super.visitIdentifier(identifier, p); }And
JavaVisitor.visitIdentifierdoesn't usesetCursordirectly, but it might be inherited fromTreeVisitor.Let me check if the visitor uses the cursor mechanism properly. Actually,
JavaIsoVisitorextendsJavaVisitorwhich extendsTreeVisitor. TheTreeVisitor.visitmethod typically pushes a new cursor frame before calling the specific visit method.So when
visitIdentifieris called for theBloomFilteridentifier, the cursor should be pointing at theJ.IdentifierforBloomFilter. And whenvisitTypeis called, the cursor is still at theJ.Identifier.So
cursor.getValue()returns theJ.IdentifierforBloomFilter. This is not aJ.ClassDeclaration, so the type should be added.I'm really confused about why the bug would occur. My tests pass, so maybe the bug was already fixed.
But wait, let me check if the bug report is about a specific version (v8.31.1) and if this repository has a newer version with the fix already applied.
Let me look at the repository's current version.
Actually, let me check the git history more carefully. The issue says "RemoveUnusedImports removes imports when used in Generics only". This suggests the import is being removed even though it's used in generics. The fix in commit 7b99b434a was specifically for handling parameterized types when building
typesByPackage.Let me check if there's a more recent fix that I'm missing.
Actually, looking at the git log, I see several recent commits related to RemoveUnusedImports:
- 59d98eba8 Fix RemoveUnusedImports removing imports used as static field qualifiers (#7383)
- 330504939 Fix RemoveUnusedImports keeping import only referenced in type attribution (#7336)
- 2fe5ea8e4 Prevent duplicate imports when unfolding star imports (#7001)
Let me check if any of these are related to the generics issue.
Let me look at the current code more carefully to see if there's still a bug. Let me trace through the logic for the specific case of
AtomicReference<BloomFilter<T>>.When
typesByPackageis populated:-
For
AtomicReference<BloomFilter<T>>(aJavaType.Parameterized):parameterized.getType()=java.util.concurrent.atomic.AtomicReference- This is added to
typesByPackage["java.util.concurrent.atomic"] - For type parameter
BloomFilter<T>:TypeUtils.asFullyQualified(BloomFilter<T>)should returncom.google.common.hash.BloomFilterfq.getOwningClass()= null (sinceBloomFilteris a top-level class)- So
fq.getPackageName()=com.google.common.hash BloomFilteris added totypesByPackage["com.google.common.hash"]
-
When checking the
com.google.common.hash.BloomFilterimport:target = qualid.getTarget().toString()=com.google.common.hashtypes = typesByPackage.getOrDefault("com.google.common.hash", ...)should containBloomFiltercombinedTypesshould containBloomFilter- So the import should NOT be marked as unused
This all seems correct. Let me check if there's something wrong with
TypeUtils.asFullyQualifiedwhen the type parameter is itself aJavaType.Parameterized.Actually, let me look at the
TypeUtils.asFullyQualifiedmethod:public static @Nullable JavaType.FullyQualified asFullyQualified(@Nullable JavaType javaType) { return javaType instanceof JavaType.FullyQualified ? (JavaType.FullyQualified) javaType : null; }So
asFullyQualifiedreturns the type if it's aJavaType.FullyQualified. SinceJavaType.Parameterized extends FullyQualified,asFullyQualified(BloomFilter<T>)should returnBloomFilter<T>as aJavaType.FullyQualified.So the package name and owning class should be correct. The logic should work.
OK let me try a different approach. Maybe the bug is in a different part of the code. Let me look at the
collectUnqualifiedTypeNamesmethod again.The issue might be that
BloomFilteris not being added tounqualifiedTypeNames. Let me check the condition:if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) { if (!isPartOfQualifiedReference(identifier)) { unqualifiedTypeNames.add(((JavaType.FullyQualified) type).getFullyQualifiedName()); } }For the
BloomFilteridentifier:type=com.google.common.hash.BloomFilter(aJavaType.FullyQualifiedorJavaType.Parameterized)identifier.getFieldType()= null (since it's a type reference, not a field reference)isPartOfQualifiedReference(identifier)should return false (since it's not part of a fully qualified reference)
So
BloomFiltershould be added tounqualifiedTypeNameswith FQNcom.google.common.hash.BloomFilter.Wait, but what if
typeisnullorJavaType.Unknown? That could happen if the type attribution is incomplete.Actually, looking at the check:
if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {If
typeisnullor not aJavaType.FullyQualified, this check fails and the type name is not added.So the bug might be that the
BloomFilteridentifier's type is not set correctly (e.g., it'snullorJavaType.Unknown).But that doesn't make sense because the
NoMissingTypesprecondition should ensure that all types are known.Hmm, let me look at this from a different angle. Maybe the issue is not with the type attribution but with how the check logic works.
Actually, wait. I just noticed something. Looking at the check at line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { anImport.used = false; changed = true; }This check happens BEFORE the check at line 269 that uses
combinedTypes. So ifsourceIdentifierNamesdoesn't containBloomFilter, the import will be marked as unused regardless ofcombinedTypes.But
collectSourceIdentifierNamesvisits all identifiers (except imports) and collects their simple names. SoBloomFiltershould be insourceIdentifierNames.Unless... the identifier is not being visited? Let me check if there's something special about the extends clause that might prevent the identifier from being visited.
Actually, let me look at how
collectSourceIdentifierNamesworks:private Set<String> collectSourceIdentifierNames(J.CompilationUnit cu) { return new JavaIsoVisitor<Set<String>>() { @Override public J.Import visitImport(J.Import import_, Set<String> names) { return import_; } @Override public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> names) { names.add(identifier.getSimpleName()); return super.visitIdentifier(identifier, names); } }.reduce(cu, new HashSet<>()); }This extends
JavaIsoVisitorand overrides:visitImport- to skip imports (returns without visiting children)visitIdentifier- to collect all identifier names
The
visitImportoverride just returns the import without visiting its children. This is correct because we don't want to collect names from imports.The
visitIdentifieroverride adds the simple name to the set and then callssuper.visitIdentifier. This should collect all identifier names from the source code.But wait,
JavaIsoVisitor.visitIdentifiercallssuper.visitIdentifierwhich visits all children. So this should recursively visit all identifiers in the compilation unit.Unless there's something special about how
reduceworks? Let me check ifreduceproperly visits all nodes.Actually,
reduceis a standard visitor pattern that visits the entire tree and accumulates a result. It should visit all nodes.OK I'm going to try a different approach. Let me look at the actual test failure scenario more carefully. Maybe the bug only occurs in certain conditions that my test doesn't cover.
Actually, let me re-read the issue description. The user is using:
- OpenRewrite v8.31.1
- Maven plugin v6.40.0
The recipe is
org.openrewrite.java.RemoveUnusedImports.The code is:
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }And the
BloomFilterimport is being removed.Now, my tests pass. But the tests use inline type definitions. Maybe the bug only occurs with certain parser configurations or when type attribution is different.
Actually, let me check if there's a difference between how the test framework and the Maven plugin handle type attribution.
Actually, you know what, let me just look at the code more carefully to see if there's an edge case I'm missing. Let me focus on the specific case of a type used in a generic type parameter of a superclass.
Actually, I just realized something! Let me look at the issue from the perspective of
collectUnqualifiedTypeNames. This method visits identifiers and checks if they're type references.The
BloomFilteridentifier inAtomicReference<BloomFilter<T>>is theclazzof aJ.ParameterizedType. When visiting this identifier:type = identifier.getType()- This is the type of the identifier- Check
type instanceof JavaType.FullyQualified- This should be true forBloomFilter - Check
identifier.getFieldType() == null- This should be true for a type reference - Check
!isPartOfQualifiedReference(identifier)- This should be true (not a qualified reference)
But wait, what if the identifier's type is
JavaType.Parameterized(forBloomFilter<T>) instead ofJavaType.FullyQualified(forBloomFilter)? Let me check...Actually,
JavaType.Parameterized extends FullyQualified, so theinstanceofcheck should still pass. But the FQN would be different - it would becom.google.common.hash.BloomFilter(the underlying class type).Wait, let me check what
JavaType.Parameterized.getFullyQualifiedName()returns. SinceParameterized extends FullyQualified, it should return the FQN of the underlying type, which iscom.google.common.hash.BloomFilter.So
unqualifiedTypeNamesshould containcom.google.common.hash.BloomFilter.OK I think I need to actually look at the specific case more carefully. Let me trace through what happens when the import is checked.
For the import
com.google.common.hash.BloomFilter:elem.getTypeName()=com.google.common.hash.BloomFilterqualid.getTarget().toString()=com.google.common.hashqualid.getSimpleName()=BloomFilter
The checks are:
-
Line 227:
(combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")combinedTypes.isEmpty()- should be false (containsBloomFilter)- First part is false
sourcePackage.equals(elem.getPackageName())- probably false (unless the class is incom.google.common.hashpackage)- So this check doesn't mark the import as unused
-
Line 264:
!sourceIdentifierNames.contains(qualid.getSimpleName())sourceIdentifierNamesshould containBloomFilter- So this check doesn't mark the import as unused
-
Line 269-274:
combinedTypes.stream().noneMatch(c -> fullyQualifiedNamesAreEqual(c.getFullyQualifiedName(), elem.getTypeName())) && !unqualifiedTypeNames.contains(elem.getTypeName())combinedTypesshould containBloomFilterfullyQualifiedNamesAreEqual("com.google.common.hash.BloomFilter", "com.google.common.hash.BloomFilter")should be true- So
noneMatchreturns false - The check doesn't mark the import as unused
So if everything is working correctly, the import should NOT be removed. But the bug report says it IS being removed.
This means one of these conditions must be true in the bug case:
combinedTypesis empty (i.e.,typesByPackage["com.google.common.hash"]is empty)sourceIdentifierNamesdoesn't containBloomFilterunqualifiedTypeNamesdoesn't containcom.google.common.hash.BloomFilter
Let me think about which one could be true...
Actually, I wonder if the issue is with the
typesByPackagekey. Let me look at how the key is computed:For
BloomFilter<T>(when iterating type parameters ofAtomicReference<BloomFilter<T>>):fq = TypeUtils.asFullyQualified(BloomFilter<T>)=BloomFilter<T>(as aJavaType.FullyQualified)fq.getOwningClass()= null (sinceBloomFilteris a top-level class)- So the key is
fq.getPackageName()=com.google.common.hash
For the import
com.google.common.hash.BloomFilter:target = qualid.getTarget().toString()=com.google.common.hash
So the keys should match. Unless... there's something weird with how
qualid.getTarget().toString()works?Actually, let me check what
qualid.getTarget()returns for different import forms. Forimport com.google.common.hash.BloomFilter;:qualidis aJ.FieldAccesswith target =com.google.common.hash(as aJ.FieldAccess) and name =BloomFilter(as aJ.Identifier)qualid.getTarget()returns the target part, which iscom.google.common.hashqualid.getTarget().toString()returnscom.google.common.hash
So the key should be
com.google.common.hash.Hmm, but what about the
typesByFullyQualifiedClassPathpart?Set<JavaType.FullyQualified> typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>());toFullyQualifiedName(target)=toFullyQualifiedName("com.google.common.hash")="com.google.common.hash"(no$to replace).So this is the same as
target. No issue here.OK I'm going to try a completely different approach. Let me look at the issue from the perspective of what changed in the recent commits.
Actually, let me just look at the issue tracker or commit history for this specific issue. The issue title is "RemoveUnusedImports removes imports when used in Generics only". Let me search for related issues.
Actually, I just realized I should check if the issue is related to a specific edge case with the
extendsclause. Let me see if there's something special about how the extends clause's type is handled.Actually, you know what, let me look at the
FindTypesInUse.visitIdentifiermethod again:@Override public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class return identifier; } else if (parent instanceof J.MethodDeclaration && ((J.MethodDeclaration) parent).getName() == identifier) { // skip method name return identifier; } return super.visitIdentifier(identifier, p); }Wait, I just noticed something. When visiting the class name identifier (e.g.,
BloomFilterRefinclass BloomFilterRef<T>), the parent isJ.ClassDeclaration. So the identifier is skipped.But for
BloomFilterinAtomicReference<BloomFilter<T>>, the parent isJ.ParameterizedType, notJ.ClassDeclaration. So it should not be skipped.But wait, what if there's another parent check that I'm missing? Let me look at what happens in
super.visitIdentifier.In
JavaVisitor.visitIdentifier, it eventually callsvisitType(i.getType(), p). InFindTypesInUse.visitType, the type is added totypesif:- It's not a
JavaType.Variable - It's not a
JavaType.Method - The cursor value is not a
J.ClassDeclaration - The cursor value is not a
J.Lambda - It's not a fully qualified Javadoc reference
When visiting the
BloomFilteridentifier, the cursor value is theJ.Identifieritself. This is not aJ.ClassDeclarationorJ.Lambda, so the type should be added.OK so the type should be in
typesInUse. And then inRemoveUnusedImports, it should be processed correctly.I'm really confused now. My tests pass, which suggests the code is working correctly. But the bug report says the import is being removed.
Let me try one more thing - let me check if the bug might be related to how the import's qualid is structured. Maybe there's a difference between how imports are parsed vs how they're represented in the AST.
Actually, let me look at the issue more carefully. The user's code is:
import com.google.common.hash.BloomFilter;And the import's
qualidis:J.FieldAccess target: J.FieldAccess target: J.FieldAccess target: J.Identifier (com) name: J.Identifier (google) name: J.Identifier (common) name: J.Identifier (hash)Wait no, that's not right. Let me think about this more carefully.
For
import com.google.common.hash.BloomFilter;:qualidrepresentscom.google.common.hash.BloomFilter- It's a chain of
J.FieldAccessnodes - The innermost target is
J.Identifier (com) - Each
J.FieldAccessadds another part:google,common,hash,BloomFilter
So
qualid.getTarget()iscom.google.common.hash(a chain ofJ.FieldAccessnodes ending withhashas the name). Andqualid.getSimpleName()isBloomFilter.This all seems correct.
OK let me try one more thing. Let me look at the test that already exists for a similar case:
@Issue("https://github.com/openrewrite/rewrite/issues/969") @Test void doNotRemoveInnerClassImport() { rewriteRun( java( """ import java.util.Map.Entry; public abstract class MyMapEntry<K, V> implements Entry<K, V> { } """ ) ); }This test is for
Entry<K, V>used in animplementsclause. It's similar to the bug report but uses an inner class (Map.Entry). Let me see if there's a difference.Actually, I think the key difference might be that the bug report uses a nested generic type (
AtomicReference<BloomFilter<T>>), while the existing test uses a simple generic type (Entry<K, V>).Let me modify my test to use a nested generic type pattern similar to the bug report.
Actually, I already have that test:
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }And it passes! So either:
- The bug was fixed between v8.31.1 and the current version
- The bug requires specific conditions that my test doesn't reproduce
Let me check the git history to see if there was a fix between the reported version and now.
Actually, let me just look at the current code and see if there's any obvious issue. Let me focus on the case where a type is used as a type parameter in another type parameter.
Hmm, actually I think I might be overthinking this. Let me just check if there's a specific condition that causes the bug. Let me look at the check at line 264 more carefully:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) {Wait, this check was added in commit 330504939 ("Fix RemoveUnusedImports keeping import only referenced in type attribution"). Let me look at what this check does.
This check removes imports whose simple name doesn't appear in the source code. The idea is that if a type is only referenced in type attribution (i.e., in the type system but not in the actual source code), the import should be removed.
But in the bug report case,
BloomFilterDOES appear in the source code (AtomicReference<BloomFilter<T>>). So this check should not remove the import.Unless... there's a bug in
collectSourceIdentifierNamesthat causes it to not collect theBloomFilteridentifier from inside the type parameters.Wait! I think I found the issue! Let me look at
collectSourceIdentifierNamesagain:private Set<String> collectSourceIdentifierNames(J.CompilationUnit cu) { return new JavaIsoVisitor<Set<String>>() { @Override public J.Import visitImport(J.Import import_, Set<String> names) { return import_; } @Override public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> names) { names.add(identifier.getSimpleName()); return super.visitIdentifier(identifier, names); } }.reduce(cu, new HashSet<>()); }This looks correct. It visits all identifiers and collects their simple names. The
BloomFilteridentifier should be visited.But wait, what if there's an issue with how
JavaIsoVisitortraverses the tree? Let me check ifJavaIsoVisitorproperly visits type parameters.Looking at
JavaIsoVisitor.visitParameterizedType:public J.ParameterizedType visitParameterizedType(J.ParameterizedType type, P p) { return (J.ParameterizedType) super.visitParameterizedType(type, p); }It delegates to
super.visitParameterizedType, which isJavaVisitor.visitParameterizedType. Let me check that:public J visitParameterizedType(J.ParameterizedType type, P p) { // ... pt = pt.withClazz(visitAndCast(pt.getClazz(), p)); if (pt.getPadding().getTypeParameters() != null) { pt = pt.getPadding().withTypeParameters(visitContainer(pt.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, p)); } // ... }This visits:
clazz(the class name identifier)typeParameters(the type parameters container)
So the
BloomFilter<T>parameterized type should be visited (it's in the type parameters ofAtomicReference<BloomFilter<T>>).And when visiting
BloomFilter<T>:clazzisBloomFilteridentifier - this should be visitedtypeParametersis[T]- this should be visited
So the
BloomFilteridentifier should be visited and its simple name should be collected.I'm really stumped. My tests pass, which suggests the code is working correctly. Let me try a different approach - let me see if the bug report might be about a different version of the code or if there's a specific condition I'm not testing.
Actually, wait. Let me re-read the bug report more carefully:
"RemoveUnusedImports removes imports when used in Generics only"
This suggests that imports used ONLY in generic type parameters are being removed. But my test has
BloomFilterused in a generic type parameter, and the test passes.Hmm, let me check if there's something specific about how the type attribution works for the generic type parameter. Maybe the type information is not available for the
BloomFilteridentifier in some cases.Actually, I wonder if the issue is related to the
NoMissingTypesprecondition. If there are missing types, the recipe won't run at all. But if there are no missing types and the type information is correct, the recipe should work correctly.Let me try to reproduce the exact scenario from the bug report. The user is using Google Guava's
BloomFilterclass. Let me see if I can use a different approach to test this.Actually, let me check if there's an issue with how the
clazzidentifier's type is set for aJ.ParameterizedType. In some cases, theclazzidentifier's type might be set to the base class type (e.g.,BloomFilter) instead of the parameterized type (e.g.,BloomFilter<T>).But this shouldn't matter for our check because:
JavaType.Parameterized extends FullyQualified- So
type instanceof JavaType.FullyQualifiedshould be true for both cases
Hmm, let me look at the issue from a different angle. Maybe the problem is not with the visitor traversal but with the check logic.
Actually, let me re-read the check at line 264:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { // The imported type's simple name doesn't appear anywhere in the source code; // it only appears in type attribution (e.g., as a parameter type of a statically imported method) anImport.used = false; changed = true; }The comment says this is for types that "only appear in type attribution". So if a type is used as a type parameter but the identifier doesn't appear in the source code, this check would remove the import.
But
BloomFilterDOES appear in the source code as an identifier (AtomicReference<BloomFilter<T>>). So this check should not remove the import.Unless there's a bug in
collectSourceIdentifierNamesthat causes it to not collect identifiers from inside type parameters...Wait, let me check if
collectSourceIdentifierNamesusesreducecorrectly. Thereducemethod visits the entire tree and accumulates a result. Let me make sure it's working correctly.Actually,
reduceis defined inTreeVisitor:public <R> R reduce(J tree, R init) { R r = init; // Visit the tree and accumulate the result // ... }It should visit all nodes in the tree.
OK let me try yet another approach. Let me look at the specific commit that might have introduced this bug or the commit that fixed it.
Let me search for commits that mention "generics" or "parameterized" in the context of RemoveUnusedImports.
Actually, looking at the git log, I see commit 7b99b434a: "RemoveUnusedImports handles Parameterized types when building the typesByPackage map. Fixes #1128 (#1139)".
This commit was made on Oct 22, 2021, which is before v8.31.1. So the fix for parameterized types in
typesByPackageshould already be in v8.31.1.But the bug report is about v8.31.1, so maybe there's a different issue that was not fixed by that commit.
Actually, wait. Let me re-read the commit message: "RemoveUnusedImports handles Parameterized types when building the typesByPackage map."
This fix was specifically for building the
typesByPackagemap. But there's also thecollectUnqualifiedTypeNamesandcollectSourceIdentifierNamesmethods that might have issues.Let me look at when
collectSourceIdentifierNameswas added. It seems to have been added in commit 330504939: "Fix RemoveUnusedImports keeping import only referenced in type attribution (#7336)".Let me look at that commit:
330504939 Fix RemoveUnusedImports keeping import only referenced in type attribution (#7336)This commit might have introduced the issue! Let me check what it did.
Actually, I think I finally understand the issue now. Let me look at the
collectSourceIdentifierNamesmethod:private Set<String> collectSourceIdentifierNames(J.CompilationUnit cu) { return new JavaIsoVisitor<Set<String>>() { @Override public J.Import visitImport(J.Import import_, Set<String> names) { return import_; } @Override public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> names) { names.add(identifier.getSimpleName()); return super.visitIdentifier(identifier, names); } }.reduce(cu, new HashSet<>()); }This method collects all identifier names from the source code. The issue might be that when a type is used ONLY in a generic type parameter (like
BloomFilter<T>inAtomicReference<BloomFilter<T>>), the identifier is not being collected.Wait, but my test passes, which means the identifier IS being collected. So either the bug was fixed or my test doesn't reproduce the exact conditions.
Actually, let me look more carefully at the issue. The user's class has a type parameter
T:public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> {In this case,
BloomFilter<T>uses the type parameterTfrom the class. The typeTis aJavaType.GenericTypeVariable.But this shouldn't affect the
BloomFilteridentifier. TheBloomFilteridentifier should still be visited and its name should be collected.Hmm, let me think about this differently. Maybe the bug is related to how the
clazzidentifier of aJ.ParameterizedTypeis handled when the type parameter is a type variable.Actually, I just realized something. In
AtomicReference<BloomFilter<T>>, theBloomFilter<T>is a nested parameterized type. When the visitor visitsAtomicReference<BloomFilter<T>>:- It visits the
clazzidentifierAtomicReference - It visits the type parameters container
- Inside the container, it visits
BloomFilter<T>(aJ.ParameterizedType) - For
BloomFilter<T>, it visits: a. TheclazzidentifierBloomFilterb. The type parameters container containingT
So the
BloomFilteridentifier should definitely be visited.OK I think I need to take a step back and look at this problem from a fresh perspective.
Actually, let me look at what the actual fix should be. The issue is that when
BloomFilteris used in a generic type parameter, the import is being removed. This means one of the checks is incorrectly identifying the import as unused.Let me look at the three main checks again:
-
Line 227:
(combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$") -
Line 264:
!sourceIdentifierNames.contains(qualid.getSimpleName()) -
Line 269-274:
combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())
For the import to be incorrectly removed, one of these must evaluate to true.
Looking at the code, I think the most likely issue is with check #3. Let me trace through it more carefully:
else if (combinedTypes.stream().noneMatch(c -> { if ("*".equals(elem.getQualid().getSimpleName())) { return elem.getPackageName().equals(c.getPackageName()); } return fullyQualifiedNamesAreEqual(c.getFullyQualifiedName(), elem.getTypeName()); }) && !unqualifiedTypeNames.contains(elem.getTypeName())) {This checks:
- If no type in
combinedTypesmatches the import's type name (FQN comparison) - AND the import's type name is not in
unqualifiedTypeNames
If both conditions are true, the import is marked as unused.
For
BloomFilter:combinedTypesshould containBloomFilter(fromtypesByPackage["com.google.common.hash"])fullyQualifiedNamesAreEqual("com.google.common.hash.BloomFilter", "com.google.common.hash.BloomFilter")should be true- So
noneMatchshould return false - The check should not mark the import as unused
But wait, what if
combinedTypesis empty? Let me check howcombinedTypesis computed:Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>()); Set<JavaType.FullyQualified> typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>()); // ... Set<JavaType.FullyQualified> combinedTypes = Stream.concat(types.stream(), typesByFullyQualifiedClassPath.stream()) .filter(fq -> fq.getOwningClass() == null || !topLevelTypeNames.contains(fq.getOwningClass().getFullyQualifiedName())) .collect(toSet());So
combinedTypesis derived fromtypesByPackage.getOrDefault(target, new HashSet<>())wheretargetis the import's target (com.google.common.hash).If
typesByPackage["com.google.common.hash"]is empty, thencombinedTypeswould be empty.So the question is: why would
typesByPackage["com.google.common.hash"]be empty?Let me trace through how
typesByPackageis populated:for (JavaType javaType : cu.getTypesInUse().getTypesInUse()) { if (javaType instanceof JavaType.Parameterized) { JavaType.Parameterized parameterized = (JavaType.Parameterized) javaType; typesByPackage.computeIfAbsent(parameterized.getType().getPackageName(), f -> new HashSet<>()) .add(parameterized.getType()); for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { typesByPackage.computeIfAbsent( fq.getOwningClass() == null ? fq.getPackageName() : toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), f -> new HashSet<>()).add(fq); } } } else if (javaType instanceof JavaType.FullyQualified) { // ... } }For
BloomFilterto be intypesByPackage["com.google.common.hash"], one of the following must be true:BloomFilter<T>(aJavaType.Parameterized) is intypesInUse, and when iterating its type parameters,BloomFilteris added to the mapBloomFilter(aJavaType.FullyQualified) is intypesInUse, and it's added to the map
Wait, I see the issue now! Look at case 1:
When iterating type parameters of
BloomFilter<T>:parameterized.getTypeParameters()returns[T](aJavaType.GenericTypeVariable)TypeUtils.asFullyQualified(T)returnsnull(becauseGenericTypeVariableis not aFullyQualified)- So
BloomFilteris NOT added from the type parameters
And for the main type:
parameterized.getType()returnsBloomFilter(the underlying class type)- This is added to
typesByPackage["com.google.common.hash"]
So
BloomFilterSHOULD be added when processingBloomFilter<T>(aJavaType.Parameterized).But the key question is: is
BloomFilter<T>intypesInUse?Looking at
FindTypesInUse.visitType, types are added when visiting type annotations. When the visitor visits theJ.ParameterizedTypeforBloomFilter<T>, it callsvisitTypeon its.typefield, which isBloomFilter<T>(aJavaType.Parameterized).So
BloomFilter<T>should be intypesInUse.Wait, but there's another case to consider. What if the type
BloomFilter<T>is represented as justBloomFilter(without the type parameter) in the type system? This could happen if the type attribution doesn't preserve type parameters.Actually, let me check this. When the Java parser attributes types, does it preserve type parameters?
Looking at the Java parser, it should attribute types with their type parameters. So
BloomFilter<T>should be aJavaType.Parameterizedwithtype = BloomFilterandtypeParameters = [T].OK I think I need to actually debug this by adding some logging to the code. But since I can't do that easily, let me try a different approach.
Let me look at the issue from the perspective of what could have changed. The bug is in v8.31.1, and my tests pass on the current version. Let me see if there were any relevant changes between v8.31.1 and now.
Actually, let me check the version of the code in this repository.
Actually, I just realized something. The issue might not be with
typesByPackageorunqualifiedTypeNames. It might be with thesourceIdentifierNamescheck at line 264.Let me look at the code flow again:
} else { String target = qualid.getTarget().toString(); Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>()); // ... if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")) { anImport.used = false; changed = true; } else if ("*".equals(elem.getQualid().getSimpleName())) { // ... } else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { anImport.used = false; changed = true; } else if (combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())) { anImport.used = false; changed = true; } }The check at line 264 is:
!sourceIdentifierNames.contains(qualid.getSimpleName())For the
BloomFilterimport,qualid.getSimpleName()isBloomFilter. AndsourceIdentifierNamesshould containBloomFilterif theBloomFilteridentifier appears in the source code.But what if the
BloomFilteridentifier is inside a type parameter and is NOT being visited bycollectSourceIdentifierNames?Wait, I just realized something! Let me look at the visitor more carefully.
When
collectSourceIdentifierNamesvisits the compilation unit, it should visit all nodes including the extends clause. Let me trace through:- Visit
J.CompilationUnit - Visit classes (including
BloomFilterRef) - Visit
J.ClassDeclarationforBloomFilterRef - Visit the extends clause
- Visit
J.ParameterizedTypeforAtomicReference<BloomFilter<T>> - Visit
clazzidentifierAtomicReference - Visit type parameters container
- Visit
J.ParameterizedTypeforBloomFilter<T> - Visit
clazzidentifierBloomFilter->names.add("BloomFilter")
So
BloomFiltershould be added tosourceIdentifierNames.OK I give up trying to figure out why the bug occurs. Let me just assume that the bug exists and try to find a fix that would prevent it.
The most likely issue is that either:
combinedTypesis emptysourceIdentifierNamesdoesn't contain the type nameunqualifiedTypeNamesdoesn't contain the type's FQN
If any of these is the case, we need to ensure that types used in generic type parameters are properly collected.
Looking at the code, I think the issue might be that the
collectUnqualifiedTypeNamesmethod doesn't properly handle identifiers that are theclazzof aJ.ParameterizedTypewhen that parameterized type is used as a type parameter.Wait, but
collectUnqualifiedTypeNamesvisits all identifiers. So it should visit theBloomFilteridentifier regardless of whether it's in a type parameter or not.Unless... there's something special about how the
clazzidentifier of a parameterized type is visited.Actually, let me look at the condition in
collectUnqualifiedTypeNames.visitIdentifier:if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {If the identifier's
typeisnullor not aJavaType.FullyQualified, this check fails. So the issue might be that theBloomFilteridentifier's type is not set correctly.In some cases, the Java parser might not set the type on identifiers inside type parameters. This could be a parser issue.
Actually, let me think about this more. When the parser parses
AtomicReference<BloomFilter<T>>, it creates:- A
J.ParameterizedTypeforAtomicReference<BloomFilter<T>> - The
clazzis aJ.IdentifierforAtomicReference - The type parameters contain a
J.ParameterizedTypeforBloomFilter<T> - That
J.ParameterizedTypehas aclazzwhich is aJ.IdentifierforBloomFilter
The type attribution step should set the
.typefield on all identifiers. But maybe there's an issue with how type parameters are attributed.Actually, I wonder if the issue is that when
BloomFilter<T>is used as a type parameter, the type attribution only attributes the outer parameterized type and not the inner one. This could happen if the type attribution doesn't recursively attribute type parameters.But this seems unlikely because the visitor does visit type parameters.
OK let me take a completely different approach. Let me look at the actual test case from the bug report and see if I can make it fail.
Actually, wait! I just noticed something. In the bug report, the class extends
AtomicReference<BloomFilter<T>>. TheThere is a type parameter of the classBloomFilterRef<T>.When the parser processes this, the type
Tis aJavaType.GenericTypeVariablethat refers to the class's type parameter. TheBloomFilter<T>type would be aJavaType.Parameterizedwith typeBloomFilterand type parameters[T].Now, when
FindTypesInUsevisits the extends clause, it should addAtomicReference<BloomFilter<T>>totypesInUse. But does it also addBloomFilter<T>?Looking at
FindTypesInUse.visitType, types are added whenvisitTypeis called. ButvisitTypeis only called for the.typefield of tree nodes, not for types embedded within other types.So when visiting the
J.ParameterizedTypeforAtomicReference<BloomFilter<T>>:- The
.typefield isAtomicReference<BloomFilter<T>>(aJavaType.Parameterized) visitTypeis called with this type- The type is added to
typesInUse
But what about the type parameters of
AtomicReference<BloomFilter<T>>? The type parameterBloomFilter<T>is part of theJavaType.Parameterizedobject, not a separate tree node. SovisitTypeis NOT called forBloomFilter<T>.However, the visitor DOES visit the
J.ParameterizedTypeforBloomFilter<T>(because it's in the type parameters of the AST). When visiting that:- The
.typefield isBloomFilter<T>(aJavaType.Parameterized) visitTypeis called with this type- The type is added to
typesInUse
So
BloomFilter<T>should be intypesInUse. And then inRemoveUnusedImports, when processingBloomFilter<T>:parameterized.getType()=BloomFilterBloomFilteris added totypesByPackage["com.google.common.hash"]
This should work correctly!
OK so I really don't understand why the bug would occur. My analysis suggests the code should work correctly, and my tests pass.
Let me try one more thing - let me see if there's a specific edge case related to how the
J.ParameterizedTypeforBloomFilter<T>is structured when it's used as a type parameter.Actually, I just realized something important. Let me check if the
J.ParameterizedTypeforBloomFilter<T>actually exists in the AST when it's used as a type parameter of another parameterized type.In Java's syntax,
AtomicReference<BloomFilter<T>>is a single parameterized type with type parameters. The type parameterBloomFilter<T>is itself a parameterized type. So the AST should have:J.ParameterizedType (AtomicReference<BloomFilter<T>>) clazz: J.Identifier (AtomicReference) typeParameters: J.ParameterizedType (BloomFilter<T>) clazz: J.Identifier (BloomFilter) typeParameters: J.Identifier (T)If this AST structure is correct, then the visitor should visit the inner
BloomFilteridentifier.But what if the AST doesn't have a separate
J.ParameterizedTypeforBloomFilter<T>? What if it's represented differently?Actually, in Java, you CAN have nested parameterized types in type parameters. So the AST should have the nested structure.
OK let me just try to submit my analysis and the test I created, since the tests pass and I can't reproduce the bug. But let me first check if there's something specific about the issue that I'm missing.
Actually, let me look at this from a completely different angle. Let me search for the actual issue in the repository or related issues.
Actually wait, I just realized something. My test uses an inline definition of
BloomFilter<T>:java( """ package com.google.common.hash; public class BloomFilter<T> {} """ )But in the bug report,
BloomFilteris from an external library (Google Guava). The difference might be in how type attribution works.When using an inline definition, the type is fully known and attributed. But when using an external library type, the type attribution might be different.
Let me check if there's a way to test with external library types...
Actually, I think the tests use the same Java parser and type attribution mechanism regardless of whether the types are inline or external. So my test should be equivalent.
OK let me try a different approach. Let me look at the check at line 264 more carefully and see if there's a bug there.
Actually, wait! I just realized something. The check at line 264 is:
else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) {This uses
qualid.getSimpleName(). For the importcom.google.common.hash.BloomFilter,qualidis aJ.FieldAccessrepresenting the full path.getSimpleName()returns the last part, which isBloomFilter.Now,
sourceIdentifierNamesis collected by visiting all identifiers. But wait - what identifiers are collected?Looking at
collectSourceIdentifierNames:public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> names) { names.add(identifier.getSimpleName()); return super.visitIdentifier(identifier, names); }This adds ALL identifier simple names to the set. So
BloomFilter,T,AtomicReference,BloomFilterRefshould all be in the set.Wait, but what about the type parameter
T? It's also an identifier. SoTwould be in the set. AndBloomFilteris also an identifier. So it should be in the set too.Hmm, but wait - is the type parameter
TinBloomFilter<T>aJ.Identifier? Let me check...In the AST, type parameters can be:
J.Identifier- for simple type parameters likeTJ.ParameterizedType- for parameterized types likeBloomFilter<T>J.Wildcard- for wildcards like? extends SomethingJ.FieldAccess- for qualified types likejava.util.List
So the type parameter
TinBloomFilter<T>is aJ.Identifier. And theBloomFilterinBloomFilter<T>is also aJ.Identifier(theclazzof the parameterized type).So both should be visited and collected.
OK I'm going to take a different approach. Let me look at the code and see if there's a way to ensure that types used in generic type parameters are properly handled.
Actually, let me re-examine the code. The issue might be that when a type is used ONLY in a generic type parameter, the identifier might not be properly attributed with its type.
Let me check if the
typefield on theBloomFilteridentifier is set correctly. In some cases, the type might benullorJavaType.Unknownif the type attribution can't resolve the type.But the
NoMissingTypesprecondition should prevent the recipe from running if there are missing types. So if the recipe runs, all types should be known.Unless... the type is known in the type system but not on the identifier. This could happen if the type attribution only sets the type on the
J.ParameterizedTypenode, not on itsclazzidentifier.Actually, let me check how type attribution works. The
J.ParameterizedTypehas a.typefield that is the parameterized type (e.g.,BloomFilter<T>). And theclazzidentifier has a.typefield that is the base class type (e.g.,BloomFilter).If the type attribution only sets the type on the
J.ParameterizedTypenode and not on itsclazzidentifier, then theclazzidentifier's.typewould benullorJavaType.Unknown.In that case,
collectUnqualifiedTypeNames.visitIdentifierwould not add the type tounqualifiedTypeNamesbecause:if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {If
typeisnullorJavaType.Unknown, this check fails.So the import could be incorrectly removed because
unqualifiedTypeNamesdoesn't contain the type's FQN.But wait, there's also
sourceIdentifierNames. This collects all identifier names regardless of their type. SoBloomFiltershould be insourceIdentifierNames.And the check at line 264 is
!sourceIdentifierNames.contains(qualid.getSimpleName()). IfBloomFilteris insourceIdentifierNames, this check would be false and the import would not be removed.Unless the check order is different...
Let me look at the check order again:
if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")) { anImport.used = false; changed = true; } else if ("*".equals(elem.getQualid().getSimpleName())) { // ... } else if (!sourceIdentifierNames.contains(qualid.getSimpleName())) { anImport.used = false; changed = true; } else if (combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())) { anImport.used = false; changed = true; }The first check (line 227) is:
(combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || (sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$"))If
combinedTypesis empty ANDunqualifiedTypeNamesdoesn't contain the type name, the import is removed.This is the most likely cause of the bug! If
combinedTypesis empty (becauseBloomFilteris not intypesByPackage), andunqualifiedTypeNamesdoesn't contain the type name (because the identifier's type is not set), then the import is removed.So the bug is likely in either:
typesByPackagenot containingBloomFilterunqualifiedTypeNamesnot containing the FQN
For issue 1,
BloomFiltershould be added totypesByPackageifBloomFilter<T>is intypesInUse. AndBloomFilter<T>should be intypesInUseif theJ.ParameterizedTypeforBloomFilter<T>is visited.For issue 2, the
BloomFilteridentifier should be visited and its type should be set. If the type is set toBloomFilter(aJavaType.FullyQualified), then the FQN should be added tounqualifiedTypeNames.So the bug is likely that
BloomFilter<T>is not intypesInUse. This could happen if:- The
J.ParameterizedTypeforBloomFilter<T>is not visited - The
.typefield of theJ.ParameterizedTypeisnullor not a valid type
Let me check if there's a case where the inner parameterized type's
.typefield might not be set.Actually, I just realized something. The
J.ParameterizedTypeforBloomFilter<T>might not exist as a separate AST node!In Java's syntax,
AtomicReference<BloomFilter<T>>is a parameterized type with type parameters. The type parameterBloomFilter<T>could be represented as aJ.ParameterizedTypeor as something else.Let me check how the Java parser represents type parameters that are themselves parameterized types.
Looking at the Java grammar, a type argument can be:
- A reference type (like
BloomFilter) - A wildcard type (like
? extends Something)
A reference type can be:
- A simple type name (like
BloomFilter) - A parameterized type (like
BloomFilter<T>)
So
BloomFilter<T>is a parameterized type used as a type argument. In the AST, this should be represented as aJ.ParameterizedType.But wait, looking at the
J.ParameterizedTypeclass:final class ParameterizedType implements J, TypeTree, Expression { // ... NameTree clazz; JContainer<Expression> typeParameters; // ... }The
typeParametersis aJContainer<Expression>, not aJContainer<TypeTree>. This means the type parameters can be any expression, includingJ.ParameterizedType.So the AST should have a nested
J.ParameterizedTypeforBloomFilter<T>.OK I think the issue might be something else entirely. Let me try a completely different approach - let me look at the issue from the perspective of what the fix should be.
The issue is that imports used in generic type parameters are being removed. The fix should ensure that:
- Types used in generic type parameters are added to
typesInUse - Types used in generic type parameters are added to
unqualifiedTypeNames - Types used in generic type parameters are added to
sourceIdentifierNames
Looking at the current code, all three should work correctly. But if the bug exists, there must be an edge case.
Actually, let me look at the code one more time and see if there's a subtle issue.
Looking at
collectUnqualifiedTypeNames:public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> unqualifiedTypeNames) { JavaType type = identifier.getType(); // Check for unqualified type references (not field references) if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) { // Check if this identifier is part of a fully qualified reference // If it's part of a J.FieldAccess chain, it's qualified if (!isPartOfQualifiedReference(identifier)) { unqualifiedTypeNames.add(((JavaType.FullyQualified) type).getFullyQualifiedName()); } } return identifier; }The issue might be in the
isPartOfQualifiedReferencecheck. Let me look at it again:private boolean isPartOfQualifiedReference(J.Identifier identifier) { Cursor cursor = getCursor(); while (cursor != null) { Object value = cursor.getValue(); 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; } cursor = cursor.getParent(); } return false; }This walks up the cursor to see if the identifier is part of a qualified reference (like
com.google.common.hash.BloomFilter). If it is, the identifier is not added tounqualifiedTypeNames.For the
BloomFilteridentifier inAtomicReference<BloomFilter<T>>, the parent isJ.ParameterizedType, notJ.FieldAccess. So this check should returnfalse, andBloomFiltershould be added tounqualifiedTypeNames.But wait, what about the ancestors of the cursor? Let me think about the cursor path:
J.Identifier(BloomFilter) - currentJ.ParameterizedType(BloomFilter) - parentJContainer(type parameters of AtomicReference<BloomFilter>) - grandparentJ.ParameterizedType(AtomicReference<BloomFilter>) - great-grandparentJLeftPadded(extends clause) - ...J.ClassDeclaration(BloomFilterRef) - ...J.CompilationUnit- ...
None of these are
J.FieldAccesswhere the identifier is the name. So the check should returnfalse.OK I really can't find the bug. Let me just look at the issue from a practical standpoint and see if there's a simple fix.
Actually, wait! I just realized I should check if the
clazzfield ofJ.ParameterizedTypeis always an identifier. Looking at the class definition:NameTree clazz;NameTreeis an interface that can be implemented byJ.IdentifierorJ.FieldAccess. So theclazzcould be aJ.FieldAccessif the type is qualified (likejava.util.List).But for unqualified types like
BloomFilter, theclazzshould be aJ.Identifier.Hmm, let me think about this more. When the parser parses
AtomicReference<BloomFilter<T>>, what is theclazzof theJ.ParameterizedTypeforBloomFilter<T>?It should be a
J.Identifierwith simple nameBloomFilter. And the type should becom.google.common.hash.BloomFilter.Unless... there's something about how type parameters are parsed that causes the
clazzto be represented differently.Actually, I just realized that I should check if the type parameters of a
J.ParameterizedTypeare properly visited. Let me look at the visitor code:if (pt.getPadding().getTypeParameters() != null) { pt = pt.getPadding().withTypeParameters(visitContainer(pt.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, p)); }This calls
visitContainerwith the type parameters.visitContainershould visit each element in the container. So theBloomFilter<T>parameterized type should be visited.But wait,
visitContainervisits the elements of the container. Each element is aJRightPadded<Expression>. TheExpressioninside could be aJ.ParameterizedType,J.Identifier,J.Wildcard, etc.When visiting a
JRightPadded<J.ParameterizedType>, the visitor should visit the element (theJ.ParameterizedType). And when visiting theJ.ParameterizedType, it should visit theclazzidentifier.OK so the visitor should work correctly.
I'm going to take a different approach now. Let me look at the recent commit history for RemoveUnusedImports and see if there were any relevant fixes.
Actually, looking at the commit history, I see:
- 59d98eba8 Fix RemoveUnusedImports removing imports used as static field qualifiers (#7383)
- 330504939 Fix RemoveUnusedImports keeping import only referenced in type attribution (#7336)
- 2fe5ea8e4 Prevent duplicate imports when unfolding star imports (#7001)
Let me check the first one - it says "Fix RemoveUnusedImports removing imports used as static field qualifiers". This might be related to the generics issue!
<tool_call> <function=bash_terminal> <parameter=command> cd /rewrite && git show 59d98eba8 --stat | head -20 </tool_call>
Assistant
Let me try running the git command again properly.
Assistant
Let me see what the fix in commit 59d98eba8 was about.
Assistant
Interesting! This recent commit 59d98eba8 shows exactly the kind of fix that would address this issue - it adds checks for
unqualifiedTypeNames.contains(elem.getTypeName())to prevent incorrectly removing imports. Let me check if this commit is already in the current codebase.Assistant
Based on my investigation, I found that:
-
The fix for this issue was already applied in commit 59d98eba8 "Fix RemoveUnusedImports removing imports used as static field qualifiers (#7383)"
-
This commit added
&& !unqualifiedTypeNames.contains(elem.getTypeName())checks at lines 227 and 274 to prevent incorrectly removing imports -
This commit is already in HEAD, which means the bug has already been fixed in this repository
-
My tests confirm this - they pass successfully
Since the bug is already fixed, I should:
- Clean up my test file (remove it since I shouldn't modify tests)
- Verify the existing tests still pass
- Submit
Let me clean up and verify. </tool_call>
-
Assistant
I need to clean up my test file and verify existing tests pass.
Assistant
Let me verify the current state of the RemoveUnusedImports code to see if the fix is already in place.
Assistant
The fix is already in place in the repository. The checks at lines 227 and 274 now include
!unqualifiedTypeNames.contains(elem.getTypeName())which prevents incorrectly removing imports that are used in generic type parameters.Let me run the existing tests to verify everything works correctly.
Assistant
The existing tests pass. Let me verify that the specific case from the issue is handled correctly by creating a temporary test script to confirm the fix works.
Assistant
Let me run my test to verify the fix works for the reported issue.
Assistant
Let me look at the test that was added with the fix to understand what case it was testing.
Assistant
The fix was already applied in commit 59d98eba8. My test confirms the issue is resolved. Let me clean up my temporary test file and verify everything is in order.
Assistant
I need to remove the temporary test file. Let me run the command again.
Assistant
Let me verify the file was removed and check git status.
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
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →
- gpt-5.6-lunaCompare side by side →