4

Kotlin 反射库定义KDeclarationContainer,其中Represents an entity which may contain declarations of any other entities, such as a class or a package.

this::classreturn KClass,它 extends KDeclarationContainer,但是我如何获得父级KDeclarationContainer(a KPackage?)

4

1 回答 1

7

现在 kotlin中没有KPackage,但是你可以得到一个 javaPackage代替,例如:

val pkg:Package = this::class.java.`package`

如果你真的想获取一个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`
于 2017-07-20T09:29:03.217 回答