如何使用 Kotlin 的反射?



我觉得我没有完全理解某些深度的深度。

$ 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]

有人能告诉我发生了什么吗?

好的,所以花了一天的时间在谷歌上搜索发现:

  1. 众所周知,Kotlin不包括将实现反射到所生成的jar中,这不是"默认情况下";,即";"完全";,即";绝不";。

  2. 同时,java -jar omg.jar使java自动忽略每个类路径设置,除了清单上指定的那些(我无法使其工作(,因为。。。这就是的原因。

要构建和运行的真正管道实际上应该读取

kotlinc -d omg.jar -include-runtime omg.kt
java -cp omg.jar:/snap/kotlin/current/lib/* OmgKt

其中OmgKt是您应该猜测的主要可执行类名。在简单的情况下,比如这个,它显然是从源文件名派生的,如果你解压缩并查看jar文件的内容,它可能会被找到。。。不知怎么的,当我第一次尝试它的时候,它一开始就不起作用

(/snap/kotlin/current表示典型的kotlin安装路径。(

相关内容

  • 没有找到相关文章

最新更新