Let me understand the issue better:
- The user has a class that extends
AtomicReference<BloomFilter<T>>
- The
BloomFilter import is being removed because it's not being recognized as "used"
- The
AtomicReference import is kept because it IS recognized as used
Let me trace through the code:
RemoveUnusedImportsVisitor.visitCompilationUnit collects all types in use
- It iterates through
cu.getTypesInUse().getTypesInUse() to collect JavaType.FullyQualified types
- For each import, it checks if the import's type name is in the collected types
The issue is likely that when a type is used in a generic parameter in the type hierarchy (extends/implements), it might not be properly recognized as a type in use.
Let me look at how types are collected. In the visitor:
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) {
JavaType.FullyQualified fq = (JavaType.FullyQualified) javaType;
typesByPackage.computeIfAbsent(
fq.getOwningClass() == null ?
fq.getPackageName() :
toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()),
f -> new HashSet<>()).add(fq);
}
}
This collects types from typesInUse. But it seems like it might only be looking at the package name, not the fully qualified name. Let me trace through:
When the class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>>:
- The type hierarchy would have
AtomicReference<BloomFilter<T>>
- This is a Parameterized type with:
parameterized.getType() = AtomicReference (FullyQualified)
parameterized.getTypeParameters() = [T, BloomFilter]
For BloomFilter:
- It's a type parameter of the parameterized type
fq.getOwningClass() would be null (it's not an inner class)
- So it uses
fq.getPackageName() which would be something like com.google.common.hash
But then when checking the import:
String target = qualid.getTarget().toString(); // "com.google.common.hash.BloomFilter"
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
The target would be the FULLY QUALIFIED name "com.google.common.hash.BloomFilter", but the key in typesByPackage might just be "com.google.common.hash" (the package name).
Actually, looking more carefully:
For BloomFilter:
parameterized.getType() = com.google.common.hash.BloomFilter (FullyQualified)
parameterized.getType() has package name "com.google.common.hash" and class name "BloomFilter"
- When added to
typesByPackage, it uses the package name as the key
- The value added would be
parameterized.getType() which is the FullyQualified type
Wait, let me re-read. For the Parameterized type case:
typesByPackage.computeIfAbsent(parameterized.getType().getPackageName(), f -> new HashSet<>())
.add(parameterized.getType());
So for AtomicReference<BloomFilter<T>>:
parameterized.getType() = AtomicReference
- It adds
AtomicReference to the map with key "java.util.concurrent"
For type parameters:
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>:
- The type parameter
BloomFilter is a type parameter of AtomicReference<BloomFilter<T>>
fq = BloomFilter (FullyQualified)
fq.getOwningClass() = null
- So it uses
fq.getPackageName() = "com.google.common.hash"
- It adds
BloomFilter to the map with key "com.google.common.hash"
Now for the import check:
String target = qualid.getTarget().toString(); // "com.google.common.hash.BloomFilter"
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
The target is "com.google.common.hash.BloomFilter" (the full import statement's target), but we looked up using fq.getPackageName() = "com.google.common.hash".
AH! There's the bug! The typesByPackage map is keyed by the package name (e.g., "com.google.common.hash"), but when looking up, we're using qualid.getTarget().toString() which returns the fully qualified name (e.g., "com.google.common.hash.BloomFilter").
Let me verify by looking at the lookup code again:
String target = qualid.getTarget().toString();
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
Set<JavaType.FullyQualified> typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>());
So target = "com.google.common.hash.BloomFilter", and it's looking up in typesByPackage with key "com.google.common.hash.BloomFilter", but the keys stored are package names like "com.google.common.hash".
And toFullyQualifiedName("com.google.common.hash.BloomFilter") would return "com.google.common.hash.BloomFilter" (same thing), so that lookup would also fail.
Wait, but typesByPackage stores the types as VALUES, not as keys. The KEYS are package names. So the lookup by target would be looking for a key "com.google.common.hash.BloomFilter" in the map, but the key is "com.google.common.hash".
Let me look at toFullyQualifiedName:
import static org.openrewrite.java.tree.TypeUtils.toFullyQualifiedName;
Let me check what toFullyQualifiedName does.
Actually wait, I need to re-read this more carefully. Looking at the import handling:
String target = qualid.getTarget().toString(); // This is "com.google.common.hash.BloomFilter"
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
Set<JavaType.FullyQualified> typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>());
Hmm, toFullyQualifiedName would just return the same string. So both lookups would be for "com.google.common.hash.BloomFilter".
But the keys stored in typesByPackage are package names like "java.util" or "com.google.common.hash". So neither lookup would find anything!
Unless... let me check toFullyQualifiedName:
import static org.openrewrite.java.tree.TypeUtils.toFullyQualifiedName;
Let me look at the TypeUtils class to understand what it does.
Wait, let me re-examine the code. When adding types:
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());
So we're storing parameterized.getType() which is a JavaType.FullyQualified. And the key is the package name.
Then when we check the import:
String target = qualid.getTarget().toString(); // "com.google.common.hash.BloomFilter"
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
We're looking up by target which is the full FQN. But the keys are package names...
So for the BloomFilter case, target = "com.google.common.hash.BloomFilter", but the key in typesByPackage is "com.google.common.hash".
That means the lookup would return an empty set, and then the import would be marked as unused.
But wait, that would also affect other imports like AtomicReference. Let me think...
For AtomicReference:
- The FQN is "java.util.concurrent.AtomicReference"
- When added: key = "java.util.concurrent", value includes AtomicReference
- When looked up: key = "java.util.concurrent.AtomicReference"
- This would also not be found!
So why is AtomicReference kept?
Oh, I see - there might be another code path that handles non-generic type references differently. Or the AtomicReference might be stored as a top-level entry, not just as a parameterized type.
Let me think about this differently. AtomicReference<BloomFilter<T>> is a Parameterized type where:
- The base type is
AtomicReference
- The type parameters are
T and BloomFilter
When stored:
parameterized.getType() = AtomicReference (FullyQualified), stored under key "java.util.concurrent"
parameterized.getTypeParameters() = [T, BloomFilter]
- For T: it's a type variable, not FullyQualified
- For BloomFilter: it's FullyQualified, stored under key "com.google.common.hash"
So BloomFilter should be in the map with key "com.google.common.hash"...
Let me look at the non-static import handling more carefully:
} else {
String target = qualid.getTarget().toString();
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
Set<JavaType.FullyQualified> typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>());
Set<String> topLevelTypeNames = Stream.concat(types.stream(), typesByFullyQualifiedClassPath.stream())
.filter(fq -> fq.getOwningClass() == null)
.map(JavaType.FullyQualified::getFullyQualifiedName)
.collect(toSet());
Set<JavaType.FullyQualified> combinedTypes = Stream.concat(types.stream(), typesByFullyQualifiedClassPath.stream())
.filter(fq -> fq.getOwningClass() == null || !topLevelTypeNames.contains(fq.getOwningClass().getFullyQualifiedName()))
.collect(toSet());
JavaType.FullyQualified qualidType = TypeUtils.asFullyQualified(elem.getQualid().getType());
if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || sourcePackage.equals(elem.getPackageName()) && qualidType != null && !qualidType.getFullyQualifiedName().contains("$")) {
anImport.used = false;
changed = true;
So for BloomFilter:
target = "com.google.common.hash.BloomFilter"
types = typesByPackage.getOrDefault("com.google.common.hash.BloomFilter", new HashSet<>()) → empty set
typesByFullyQualifiedClassPath → also empty
combinedTypes → empty
unqualifiedTypeNames.contains("BloomFilter") → depends on whether BloomFilter appears unqualified in source
If BloomFilter doesn't appear unqualified in source (it's only used in AtomicReference<BloomFilter<T>>), then unqualifiedTypeNames wouldn't contain "BloomFilter".
But wait, in AtomicReference<BloomFilter<T>>, the BloomFilter does appear unqualified! The type parameter is just BloomFilter<T>, not com.google.common.hash.BloomFilter<T>.
So the issue might be in collectUnqualifiedTypeNames. Let me check:
private Set<String> collectUnqualifiedTypeNames(J.CompilationUnit cu) {
return new JavaIsoVisitor<Set<String>>() {
@Override
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;
}
...
}.reduce(cu, new HashSet<>());
}
So it's visiting all identifiers and checking if their type is FullyQualified. For BloomFilter in BloomFilter<T>, the identifier would be BloomFilter with type BloomFilter.
But here's the thing - this is running on the AST of the source file, not on the fully attributed types. The identifiers in the source are parsed, but the TYPE attribution happens during type resolution. So whether identifier.getType() returns the proper type depends on whether the type has been attributed.
Hmm, but the visitor uses TypesInUse which should already have done type attribution. Let me look at what cu.getTypesInUse().getTypesInUse() returns.
Actually, I think I need to look at how TypesInUse collects types. Let me check that file.
Let me also think about this more carefully. The issue is:
- BloomFilter is used in
extends AtomicReference<BloomFilter<T>>
- The BloomFilter import is being removed
So there are two possible issues:
- BloomFilter isn't being collected as a "type in use"
- BloomFilter IS being collected but the import matching logic fails
Let me trace through the code path:
For the import com.google.common.hash.BloomFilter:
elem.isStatic() is false (it's not a static import)
- We enter the
else block (non-static import handling)
target = qualid.getTarget().toString() = "com.google.common.hash.BloomFilter"
types = typesByPackage.getOrDefault("com.google.common.hash.BloomFilter", ...)
Now, in typesByPackage, the key is "com.google.common.hash" (from fq.getPackageName()), and the value includes BloomFilter. So looking up "com.google.common.hash.BloomFilter" would return empty!
- Same for
typesByFullyQualifiedClassPath
combinedTypes is empty
unqualifiedTypeNames - this is collected from the AST
If collectUnqualifiedTypeNames finds BloomFilter (because it appears as BloomFilter<T> unqualified in the source), then !unqualifiedTypeNames.contains(elem.getTypeName()) would be false, and the condition combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName()) would be false, so the import wouldn't be removed.
But if collectUnqualifiedTypeNames doesn't find BloomFilter... then unqualifiedTypeNames.contains(elem.getTypeName()) would be false, and the condition would be true, so the import WOULD be removed.
So the question is: does collectUnqualifiedTypeNames find BloomFilter?
Looking at the method again:
private Set<String> collectUnqualifiedTypeNames(J.CompilationUnit cu) {
return new JavaIsoVisitor<Set<String>>() {
@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> unqualifiedTypeNames) {
JavaType type = identifier.getType();
if (type instanceof JavaType.FullyQualified && identifier.getFieldType() == null) {
if (!isPartOfQualifiedReference(identifier)) {
unqualifiedTypeNames.add(((JavaType.FullyQualified) type).getFullyQualifiedName());
}
}
return identifier;
}
@Override
public J.Import visitImport(J.Import import_, Set<String> unqualifiedTypeNames) {
return import_;
}
...
}.reduce(cu, new HashSet<>());
}
This visits all identifiers. For BloomFilter in extends AtomicReference<BloomFilter<T>>, the identifier should have type = BloomFilter (if attribution is done properly).
But wait - this method is using reduce with a visitor pattern. The key issue might be that the visitor isn't properly traversing into the type declarations (like the extends clause).
Actually, the issue might be simpler. Let me check if JavaIsoVisitor visits all nodes in the AST. The BloomFilter identifier is in the method declaration or the type hierarchy of the class...
In a class declaration like:
public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> {
}
The type hierarchy (extends/implements) is part of the J.ClassDeclaration. The JavaIsoVisitor should visit all nodes including type parameters.
Let me think about this from a different angle. Let me look at the TypesInUse collection to understand if BloomFilter is properly attributed.
Actually, let me just look at the code more carefully. The issue is in the import checking logic:
String target = qualid.getTarget().toString();
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>());
The qualid.getTarget() for an import like import com.google.common.hash.BloomFilter; would be the target of the field access, which is "com.google.common.hash". And qualid.toString() or qualid.getTarget().toString() might be different things.
Wait, I need to understand the AST structure better. For import com.google.common.hash.BloomFilter:
elem.getQualid() is the field access expression
- The qualid would be
com.google.common.hash.BloomFilter
qualid.getTarget() would be com.google.common.hash (the target of the field access to BloomFilter)
qualid.getName() would be BloomFilter
So qualid.getTarget().toString() = "com.google.common.hash"
And qualid.toString() might be "com.google.common.hash.BloomFilter"
Actually, let me check the import handling:
J.FieldAccess qualid = elem.getQualid();
J.Identifier name = qualid.getName(); // "BloomFilter"
And then for the import check:
String target = qualid.getTarget().toString(); // This could be "com.google.common.hash" or "BloomFilter"
Hmm, I'm not sure about the exact AST structure. Let me look at the test file for a clue:
Looking at the test doNotRemoveInnerClassImport:
java(
"""
import java.util.Map.Entry;
public abstract class MyMapEntry<K, V> implements Entry<K, V> {
}
"""
)
This test expects the import to be kept. So this case works. The difference from our issue is that here Map.Entry is both the import AND the usage (in implements Entry<K, V>).
In our case:
- Import:
com.google.common.hash.BloomFilter
- Usage:
AtomicReference<BloomFilter<T>> (BloomFilter used in generic parameter)
Let me trace through more carefully. For the import com.google.common.hash.BloomFilter:
qualid = the FieldAccess representing the import path
qualid.getTarget() = the target of the field access, which is... I need to think about this.
Actually, looking at the code more carefully, for a simple import like import com.google.common.hash.BloomFilter;, the qualid would have:
- Target: a FieldAccess representing
com.google.common.hash
- Name:
BloomFilter
And for a simple import like import java.util.List;:
- Target:
java.util (possibly as a field access)
- Name:
List
So qualid.getTarget().toString() for com.google.common.hash.BloomFilter would be "com.google.common.hash"
And the code:
String target = qualid.getTarget().toString(); // "com.google.common.hash"
Set<JavaType.FullyQualified> types = typesByPackage.getOrDefault(target, new HashSet<>()); // This WOULD find "com.google.common.hash"!
So for com.google.common.hash.BloomFilter, target = "com.google.common.hash", and we'd look up the key "com.google.common.hash" in typesByPackage.
Now, in typesByPackage, we stored BloomFilter under the key "com.google.common.hash":
typesByPackage.computeIfAbsent(fq.getPackageName(), f -> new HashSet<>()).add(fq);
So we should find BloomFilter! Unless... there's something wrong with how BloomFilter is being stored.
Let me re-examine how BloomFilter gets added to typesByPackage.
For AtomicReference<BloomFilter<T>>:
- This is a
JavaType.Parameterized
parameterized.getType() = AtomicReference (FullyQualified)
parameterized.getTypeParameters() = [typeParam1, typeParam2] where:
- typeParam1 = T (type variable)
- typeParam2 = BloomFilter (but what type is this?)
What is the JavaType of the type parameter BloomFilter? It might be:
- A
JavaType.FullyQualified directly
- Or another
JavaType.Parameterized if it has its own type parameters
Actually, looking at the issue more carefully, BloomFilter<T> itself might be a Parameterized type. So:
- The outer Parameterized is
AtomicReference<...>
- Its type parameters include
BloomFilter<T>
BloomFilter<T> itself is a Parameterized type with base BloomFilter and type param T
Let me trace through:
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());
This adds AtomicReference (the base type of the Parameterized) to the map with key "java.util.concurrent".
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 type parameters:
- T is a type variable,
TypeUtils.asFullyQualified(T) returns null, so it's skipped
- BloomFilter -
TypeUtils.asFullyQualified(BloomFilter<T>)...
Here's the question: is BloomFilter<T> a FullyQualified or a Parameterized?
If it's a Parameterized (which makes sense since BloomFilter takes a type parameter), then TypeUtils.asFullyQualified(BloomFilter<T>) would return null, and it would be skipped!
That's the bug! When the type parameter is itself a Parameterized type, it's not being added to typesByPackage.
Let me verify this. For AtomicReference<BloomFilter<T>>:
parameterized.getTypeParameters() = [T, BloomFilter]
- For
T: asFullyQualified(T) = null → skip
- For
BloomFilter<T>: asFullyQualified(BloomFilter<T>) = null (because it's Parameterized, not FullyQualified) → skip!
So BloomFilter is never added to typesByPackage, which means when we check the import, we find an empty set.
The fix would be to handle the case where type parameters themselves are Parameterized types. We need to recursively process them.
For BloomFilter<T> which is a Parameterized:
- Its base type
BloomFilter should be added to typesByPackage under key "com.google.common.hash"
- Its type parameter
T is a type variable, which doesn't need to be added
Let me write the fix:
Instead of just:
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);
}
}
We need to also handle Parameterized type parameters:
for (JavaType typeParameter : parameterized.getTypeParameters()) {
if (typeParameter instanceof JavaType.FullyQualified) {
JavaType.FullyQualified fq = (JavaType.FullyQualified) typeParameter;
typesByPackage.computeIfAbsent(
fq.getOwningClass() == null ?
fq.getPackageName() :
toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()),
f -> new HashSet<>()).add(fq);
} else if (typeParameter instanceof JavaType.Parameterized) {
JavaType.Parameterized paramTypeParam = (JavaType.Parameterized) typeParameter;
// Add the base type
JavaType.FullyQualified fq = paramTypeParam.getType();
typesByPackage.computeIfAbsent(
fq.getOwningClass() == null ?
fq.getPackageName() :
toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()),
f -> new HashSet<>()).add(fq);
}
}
Or we could extract this into a helper method that recursively processes types.
Let me also think about edge cases:
- Nested generics:
Map<String, List<Integer>> - List<Integer> is Parameterized, and Integer is FullyQualified
- Deep nesting:
List<Map<String, Set<Integer>>> - multiple levels
- Type variables:
T - already handled (skipped)
- Wildcards:
? - might need handling too
For a more general solution, I should handle all cases recursively. Let me create a helper method.
Actually, looking at the code more carefully, I think the simplest fix would be to change the asFullyQualified check to also handle Parameterized types by extracting their base type.
Let me write a helper that converts any type to its "root" FullyQualified:
private Set<JavaType.FullyQualified> collectFullyQualifiedTypes(JavaType type) {
Set<JavaType.FullyQualified> result = new HashSet<>();
if (type instanceof JavaType.FullyQualified) {
result.add((JavaType.FullyQualified) type);
} else if (type instanceof JavaType.Parameterized) {
JavaType.Parameterized pt = (JavaType.Parameterized) type;
result.add(pt.getType());
for (JavaType typeParam : pt.getTypeParameters()) {
result.addAll(collectFullyQualifiedTypes(typeParam));
}
}
return result;
}
Then we can use this in the loop over type parameters.
But wait, I need to be careful about the key used in typesByPackage. Let me re-examine:
for (JavaType javaType : cu.getTypesInUse().getTypesInUse()) {
This iterates over ALL types in use. So for AtomicReference<BloomFilter<T>>, the types in use would include:
AtomicReference<BloomFilter<T>> (Parameterized)
BloomFilter<T> (Parameterized) - as a type parameter
AtomicReference (FullyQualified) - the base of the Parameterized
BloomFilter (FullyQualified) - the base of the nested Parameterized
- Maybe
T (TypeVariable)
Actually, TypesInUse.getTypesInUse() should already return all types, including nested ones. So maybe the issue is different.
Let me check if the loop already handles Parameterized type parameters properly. Looking at the code:
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());
This adds the base type (e.g., AtomicReference, BloomFilter) for any Parameterized type. So if BloomFilter<T> is in typesInUse(), then BloomFilter would be added.
But then the type parameter processing:
for (JavaType typeParameter : parameterized.getTypeParameters()) {
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter);
if (fq != null) {
This tries to get the FullyQualified from type parameters. For BloomFilter<T>, the type parameters are T and BloomFilter<T>. T is a TypeVariable (not FullyQualified), and BloomFilter<T> is a Parameterized (not FullyQualified).
But then, the outer loop would process BloomFilter<T> as another Parameterized and add BloomFilter to the map.
So actually, the issue might be that BloomFilter<T> is NOT in typesInUse(). Let me think about this...
When we have extends AtomicReference<BloomFilter<T>>:
- The class declaration has a type hierarchy
- The type resolution should attribute
AtomicReference<BloomFilter<T>>
- The types in use should include
AtomicReference<BloomFilter<T>> and possibly BloomFilter<T>
I'm not 100% sure if BloomFilter<T> would be included in typesInUse(). It depends on how TypesInUse traverses the AST.
Actually, I think I need to write a test to reproduce the issue first, then debug it.
Let me create a test file and run the existing tests to see what's happening.
Actually wait, let me look at the collectUnqualifiedTypeNames method more carefully. This method visits identifiers in the source code. For BloomFilter<T> in the class declaration, there's an identifier BloomFilter that represents the unqualified type name.
private Set<String> collectUnqualifiedTypeNames(J.CompilationUnit cu) {
return new JavaIsoVisitor<Set<String>>() {
@Override
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;
}
For BloomFilter in AtomicReference<BloomFilter<T>>:
identifier.getType() would be the type of the identifier
- If attribution is done, this should be
BloomFilter (FullyQualified)
identifier.getFieldType() should be null (it's not a field reference)
isPartOfQualifiedReference(identifier) should return false (it's not part of a field access chain like com.google.common.hash.BloomFilter)
So unqualifiedTypeNames should contain "com.google.common.hash.BloomFilter".
But wait, for isPartOfQualifiedReference, let me check:
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;
}
For BloomFilter in AtomicReference<BloomFilter<T>>:
- The identifier
BloomFilter is the name part of a type parameter
- Its parent might be a type parameter list
- It's NOT part of a J.FieldAccess chain
So isPartOfQualifiedReference should return false, and BloomFilter should be in unqualifiedTypeNames.
Now back to the import check:
if ((combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())) || ...) {
anImport.used = false;
For BloomFilter:
combinedTypes is empty (as we discussed)
elem.getTypeName() is "com.google.common.hash.BloomFilter" (the import's type name)
unqualifiedTypeNames.contains("com.google.common.hash.BloomFilter") should be true
So the condition is true && false = false, and the import is NOT removed.
Wait, that means the import should be KEPT! But the user says it's being removed...
Let me re-read the issue:
What did you see instead?
import java.util.concurrent.atomic.AtomicReference;
...
So com.google.common.hash.BloomFilter IS being removed. But based on my analysis, it shouldn't be removed if collectUnqualifiedTypeNames properly finds BloomFilter.
Hmm, let me think about this differently. Maybe the issue is that collectUnqualifiedTypeNames doesn't find BloomFilter because:
- The identifier isn't visited (maybe because it's in a class declaration context?)
- The identifier's type isn't attributed (maybe the test doesn't have type attribution?)
Wait, the test might not have type attribution! In OpenRewrite, types are only attributed when TypeTree#getType() is called with proper type information. In unit tests, the sources might not be fully attributed.
Actually, RemoveUnusedImportsVisitor.visitCompilationUnit uses cu.getTypesInUse() which should have done type attribution. But collectUnqualifiedTypeNames uses a separate visitor that visits the AST...
Let me check if the type attribution is available in the identifiers when collectUnqualifiedTypeNames runs. The visitor uses JavaIsoVisitor, and it checks identifier.getType(). This should return the attributed type if attribution was done.
Actually, I think the issue might be in how the visitor traverses. Let me check if JavaIsoVisitor visits the type hierarchy (extends/implements) of class declarations.
The JavaIsoVisitor should visit all nodes in the AST tree, including the J.ClassDeclaration and its type hierarchy. So it should visit the BloomFilter identifier.
Let me think about this differently. Maybe the problem is that in the test scenario, the types aren't attributed at all, so identifier.getType() returns null or a type that doesn't match.
Actually, let me just run the existing tests and see if they pass, then create a reproduction test.
Let me look at the test file to see if there are any existing tests related to generics in type hierarchies.
Let me search for tests related to extends/implements with generics: