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.
xml<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?
javaimport 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?
javaimport 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?
java - Assistant
I’ll locate
RemoveUnusedImports, its visitor logic, and nearby tests. I’ll then reproduce the generic-type case, make the smallest source change consistent with the project’s type-attribution model, run focused tests (without modifying tests), and inspect the final diff before submitting. - Assistant
The failure path is in the attribution-derived
typesByPackageindex. The current code only records a parameterized type’s direct arguments as fully-qualified types; nested parameterized arguments can be represented asJavaType.Parameterized, and the raw type behind that wrapper is not reliably indexed. I’ll make indexing recursive over both the raw type and all type arguments, preserving existing handling for ordinary fully-qualified types and avoiding generic variables. - Assistant
The recursive indexer is now in place. I also detected an unrelated pre-existing modification to
settings.gradle.kts(removal ofrewrite-android); I will leave it untouched and ensure only the recipe source is part of my change. Next I’m compiling and running the relevant test module. - Assistant
The recursive approach changed wildcard-import unfolding behavior, so I discarded it to avoid regressions. The actual defect is the early “no attributed types” removal branch: it ignored the already-collected source identifier names, even though generic-only references are present in source. I changed both relevant removal predicates to retain an import when its class name appears in source but attribution did not expose it.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.