Skip to content

RemoveImport: keep package-qualified and inherited static imports#8159

Draft
timtebeek wants to merge 1 commit into
mainfrom
tim/kotlin-removeimport-type-attribution
Draft

RemoveImport: keep package-qualified and inherited static imports#8159
timtebeek wants to merge 1 commit into
mainfrom
tim/kotlin-removeimport-type-attribution

Conversation

@timtebeek

Copy link
Copy Markdown
Member

What's changed?

RemoveImport now keeps two kinds of valid imports it previously dropped, handled generically in the shared rewrite-java recipe with no language-specific (source-path / "Kt" facade) special-casing: members imported by their package-qualified name even though they are compiled onto a synthetic enclosing class (e.g. Kotlin top-level functions on a <File>Kt facade), and members statically imported through a subtype that inherits them (e.g. a Kotlin companion-object member declared on an implemented interface). The first is matched via the used method's declaring-type package plus name (with a guard so a used package-qualified import isn't removed); the second by walking the imported owner's supertypes/interfaces.

What's your motivation?

This expresses the behavior purely in terms of existing JavaType data instead of leaking Kotlin awareness into the shared module, so every recipe calling maybeRemoveImport benefits and no type-attribution/parser changes are needed.

Any additional context

Added Kotlin RemoveImportTest coverage for top-level functions and inherited companion members; keepStarFoldWhenUsingStaticChildAndParentMembersFromJavaClasses stays @Disabled for the pre-existing reason (Java sources can't yet be deps of Kotlin sources in tests). Verified green with no regressions across the full rewrite-java-test module and Kotlin RemoveImportTest / ChangeTypeTest / OrderImportsTest / AddImportTest.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Handle two cases generically in the shared recipe, without language-specific
path or facade special-casing:

- Members imported by their package-qualified name even though they are compiled
  onto a synthetic enclosing class (e.g. Kotlin top-level functions on a
  `<File>Kt` facade), matched via the declaring type's package plus method name.
- Members statically imported through a subtype that inherits them (e.g. a Kotlin
  companion-object member declared on an implemented interface), matched by
  walking the imported owner's supertypes.

No type-attribution changes; driven off existing JavaType data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant