Skip to content

Imports from companion objects should have supertype information #5862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jevanlingen
Copy link
Contributor

@jevanlingen jevanlingen commented Aug 6, 2025

What's changed?

Non-star imports in Kotlin now retain not only the type of the imported object (e.g., a companion object), but also its supertypes. This ensures that full type information is available for the imported member.

What's your motivation?

When analyzing a Kotlin file, type information such as cu.getTypesInUse().getUsedMethods() or cu.getTypesInUse().getVariables() will refer to the method or variable as declared on its actual type. This can be the declaring class, a superclass, or an interface.

In Kotlin, this becomes problematic when importing a member from a companion object that implements an interface. The import itself only carries the type of the companion object, not of its supertype. As a result, the type of the used method and the type of the imported symbol don’t match, making it impossible to correlate the import with the usage.

By including supertype information on imported members, this allows recipes to match imports against used methods and variables, even when they’re inherited from a supertype.

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

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 6, 2025
@jevanlingen jevanlingen moved this from In Progress to Backlog in OpenRewrite Aug 6, 2025
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek marked this pull request as draft August 6, 2025 12:43
spec -> spec.afterRecipe(cu -> {
//noinspection DataFlowIssue
var supertype = TypeUtils.asFullyQualified(cu.getImports().getFirst().getQualid().getType()).getSupertype();
assertThat(supertype).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently fails with

java.lang.AssertionError:  
Expecting actual not to be null  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

2 participants