dimanche 20 septembre 2020

How would one use Kotlin's reflection?

I feel like I don't fully understand the depth of some deeps.

$ cat blya.kt 
inline fun<reified T> omg() {
    val x = T::class.supertypes  // such an innocent init
}

fun main() {
    omg<Int>()
}

$ kotlinc -d omg.jar -include-runtime blya.kt && java -jar omg.jar
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
blya.kt:2:6: warning: variable 'x' is never used
    val x = T::class.supertypes
     ^
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:84)
    at kotlin.jvm.internal.ClassReference.getSupertypes(ClassReference.kt:42)
    at BlyaKt.main(blya.kt:9)
    at BlyaKt.main(blya.kt)

$ ls /snap/kotlin/current/lib/kotlin-reflect.jar  -l
-rw-r--r-- 1 root root 2893798 Sep 10 13:15 /snap/kotlin/current/lib/kotlin-reflect.jar

$ # hmm, strage it's not found on default, well, ok, let's add it

$ kotlinc -classpath /snap/kotlin/current/lib/ -d omg.jar -include-runtime blya.kt && java -jar omg.jar
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
blya.kt:2:6: warning: variable 'x' is never used
    val x = T::class.supertypes
     ^
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:84)
    at kotlin.jvm.internal.ClassReference.getSupertypes(ClassReference.kt:42)
    at BlyaKt.main(blya.kt:9)
    at BlyaKt.main(blya.kt)

$ kotlinc -classpath /snap/kotlin/current/lib/kotlin-reflect.jar -d omg.jar -include-runtime blya.kt && java -jar omg.jar
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
blya.kt:2:6: warning: variable 'x' is never used
    val x = T::class.supertypes
     ^
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:84)
    at kotlin.jvm.internal.ClassReference.getSupertypes(ClassReference.kt:42)
    at BlyaKt.main(blya.kt:9)
    at BlyaKt.main(blya.kt)

$ kotlinc -classpath /snap/kotlin/current/lib/kotlin-reflect.jar -d omg.jar -include-runtime blya.kt && java -classpath /snap/kotlin/current/lib -jar omg.jar
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
blya.kt:2:6: warning: variable 'x' is never used
    val x = T::class.supertypes
     ^
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
    at kotlin.jvm.internal.ClassReference.error(ClassReference.kt:84)
    at kotlin.jvm.internal.ClassReference.getSupertypes(ClassReference.kt:42)
    at BlyaKt.main(blya.kt:9)
    at BlyaKt.main(blya.kt)

As if I shouldn't want to use it at all.

Can anyone give some hint on what's happening?





Aucun commentaire:

Enregistrer un commentaire