tan*_*o24 4 kotlin kotlin-reflect
Kotlin反射库定义了KDeclarationContainer哪个Represents an entity which may contain declarations of any other entities, such as a class or a package.
this::class返回KClass,扩展KDeclarationContainer,但我如何得到父KDeclarationContainer(a KPackage?)
KPackage现在kotlin 中没有,但你可以得到一个java Package,例如:
val pkg:Package = this::class.java.`package`
Run Code Online (Sandbox Code Playgroud)
如果你真的想要获得一个KPackageImpl实例,你可以从中得到它kotlin.jvm.internal.Reflection,但它没有意义,因为Kotlin的反映还不完整,例如:
val pkg = Reflection.getOrCreateKotlinPackage(this::class.java, "")
// ^--- there is no methods to get package information like as java.lang.Package,
// since it is a `KDeclarationContainer` rather than a `KPackage`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
688 次 |
| 最近记录: |