RemoveUnusedImports removes imports when used in Generics only <!-- Thank you for…
- User
RemoveUnusedImports removes imports when used in Generics only
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.31.1
- Maven/Gradle plugin v6.40.0
How are you running OpenRewrite?
I am using the Maven plugin, and my project is a single module project.
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>6.40.0</version> <executions> <execution> <phase>process-sources</phase> <goals> <goal>run</goal> </goals> <configuration> <activeRecipes combine.children="append"> <recipe>org.openrewrite.java.RemoveUnusedImports</recipe> <recipe>org.openrewrite.java.OrderImports</recipe> </activeRecipes> </configuration> </execution> </executions> </plugin>What is the smallest, simplest way to reproduce the problem?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you expect to see?
import com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you see instead?
import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What is the full stack trace of any errors you encountered?
stacktrace output hereAre you interested in contributing a fix to OpenRewrite?
Interface
No new interfaces are introduced.
- Assistant
Investigating how
RemoveUnusedImportsdetects import usage, focusing on types referenced only in generic arguments. - Assistant
Implementing fixes: nested generic types may be missing from usage maps, and type names in generic positions may not be collected for unqualified/source checks.
- Assistant
Fixed
RemoveUnusedImportsincorrectly dropping imports for types that appear only as generic type arguments (e.g.BloomFilterinextends AtomicReference<BloomFilter<T>>).Cause
Several checks treated an import as unused when it wasn’t in
typesByPackage/unqualifiedTypeNames, even though the simple name was still in source inside a generic. Nested parameterized types weren’t fully walked when buildingtypesByPackage, and unqualified usage wasn’t always recorded when type attribution sat on theParameterizedTypeinstead of the inner identifier.Changes in
RemoveUnusedImports.javaaddTypeToTypesByPackage– Recursively registers raw types from nested generic type arguments intypesByPackage.- Import removal guards – When
combinedTypesis empty or doesn’t match, also require that the import’s simple name is not insourceIdentifierNamesbefore removing the import.
- Assistant
Result:
RemoveUnusedImportswas updated so imports are kept when a type is used only in generics (e.g.BloomFilterinextends AtomicReference<BloomFilter<T>>). Changes are inrewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java(recursive generic type tracking, parameterized-type unqualified usage, and guards that consultsourceIdentifierNamesbefore dropping imports).git diff --checkon that file was clean;:rewrite-java:compileJavasucceeded.Tests:
RemoveUnusedImportsTestdid not run here because:rewrite-java-test:compileTestJavafailed on unrelated missingorg.openrewrite.yaml.Assertionssymbols. After a full repo build, run:./gradlew :rewrite-java-test:test --tests "org.openrewrite.java.RemoveUnusedImportsTest" - Assistant
The verification run finished successfully:
:rewrite-java:compileJavacompleted with exit code 0, andgit diff --checkonRemoveUnusedImports.javareported no issues. The generic-import fix is compiled and the diff is clean from a whitespace/conflict-marker standpoint.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.