如何通过反射获取kotlin包

How to get a kotlin package by reflection

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

this::class returns KClass,它扩展了 KDeclarationContainer,但是我如何获得父 KDeclarationContainer(一个 KPackage?)

现在kotlin中没有KPackage,但是你可以用java Package代替,例如:

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

IF你真的想得到一个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`