2017-07-20 7 views
1

Kotlin bibliothèque de réflexion définit KDeclarationContainer, qui Represents an entity which may contain declarations of any other entities, such as a class or a package.Comment obtenir un paquet Kotlin par réflexion

this::class retours KClass, qui étend KDeclarationContainer, mais comment puis-je obtenir le parent KDeclarationContainer (un KPackage?)

Répondre

2

Il y a pas KPackage en Kotlin maintenant, mais vous pouvez obtenir un java Package à la place, par exemple:

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

SI vous voulez vraiment obtenir une instance KPackageImpl, vous pouvez l'obtenir à partir kotlin.jvm.internal.Reflection, mais il n'a pas de sens, car Kotlin Reflect est incomplète encore, par exemple:

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` 
+0

Merci, mais J'espérais un équivalent kotlin. Le commentaire d'enregistrement sur https://github.com/JetBrains/kotlin/tree/master/core/builtins/src/kotlin/reflect mentionne en fait un 'KPackage' – tango24