Support reflection on top level properties

This commit is contained in:
Michael Bogdanov
2015-10-13 10:21:49 +03:00
parent e8f9a9f3b8
commit a09c8105c4
2 changed files with 19 additions and 6 deletions
@@ -23,3 +23,6 @@ package kotlin.reflect
*/
public val KDeclarationContainer.functions: Collection<KFunction<*>>
get() = members.filterIsInstance<KFunction<*>>()
public val KDeclarationContainer.properties: Collection<KProperty<*>>
get() = members.filterIsInstance<KProperty<*>>()
@@ -87,12 +87,25 @@ public val Field.kotlinProperty: KProperty<*>?
get() {
if (isSynthetic()) return null
// TODO: fields in package parts
// TODO: optimize (search by name)
val kotlinPackage = getKPackage()
if (kotlinPackage != null) {
return kotlinPackage.members.filterIsInstance<KProperty<*>>().firstOrNull { it.javaField == this }
}
return getDeclaringClass().kotlin.memberProperties.firstOrNull { it.javaField == this }
}
private fun Member.getKPackage(): KDeclarationContainer? {
// TODO: support multifile classes
val fileFacade = declaringClass.getAnnotation(KotlinFileFacade::class.java)
return if (fileFacade != null)
Reflection.getOrCreateKotlinPackage(declaringClass, fileFacade.moduleName)
else null
}
/**
* Returns a [KFunction] instance corresponding to the given Java [Method] instance,
* or `null` if this method cannot be represented by a Kotlin function
@@ -103,11 +116,8 @@ public val Method.kotlinFunction: KFunction<*>?
if (isSynthetic) return null
if (Modifier.isStatic(modifiers)) {
// TODO: support multifile classes
val fileFacade = declaringClass.getAnnotation(KotlinFileFacade::class.java)
if (fileFacade != null) {
val kotlinPackage = Reflection.getOrCreateKotlinPackage(declaringClass, fileFacade.moduleName)
val kotlinPackage = getKPackage()
if (kotlinPackage != null) {
return kotlinPackage.functions.firstOrNull { it.javaMethod == this }
}