Use collections instead of sequences in KClassImpl.Data
Create less intermediate objects in extensions in KClasses.kt
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
|
||||
@file:JvmName("KClasses")
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package kotlin.reflect
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
@@ -70,7 +72,7 @@ val KClass<*>.defaultType: KType
|
||||
* Does not include members declared in supertypes.
|
||||
*/
|
||||
val KClass<*>.declaredMembers: Collection<KCallable<*>>
|
||||
get() = (this as KClassImpl).data().declaredMembers.toList()
|
||||
get() = (this as KClassImpl).data().declaredMembers
|
||||
|
||||
/**
|
||||
* Returns all functions declared in this class, including all non-static methods declared in the class
|
||||
@@ -83,27 +85,19 @@ val KClass<*>.functions: Collection<KFunction<*>>
|
||||
* Returns static functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.staticFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl).data().allStaticMembers
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().allStaticMembers.filterIsInstance<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).data().allNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().allNonStaticMembers.filter { it.isNotExtension && it is KFunction<*> } as Collection<KFunction<*>>
|
||||
|
||||
/**
|
||||
* Returns extension functions declared in this class and all of its superclasses.
|
||||
*/
|
||||
val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl).data().allNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().allNonStaticMembers.filter { it.isExtension && it is KFunction<*> } as Collection<KFunction<*>>
|
||||
|
||||
/**
|
||||
* Returns all functions declared in this class.
|
||||
@@ -111,80 +105,57 @@ 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).data().declaredMembers
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().declaredMembers.filterIsInstance<KFunction<*>>()
|
||||
|
||||
/**
|
||||
* Returns non-extension non-static functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers.filter { it.isNotExtension && it is KFunction<*> } as Collection<KFunction<*>>
|
||||
|
||||
/**
|
||||
* Returns extension functions declared in this class.
|
||||
*/
|
||||
val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KFunction<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().declaredNonStaticMembers.filter { it.isExtension && it is KFunction<*> } as Collection<KFunction<*>>
|
||||
|
||||
/**
|
||||
* Returns static properties declared in this class.
|
||||
* Only properties representing static fields of Java classes are considered static.
|
||||
*/
|
||||
val KClass<*>.staticProperties: Collection<KProperty0<*>>
|
||||
get() = (this as KClassImpl).data().allStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty0<*>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl).data().allStaticMembers.filter { it.isNotExtension && it is KProperty0<*> } as 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>).data().allNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty1<T, *>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl<T>).data().allNonStaticMembers.filter { it.isNotExtension && it is KProperty1<*, *> } as 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>).data().allNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KProperty2<T, *, *>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl<T>).data().allNonStaticMembers.filter { it.isExtension && it is KProperty2<*, *, *> } as 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>).data().declaredNonStaticMembers
|
||||
.filterNotExtensions()
|
||||
.filterIsInstance<KProperty1<T, *>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers.filter { it.isNotExtension && it is KProperty1<*, *> } as Collection<KProperty1<T, *>>
|
||||
|
||||
/**
|
||||
* Returns extension properties declared in this class.
|
||||
*/
|
||||
val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>>
|
||||
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers
|
||||
.filterExtensions()
|
||||
.filterIsInstance<KProperty2<T, *, *>>()
|
||||
.toList()
|
||||
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers.filter { it.isExtension && it is KProperty2<*, *, *> } as Collection<KProperty2<T, *, *>>
|
||||
|
||||
|
||||
private fun <T : KCallableImpl<*>> Sequence<T>.filterExtensions(): Sequence<T> =
|
||||
filter { it.descriptor.extensionReceiverParameter != null }
|
||||
private val KCallableImpl<*>.isExtension: Boolean
|
||||
get() = descriptor.extensionReceiverParameter != null
|
||||
|
||||
private fun <T : KCallableImpl<*>> Sequence<T>.filterNotExtensions(): Sequence<T> =
|
||||
filter { it.descriptor.extensionReceiverParameter == null }
|
||||
private val KCallableImpl<*>.isNotExtension: Boolean
|
||||
get() = !isExtension
|
||||
|
||||
/**
|
||||
* Immediate superclasses of this class, in the order they are listed in the source code.
|
||||
@@ -256,7 +227,6 @@ fun KClass<*>.isSuperclassOf(derived: KClass<*>): Boolean =
|
||||
* @see [KClass.isInstance]
|
||||
* @see [KClass.safeCast]
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
|
||||
return value as T
|
||||
@@ -269,7 +239,6 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
|
||||
* @see [KClass.isInstance]
|
||||
* @see [KClass.cast]
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
|
||||
return if (isInstance(value)) value as T else null
|
||||
}
|
||||
|
||||
@@ -134,22 +134,23 @@ internal class KClassImpl<T : Any>(override val jClass: Class<T>) : KDeclaration
|
||||
}
|
||||
}
|
||||
|
||||
val declaredNonStaticMembers: Sequence<KCallableImpl<*>>
|
||||
val declaredNonStaticMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(memberScope, DECLARED) }
|
||||
val declaredStaticMembers: Sequence<KCallableImpl<*>>
|
||||
val declaredStaticMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(staticScope, DECLARED) }
|
||||
val inheritedNonStaticMembers: Sequence<KCallableImpl<*>>
|
||||
val inheritedNonStaticMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { getMembers(memberScope, INHERITED) }
|
||||
val inheritedStaticMembers: Sequence<KCallableImpl<*>>
|
||||
val inheritedStaticMembers: Collection<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()
|
||||
}
|
||||
val allNonStaticMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { declaredNonStaticMembers + inheritedNonStaticMembers }
|
||||
val allStaticMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { declaredStaticMembers + inheritedStaticMembers }
|
||||
val declaredMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { declaredNonStaticMembers + declaredStaticMembers }
|
||||
val allMembers: Collection<KCallableImpl<*>>
|
||||
by ReflectProperties.lazySoft { allNonStaticMembers + allStaticMembers }
|
||||
}
|
||||
|
||||
val data = ReflectProperties.lazy { Data() }
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.load.java.structure.reflect.safeClassLoader
|
||||
import org.jetbrains.kotlin.load.kotlin.reflect.RuntimeModuleData
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.utils.toReadOnlyList
|
||||
import java.lang.reflect.Constructor
|
||||
import java.lang.reflect.Method
|
||||
import java.util.*
|
||||
@@ -47,7 +48,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
|
||||
abstract fun getFunctions(name: Name): Collection<FunctionDescriptor>
|
||||
|
||||
protected fun getMembers(scope: MemberScope, belonginess: MemberBelonginess): Sequence<KCallableImpl<*>> {
|
||||
protected fun getMembers(scope: MemberScope, belonginess: MemberBelonginess): Collection<KCallableImpl<*>> {
|
||||
val visitor = object : DeclarationDescriptorVisitorEmptyBodies<KCallableImpl<*>, Unit>() {
|
||||
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit): KCallableImpl<*> =
|
||||
createProperty(descriptor)
|
||||
@@ -59,15 +60,13 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
|
||||
throw IllegalStateException("No constructors should appear in this scope: $descriptor")
|
||||
}
|
||||
|
||||
return scope.getContributedDescriptors().asSequence()
|
||||
.filter { descriptor ->
|
||||
descriptor is CallableMemberDescriptor &&
|
||||
descriptor.visibility != Visibilities.INVISIBLE_FAKE &&
|
||||
belonginess.accept(descriptor)
|
||||
}
|
||||
.mapNotNull { descriptor ->
|
||||
descriptor.accept(visitor, Unit)
|
||||
}
|
||||
return scope.getContributedDescriptors().mapNotNull { descriptor ->
|
||||
if (descriptor is CallableMemberDescriptor &&
|
||||
descriptor.visibility != Visibilities.INVISIBLE_FAKE &&
|
||||
belonginess.accept(descriptor))
|
||||
descriptor.accept(visitor, Unit)
|
||||
else null
|
||||
}.toReadOnlyList()
|
||||
}
|
||||
|
||||
protected enum class MemberBelonginess {
|
||||
|
||||
@@ -56,7 +56,7 @@ internal class KPackageImpl(override val jClass: Class<*>, val moduleName: Strin
|
||||
val packageFragment = callableDescriptor.containingDeclaration as PackageFragmentDescriptor
|
||||
val source = (packageFragment as? LazyJavaPackageFragment)?.source as? KotlinJvmBinaryPackageSourceElement
|
||||
(source?.getContainingBinaryClass(callableDescriptor) as? ReflectKotlinClass)?.klass == jClass
|
||||
}.toList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user