gpt4 book ai didi

ubuntu - 如何使用 Kotlin 的反射?

转载 作者:行者123 更新时间:2023-12-04 19:08:07 26 4
gpt4 key购买 nike

我觉得我不完全了解某些深处的深度。

$ 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)
好像我根本不想使用它。
即使有详细的输出,它也假装包含它......在某种程度上。
$ kotlinc -verbose -d omg.jar -include-runtime blya.kt
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.
logging: using Kotlin home directory /snap/kotlin/53
logging: configuring the compilation environment
logging: configure scripting: Added template org.jetbrains.kotlin.mainKts.MainKtsScript from [/snap/kotlin/53/lib/kotlin-main-kts.jar, /snap/kotlin/53/lib/kotlin-reflect.jar, /snap/kotlin/53/lib/kotlin-script-runtime.jar, /snap/kotlin/53/lib/kotlin-stdlib.jar]
logging: loading modules: [java.se, jdk.accessibility, jdk.attach, jdk.compiler, jdk.dynalink, jdk.httpserver, jdk.incubator.foreign, jdk.jartool, jdk.javadoc, jdk.jconsole, jdk.jdi, jdk.jfr, jdk.jshell, jdk.jsobject, jdk.management, jdk.management.jfr, jdk.net, jdk.nio.mapmode, jdk.scripting.nashorn, jdk.sctp, jdk.security.auth, jdk.security.jgss, jdk.unsupported, jdk.unsupported.desktop, jdk.xml.dom, java.base, java.compiler, java.datatransfer, java.desktop, java.xml, java.instrument, java.logging, java.management, java.management.rmi, java.rmi, java.naming, java.net.http, java.prefs, java.scripting, java.security.jgss, java.security.sasl, java.sql, java.transaction.xa, java.sql.rowset, java.xml.crypto, jdk.internal.jvmstat, jdk.management.agent, jdk.jdwp.agent, jdk.internal.ed, jdk.internal.le, jdk.internal.opt]
任何人都可以对正在发生的事情给出一些提示吗?

最佳答案

好的,所以一天的简短谷歌搜索发现:

  • 众所周知,Kotlin 不会在生成的 jar 中包含反射实现,这不是“默认”,而是“根本”,即“绝不”。
  • 同时java -jar omg.jar使 java 自动忽略每个类路径设置,也许保存 list 上指定的那些(我无法工作),因为......好吧,这就是原因。

  • 构建和运行的真正管道实际上应该读取
    kotlinc -d omg.jar -include-runtime omg.kt
    java -cp omg.jar:/snap/kotlin/current/lib/* OmgKt
    在哪里 OmgKt是您应该猜到的主要可执行类名称。在简单的情况下,比如这个,它显然是从源文件名派生的,如果你解压缩并查看 jar 文件的内容,它可能可以找到……不知何故,当我第一次开始时它不起作用试过了。
    (而 /snap/kotlin/current 表示典型的 kotlin 安装路径。)

    关于ubuntu - 如何使用 Kotlin 的反射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63982313/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com