Skip to content

Commit a78e07a

Browse files
authored
Imports from companion objects should have owningClass information (#5862)
1 parent faf2c03 commit a78e07a

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeMapping.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,15 @@ class KotlinTypeMapping(
224224
return null
225225
}
226226

227-
// If the symbol is not resolvable we return a NEW ShallowClass to prevent caching on a potentially resolvable class type.
228-
val sym = type.importedFqName!!.topLevelClassAsmType().classId.toSymbol(firSession) ?: return ShallowClass.build(signature)
229-
return type(sym.fir, signature)
227+
// If the symbol is not resolvable, we return a NEW ShallowClass to prevent caching on a potentially resolvable class type.
228+
return type.importedFqName!!.topLevelClassAsmType().classId.toSymbol(firSession)
229+
?.let { type(it.fir, signature) }
230+
?: ShallowClass.build(signature)
231+
.withOwningClass(
232+
(type as? FirResolvedImport)?.resolvedParentClassId?.toSymbol(firSession)
233+
?.let { it as? FirRegularClassSymbol }
234+
?.let { TypeUtils.asFullyQualified(type(it.fir, signature)) }
235+
)
230236
}
231237

232238
private fun packageDirective(signature: String): JavaType? {

rewrite-kotlin/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.params.provider.CsvSource;
2121
import org.openrewrite.Issue;
2222
import org.openrewrite.java.tree.J;
23+
import org.openrewrite.java.tree.JavaType;
24+
import org.openrewrite.java.tree.TypeUtils;
2325
import org.openrewrite.kotlin.KotlinIsoVisitor;
2426
import org.openrewrite.test.RewriteTest;
2527

@@ -182,4 +184,45 @@ void quotedImportWithDollar() {
182184
)
183185
);
184186
}
187+
188+
@Test
189+
void interfaceInformation() {
190+
rewriteRun(
191+
kotlin(
192+
"""
193+
package org.example
194+
interface SuperShared {
195+
fun one() = "one"
196+
}
197+
interface Shared : SuperShared
198+
class A {
199+
companion object : Shared
200+
}
201+
"""
202+
),
203+
kotlin(
204+
"""
205+
import org.example.A.Companion.one
206+
""",
207+
spec -> spec.afterRecipe(cu -> {
208+
JavaType firstImportType = cu.getImports().getFirst().getQualid().getType();
209+
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(firstImportType);
210+
211+
//noinspection DataFlowIssue
212+
assertThat(fullyQualified.getOwningClass().getInterfaces())
213+
.singleElement()
214+
.satisfies(sharedInterface -> {
215+
assertThat(sharedInterface.getFullyQualifiedName()).isEqualTo("org.example.Shared");
216+
assertThat(sharedInterface.getInterfaces())
217+
.singleElement()
218+
.satisfies(it -> {
219+
assertThat(it.getFullyQualifiedName()).isEqualTo("org.example.SuperShared");
220+
assertThat(it.getMethods()).singleElement().extracting(JavaType.Method::getName).isEqualTo("one");
221+
});
222+
});
223+
}
224+
)
225+
)
226+
);
227+
}
185228
}

0 commit comments

Comments
 (0)