Cache declared/non-declared static/non-static KClass members
Making KClassImpl.Data, its superclass and ReflectProperties public was required because they are accessed from other package in KClasses.kt
This commit is contained in:
@@ -70,10 +70,7 @@ val KClass<*>.defaultType: KType
|
||||
* Does not include members declared in supertypes.
|
||||
*/
|
||||
val KClass<*>.declaredMembers: Collection<KCallable<*>>
|
||||
get() = with(this as KClassImpl) {
|
||||
(getMembers(memberScope, declaredOnly = true) +
|
||||
getMembers(staticScope, declaredOnly = true)).toList()
|
||||
}
|
||||
get() = (this as KClassImpl).data().declaredMembers.toList()
|
||||
|
||||
/**
|
||||
* Returns all functions declared in this class, including all non-static methods declared in the class
|
||||
@@ -86,8 +83,7 @@ val KClass<*>.functions: Collection<KFunction<*>>
|
||||
* Returns static functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.staticFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(staticScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl).data().allStaticMembers
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
|
||||
@@ -95,8 +91,7 @@ val KClass<*>.staticFunctions: Collection<KFunction<*>>
|
||||
* Returns non-extension non-static functions declared in this class and all of its superclasses.
|
||||
*/
|
||||
val KClass<*>.memberFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(memberScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl).data().allNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
@@ -105,8 +100,7 @@ val KClass<*>.memberFunctions: Collection<KFunction<*>>
|
||||
* Returns extension functions declared in this class and all of its superclasses.
|
||||
*/
|
||||
val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(memberScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl).data().allNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
@@ -117,9 +111,7 @@ val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>
|
||||
* declared in the class and the superclasses, as well as static methods declared in the class.
|
||||
*/
|
||||
val KClass<*>.declaredFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(memberScope, declaredOnly = true)
|
||||
.plus(getMembers(staticScope, declaredOnly = true))
|
||||
get() = (this as KClassImpl).data().declaredMembers
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
|
||||
@@ -127,8 +119,7 @@ val KClass<*>.declaredFunctions: Collection<KFunction<*>>
|
||||
* Returns non-extension non-static functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(memberScope, declaredOnly = true)
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
@@ -137,8 +128,7 @@ val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>
|
||||
* Returns extension functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(memberScope, declaredOnly = true)
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
@@ -148,8 +138,7 @@ val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>
|
||||
* Only properties representing static fields of Java classes are considered static.
|
||||
*/
|
||||
val KClass<*>.staticProperties: Collection<KProperty0<*>>
|
||||
get() = (this as KClassImpl)
|
||||
.getMembers(staticScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl).data().allStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty0<*>>()
|
||||
.toList()
|
||||
@@ -158,8 +147,7 @@ val KClass<*>.staticProperties: Collection<KProperty0<*>>
|
||||
* Returns non-extension properties declared in this class and all of its superclasses.
|
||||
*/
|
||||
val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>
|
||||
get() = (this as KClassImpl<T>)
|
||||
.getMembers(memberScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl<T>).data().allNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty1<T, *>>()
|
||||
.toList()
|
||||
@@ -168,8 +156,7 @@ val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>
|
||||
* Returns extension properties declared in this class and all of its superclasses.
|
||||
*/
|
||||
val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *>>
|
||||
get() = (this as KClassImpl<T>)
|
||||
.getMembers(memberScope, declaredOnly = false)
|
||||
get() = (this as KClassImpl<T>).data().allNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KProperty2<T, *, *>>()
|
||||
.toList()
|
||||
@@ -178,8 +165,7 @@ val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *
|
||||
* Returns non-extension properties declared in this class.
|
||||
*/
|
||||
val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>>
|
||||
get() = (this as KClassImpl<T>)
|
||||
.getMembers(memberScope, declaredOnly = true)
|
||||
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty1<T, *>>()
|
||||
.toList()
|
||||
@@ -188,8 +174,7 @@ val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>>
|
||||
* Returns extension properties declared in this class.
|
||||
*/
|
||||
val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>>
|
||||
get() = (this as KClassImpl<T>)
|
||||
.getMembers(memberScope, declaredOnly = true)
|
||||
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KProperty2<T, *, *>>()
|
||||
.toList()
|
||||
|
||||
@@ -32,10 +32,12 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies
|
||||
import kotlin.jvm.internal.TypeIntrinsics
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.INHERITED
|
||||
|
||||
internal class KClassImpl<T : Any>(override val jClass: Class<T>) :
|
||||
KDeclarationContainerImpl(), KClass<T>, KClassifierImpl, KAnnotatedElementImpl {
|
||||
private inner class Data : KDeclarationContainerImpl.Data() {
|
||||
inner class Data : KDeclarationContainerImpl.Data() {
|
||||
val descriptor: ClassDescriptor by ReflectProperties.lazySoft {
|
||||
val classId = classId
|
||||
val moduleData = data().moduleData
|
||||
@@ -60,9 +62,26 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) :
|
||||
}
|
||||
field.get(null) as T
|
||||
}
|
||||
|
||||
val declaredNonStaticMembers: Sequence<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(memberScope, DECLARED) }
|
||||
val declaredStaticMembers: Sequence<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(staticScope, DECLARED) }
|
||||
val inheritedNonStaticMembers: Sequence<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(memberScope, INHERITED) }
|
||||
val inheritedStaticMembers: Sequence<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(staticScope, INHERITED) }
|
||||
|
||||
val allNonStaticMembers: Sequence<KCallableImpl<*>> get() = declaredNonStaticMembers + inheritedNonStaticMembers
|
||||
val allStaticMembers: Sequence<KCallableImpl<*>> get() = declaredStaticMembers + inheritedStaticMembers
|
||||
val declaredMembers: Sequence<KCallableImpl<*>> get() = declaredNonStaticMembers + declaredStaticMembers
|
||||
|
||||
val allMembers: Collection<KCallable<*>> by ReflectProperties.lazySoft {
|
||||
(allNonStaticMembers + allStaticMembers).toList()
|
||||
}
|
||||
}
|
||||
|
||||
private val data = ReflectProperties.lazy { Data() }
|
||||
val data = ReflectProperties.lazy { Data() }
|
||||
|
||||
override val descriptor: ClassDescriptor get() = data().descriptor
|
||||
|
||||
@@ -74,8 +93,7 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) :
|
||||
|
||||
internal val staticScope: MemberScope get() = descriptor.staticScope
|
||||
|
||||
override val members: Collection<KCallable<*>>
|
||||
get() = (getMembers(memberScope, declaredOnly = false) + getMembers(staticScope, declaredOnly = false)).toList()
|
||||
override val members: Collection<KCallable<*>> get() = data().allMembers
|
||||
|
||||
override val constructorDescriptors: Collection<ConstructorDescriptor>
|
||||
get() {
|
||||
|
||||
@@ -31,7 +31,7 @@ import kotlin.jvm.internal.ClassBasedDeclarationContainer
|
||||
import kotlin.reflect.KotlinReflectionInternalError
|
||||
|
||||
internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContainer {
|
||||
protected abstract inner class Data {
|
||||
abstract inner class Data {
|
||||
// This is stored here on a soft reference to prevent GC from destroying the weak reference to it in the moduleByClassLoader cache
|
||||
val moduleData: RuntimeModuleData by ReflectProperties.lazySoft {
|
||||
jClass.getOrCreateModule()
|
||||
@@ -47,33 +47,37 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
|
||||
abstract fun getFunctions(name: Name): Collection<FunctionDescriptor>
|
||||
|
||||
fun getMembers(scope: MemberScope, declaredOnly: Boolean): Sequence<KCallableImpl<*>> {
|
||||
val visitor = object : DeclarationDescriptorVisitorEmptyBodies<KCallableImpl<*>?, Unit>() {
|
||||
private fun skipCallable(descriptor: CallableMemberDescriptor): Boolean =
|
||||
declaredOnly && !descriptor.kind.isReal
|
||||
protected fun getMembers(scope: MemberScope, belonginess: MemberBelonginess): Sequence<KCallableImpl<*>> {
|
||||
val visitor = object : DeclarationDescriptorVisitorEmptyBodies<KCallableImpl<*>, Unit>() {
|
||||
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit): KCallableImpl<*> =
|
||||
createProperty(descriptor)
|
||||
|
||||
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit): KCallableImpl<*>? {
|
||||
return if (skipCallable(descriptor)) null else createProperty(descriptor)
|
||||
}
|
||||
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Unit): KCallableImpl<*> =
|
||||
KFunctionImpl(this@KDeclarationContainerImpl, descriptor)
|
||||
|
||||
override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Unit): KCallableImpl<*>? {
|
||||
return if (skipCallable(descriptor)) null else KFunctionImpl(this@KDeclarationContainerImpl, descriptor)
|
||||
}
|
||||
|
||||
override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit): KCallableImpl<*>? {
|
||||
throw IllegalStateException("No constructors should appear in this scope: $descriptor")
|
||||
}
|
||||
override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit): KCallableImpl<*> =
|
||||
throw IllegalStateException("No constructors should appear in this scope: $descriptor")
|
||||
}
|
||||
|
||||
return scope.getContributedDescriptors().asSequence()
|
||||
.filter { descriptor ->
|
||||
descriptor !is MemberDescriptor || descriptor.visibility != Visibilities.INVISIBLE_FAKE
|
||||
descriptor is CallableMemberDescriptor &&
|
||||
descriptor.visibility != Visibilities.INVISIBLE_FAKE &&
|
||||
belonginess.accept(descriptor)
|
||||
}
|
||||
.mapNotNull { descriptor ->
|
||||
descriptor.accept(visitor, Unit)
|
||||
}
|
||||
}
|
||||
|
||||
protected enum class MemberBelonginess {
|
||||
DECLARED,
|
||||
INHERITED;
|
||||
|
||||
fun accept(member: CallableMemberDescriptor): Boolean =
|
||||
member.kind.isReal == (this == DECLARED)
|
||||
}
|
||||
|
||||
private fun createProperty(descriptor: PropertyDescriptor): KPropertyImpl<*> {
|
||||
val receiverCount = (descriptor.dispatchReceiverParameter?.let { 1 } ?: 0) +
|
||||
(descriptor.extensionReceiverParameter?.let { 1 } ?: 0)
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
|
||||
import kotlin.reflect.KCallable
|
||||
import kotlin.reflect.jvm.internal.KDeclarationContainerImpl.MemberBelonginess.DECLARED
|
||||
|
||||
internal class KPackageImpl(override val jClass: Class<*>, val moduleName: String) : KDeclarationContainerImpl() {
|
||||
private inner class Data : KDeclarationContainerImpl.Data() {
|
||||
@@ -48,6 +49,15 @@ internal class KPackageImpl(override val jClass: Class<*>, val moduleName: Strin
|
||||
jClass
|
||||
}
|
||||
}
|
||||
|
||||
val members: Collection<KCallableImpl<*>> by ReflectProperties.lazySoft {
|
||||
getMembers(scope, DECLARED).filter { member ->
|
||||
val callableDescriptor = member.descriptor as DeserializedCallableMemberDescriptor
|
||||
val packageFragment = callableDescriptor.containingDeclaration as PackageFragmentDescriptor
|
||||
val source = (packageFragment as? LazyJavaPackageFragment)?.source as? KotlinJvmBinaryPackageSourceElement
|
||||
(source?.getContainingBinaryClass(callableDescriptor) as? ReflectKotlinClass)?.klass == jClass
|
||||
}.toList()
|
||||
}
|
||||
}
|
||||
|
||||
private val data = ReflectProperties.lazy { Data() }
|
||||
@@ -56,13 +66,7 @@ internal class KPackageImpl(override val jClass: Class<*>, val moduleName: Strin
|
||||
|
||||
private val scope: MemberScope get() = data().descriptor.memberScope
|
||||
|
||||
override val members: Collection<KCallable<*>>
|
||||
get() = getMembers(scope, declaredOnly = false).filter { member ->
|
||||
val callableDescriptor = member.descriptor as DeserializedCallableMemberDescriptor
|
||||
val packageFragment = callableDescriptor.containingDeclaration as PackageFragmentDescriptor
|
||||
val source = (packageFragment as? LazyJavaPackageFragment)?.source as? KotlinJvmBinaryPackageSourceElement
|
||||
(source?.getContainingBinaryClass(callableDescriptor) as? ReflectKotlinClass)?.klass == jClass
|
||||
}.toList()
|
||||
override val members: Collection<KCallable<*>> get() = data().members
|
||||
|
||||
override val constructorDescriptors: Collection<ConstructorDescriptor>
|
||||
get() = emptyList()
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/* package */ class ReflectProperties {
|
||||
public class ReflectProperties {
|
||||
public static abstract class Val<T> {
|
||||
private static final Object NULL_VALUE = new Object() {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user