0

I am trying to access the companion object of an unknown class with a known interface, given an instance of the class.

Code below:

class AccessTest() {
    companion object {
        val prop = 5
    }
    fun getComp() {
        print(this)
        print(this::class)
        print(this::class.companionObject) // Unresolved reference.
        print(this::class.companionObjectInstance) // Unresolved reference.
    }
}

inline fun <reified T> getCompanion() {
    print(T::class.companionObject) // Unresolved reference.
    print(T::class.companionObjectInstance) // Unresolved reference.
}

fun main() {
    AccessTest().getComp()
    getCompanion<AccessTest>()
}

Output:

$ kotlinc -d main.jar main.kt && kotlin -classpath main.jar MainKt
main.kt:8:27: error: unresolved reference: companionObject
        print(this::class.companionObject) // Unresolved reference.
                          ^
main.kt:9:27: error: unresolved reference: companionObjectInstance
        print(this::class.companionObjectInstance) // Unresolved reference.
                          ^
main.kt:14:20: error: unresolved reference: companionObject
    print(T::class.companionObject) // Unresolved reference.
                   ^
main.kt:15:20: error: unresolved reference: companionObjectInstance
    print(T::class.companionObjectInstance) // Unresolved reference.
                   ^

I do not think this is a duplicate of either of the below questions, as I am specifically asking what has changed or what I am misunderstanding such that the solution in the two below questions is not working for me:

how to access companion object from object instance in kotlin?

Kotlin invoke companion function with reflection

Will Chen
  • 482
  • 4
  • 12
  • Did you add the `kotlin-reflect` dependency to your module/project? – broot Nov 01 '21 at 22:21
  • @broot In the project, yes. The test code, I just ran with `kotlinc`/`kotlin`— Same result if I `import kotlin.reflect.*`. I believe the error message is different if missing the dependency. It will explicitly say `Kotlin reflection is not available` or, or complain about missing `kotlin-reflect.jar`? – Will Chen Nov 01 '21 at 22:29
  • if I don't add `kotlin-reflect` then my IntelliJ just says "unresolved reference". Maybe Android Studio behave differently, I don't know. Generally speaking, compiler and IDEs usually don't know about possible additional libs, so "unresolved reference" is a typical message if you miss a dependency. Anyway, I'm pretty sure you need `kotlin-reflect` for this to work, so I would be surprised if your `kotlinc` would succeed. – broot Nov 01 '21 at 22:54
  • Oh, my bad. `kotlinc` actually includes `kotlin-reflect` by default. – broot Nov 01 '21 at 23:05
  • 2
    So another question is: did you import `companionObject`? I ask because if you use `kotlinc` manually then maybe you don't use IDE at all, so it doesn't handle imports for you. You said that you imported `kotlin.reflect.*`, but `companionObject` is not there. It's in `kotlin.reflect.full.*`. – broot Nov 01 '21 at 23:11
  • @broot Oh! Solved! Thank you! Would you like to post an answer so I can accept it, or would it perhaps be better if I deleted this question and then left a comment/proposed an edit to the answer on the other existing question? – Will Chen Nov 02 '21 at 01:38

1 Answers1

1

After a short discussion in comments, it turned out it was just a missing import.

companionObject is not a member of KClass, but extension on it, so it is possible we have access to KClass object, but we don't see its companionObject property. Also, as it's the part of kotlin-reflect library, it is not located in kotlin.reflect package, but in kotlin.reflect.full, so importing kotlin.reflect.* isn't enough to get it.

broot
  • 21,588
  • 3
  • 30
  • 35