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.
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 trace
RemoveUnusedImportsand its type-usage accounting, add a focused regression test for the generic superclass case, then run the narrow Java tests and broaden verification if needed. - Assistant
rgisn’t installed in this workspace, so I’m usingfind/grepfor the same repository inspection. - Assistant
The failure sits in the newer “actual source identifier” guard: it compares an import’s qualified identifier node (
BloomFilterinside the import), whose simple name can be represented unexpectedly, instead of comparing the imported class name. I’m first locking the reported Guava example into a regression test to confirm that path. - Assistant
The focused test is still compiling the multi-module Java test fixture; no test output has appeared yet. Once it fails, I’ll inspect the attributed AST values at the relevant comparison rather than weakening the safety checks globally.
- Assistant
The initial Gradle configuration is unusually slow (it has now completed
buildSrcand is progressing), but the daemon is active rather than hung. The repository already had an unrelatedsettings.gradle.ktsmodification; I’m leaving it untouched. - Assistant
Gradle is stalled in dependency/configuration resolution before project tasks, consistent with remote resolution in this environment. Since this task must stay offline, I’m stopping that invocation and rerunning from the local cache with
--offline. - Assistant
The required build plugin
2.20.0is not cached, but the immediately preceding2.19.5plugin is fully cached. I’ll temporarily pin that build-only plugin version for local verification, then restorebuild.gradle.ktsexactly; this won’t be part of the delivered change. - Assistant
The cached plugin gets the build into compilation; the next offline-only blocker is a dynamic
slf4j-api:1.7.+dependency despite1.7.36being cached. I’ll force that exact cached version through a temporary Gradle init script and continue the same focused test. - Assistant
Compilation now succeeds, but the test runner itself cannot resolve uncached JUnit engine/runtime-only artifacts. I’m going to invoke this one compiled
RewriteTestmethod through a temporary plainmain, using only the already-resolved test compile classpath plus the cached Java parser and Guava jars. This preserves the real parser/recipe assertion without requiring the absent JUnit engine. - Assistant
The reported case passes against the current tree because a later generic/unqualified-type fallback already covers it. I’m making that guarantee explicit in the named-import decision path: an import referenced unqualified—including as a parameterized type—will be marked used before attribution-only cleanup runs. This removes the current dependence on incidental identifier-name collection for that case and keeps the regression test.
- Assistant
The implementation and permanent regression test are now in place, and all temporary build changes have been removed. I’m doing one final offline compile/execution of the focused test with the same temporary local-cache harness, then I’ll clean that harness again and inspect the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.