Use collections instead of sequences in KClassImpl.Data

Create less intermediate objects in extensions in KClasses.kt
This commit is contained in:
Alexander Udalov
2016-09-12 18:00:24 +03:00
parent b9dec41ea0
commit 5b047684ab
4 changed files with 40 additions and 71 deletions
@@ -15,6 +15,8 @@
*/ */
@file:JvmName("KClasses") @file:JvmName("KClasses")
@file:Suppress("UNCHECKED_CAST")
package kotlin.reflect package kotlin.reflect
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
@@ -70,7 +72,7 @@ val KClass<*>.defaultType: KType
* Does not include members declared in supertypes. * Does not include members declared in supertypes.
*/ */
val KClass<*>.declaredMembers: Collection<KCallable<*>> 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 * 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. * Returns static functions declared in this class.
*/ */
val KClass<*>.staticFunctions: Collection<KFunction<*>> val KClass<*>.staticFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().allStaticMembers get() = (this as KClassImpl).data().allStaticMembers.filterIsInstance<KFunction<*>>()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns non-extension non-static functions declared in this class and all of its superclasses. * Returns non-extension non-static functions declared in this class and all of its superclasses.
*/ */
val KClass<*>.memberFunctions: Collection<KFunction<*>> val KClass<*>.memberFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().allNonStaticMembers get() = (this as KClassImpl).data().allNonStaticMembers.filter { it.isNotExtension && it is KFunction<*> } as Collection<KFunction<*>>
.filterNotExtensions()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns extension functions declared in this class and all of its superclasses. * Returns extension functions declared in this class and all of its superclasses.
*/ */
val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>> val KClass<*>.memberExtensionFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().allNonStaticMembers get() = (this as KClassImpl).data().allNonStaticMembers.filter { it.isExtension && it is KFunction<*> } as Collection<KFunction<*>>
.filterExtensions()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns all functions declared in this class. * 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. * declared in the class and the superclasses, as well as static methods declared in the class.
*/ */
val KClass<*>.declaredFunctions: Collection<KFunction<*>> val KClass<*>.declaredFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().declaredMembers get() = (this as KClassImpl).data().declaredMembers.filterIsInstance<KFunction<*>>()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns non-extension non-static functions declared in this class. * Returns non-extension non-static functions declared in this class.
*/ */
val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>> val KClass<*>.declaredMemberFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().declaredNonStaticMembers get() = (this as KClassImpl).data().declaredNonStaticMembers.filter { it.isNotExtension && it is KFunction<*> } as Collection<KFunction<*>>
.filterNotExtensions()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns extension functions declared in this class. * Returns extension functions declared in this class.
*/ */
val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>> val KClass<*>.declaredMemberExtensionFunctions: Collection<KFunction<*>>
get() = (this as KClassImpl).data().declaredNonStaticMembers get() = (this as KClassImpl).data().declaredNonStaticMembers.filter { it.isExtension && it is KFunction<*> } as Collection<KFunction<*>>
.filterExtensions()
.filterIsInstance<KFunction<*>>()
.toList()
/** /**
* Returns static properties declared in this class. * Returns static properties declared in this class.
* Only properties representing static fields of Java classes are considered static. * Only properties representing static fields of Java classes are considered static.
*/ */
val KClass<*>.staticProperties: Collection<KProperty0<*>> val KClass<*>.staticProperties: Collection<KProperty0<*>>
get() = (this as KClassImpl).data().allStaticMembers get() = (this as KClassImpl).data().allStaticMembers.filter { it.isNotExtension && it is KProperty0<*> } as Collection<KProperty0<*>>
.filterNotExtensions()
.filterIsInstance<KProperty0<*>>()
.toList()
/** /**
* Returns non-extension properties declared in this class and all of its superclasses. * Returns non-extension properties declared in this class and all of its superclasses.
*/ */
val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>> val <T : Any> KClass<T>.memberProperties: Collection<KProperty1<T, *>>
get() = (this as KClassImpl<T>).data().allNonStaticMembers get() = (this as KClassImpl<T>).data().allNonStaticMembers.filter { it.isNotExtension && it is KProperty1<*, *> } as Collection<KProperty1<T, *>>
.filterNotExtensions()
.filterIsInstance<KProperty1<T, *>>()
.toList()
/** /**
* Returns extension properties declared in this class and all of its superclasses. * Returns extension properties declared in this class and all of its superclasses.
*/ */
val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *>> val <T : Any> KClass<T>.memberExtensionProperties: Collection<KProperty2<T, *, *>>
get() = (this as KClassImpl<T>).data().allNonStaticMembers get() = (this as KClassImpl<T>).data().allNonStaticMembers.filter { it.isExtension && it is KProperty2<*, *, *> } as Collection<KProperty2<T, *, *>>
.filterExtensions()
.filterIsInstance<KProperty2<T, *, *>>()
.toList()
/** /**
* Returns non-extension properties declared in this class. * Returns non-extension properties declared in this class.
*/ */
val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>> val <T : Any> KClass<T>.declaredMemberProperties: Collection<KProperty1<T, *>>
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers get() = (this as KClassImpl<T>).data().declaredNonStaticMembers.filter { it.isNotExtension && it is KProperty1<*, *> } as Collection<KProperty1<T, *>>
.filterNotExtensions()
.filterIsInstance<KProperty1<T, *>>()
.toList()
/** /**
* Returns extension properties declared in this class. * Returns extension properties declared in this class.
*/ */
val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>> val <T : Any> KClass<T>.declaredMemberExtensionProperties: Collection<KProperty2<T, *, *>>
get() = (this as KClassImpl<T>).data().declaredNonStaticMembers get() = (this as KClassImpl<T>).data().declaredNonStaticMembers.filter { it.isExtension && it is KProperty2<*, *, *> } as Collection<KProperty2<T, *, *>>
.filterExtensions()
.filterIsInstance<KProperty2<T, *, *>>()
.toList()
private fun <T : KCallableImpl<*>> Sequence<T>.filterExtensions(): Sequence<T> = private val KCallableImpl<*>.isExtension: Boolean
filter { it.descriptor.extensionReceiverParameter != null } get() = descriptor.extensionReceiverParameter != null
private fun <T : KCallableImpl<*>> Sequence<T>.filterNotExtensions(): Sequence<T> = private val KCallableImpl<*>.isNotExtension: Boolean
filter { it.descriptor.extensionReceiverParameter == null } get() = !isExtension
/** /**
* Immediate superclasses of this class, in the order they are listed in the source code. * 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.isInstance]
* @see [KClass.safeCast] * @see [KClass.safeCast]
*/ */
@Suppress("UNCHECKED_CAST")
fun <T : Any> KClass<T>.cast(value: Any?): T { fun <T : Any> KClass<T>.cast(value: Any?): T {
if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName") if (!isInstance(value)) throw TypeCastException("Value cannot be cast to $qualifiedName")
return value as T return value as T
@@ -269,7 +239,6 @@ fun <T : Any> KClass<T>.cast(value: Any?): T {
* @see [KClass.isInstance] * @see [KClass.isInstance]
* @see [KClass.cast] * @see [KClass.cast]
*/ */
@Suppress("UNCHECKED_CAST")
fun <T : Any> KClass<T>.safeCast(value: Any?): T? { fun <T : Any> KClass<T>.safeCast(value: Any?): T? {
return if (isInstance(value)) value as T else null 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) } by ReflectProperties.lazySoft { getMembers(memberScope, DECLARED) }
val declaredStaticMembers: Sequence<KCallableImpl<*>> val declaredStaticMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { getMembers(staticScope, DECLARED) } by ReflectProperties.lazySoft { getMembers(staticScope, DECLARED) }
val inheritedNonStaticMembers: Sequence<KCallableImpl<*>> val inheritedNonStaticMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { getMembers(memberScope, INHERITED) } by ReflectProperties.lazySoft { getMembers(memberScope, INHERITED) }
val inheritedStaticMembers: Sequence<KCallableImpl<*>> val inheritedStaticMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { getMembers(staticScope, INHERITED) } by ReflectProperties.lazySoft { getMembers(staticScope, INHERITED) }
val allNonStaticMembers: Sequence<KCallableImpl<*>> get() = declaredNonStaticMembers + inheritedNonStaticMembers val allNonStaticMembers: Collection<KCallableImpl<*>>
val allStaticMembers: Sequence<KCallableImpl<*>> get() = declaredStaticMembers + inheritedStaticMembers by ReflectProperties.lazySoft { declaredNonStaticMembers + inheritedNonStaticMembers }
val declaredMembers: Sequence<KCallableImpl<*>> get() = declaredNonStaticMembers + declaredStaticMembers val allStaticMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { declaredStaticMembers + inheritedStaticMembers }
val allMembers: Collection<KCallable<*>> by ReflectProperties.lazySoft { val declaredMembers: Collection<KCallableImpl<*>>
(allNonStaticMembers + allStaticMembers).toList() by ReflectProperties.lazySoft { declaredNonStaticMembers + declaredStaticMembers }
} val allMembers: Collection<KCallableImpl<*>>
by ReflectProperties.lazySoft { allNonStaticMembers + allStaticMembers }
} }
val data = ReflectProperties.lazy { Data() } 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.load.kotlin.reflect.RuntimeModuleData
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.utils.toReadOnlyList
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
import java.lang.reflect.Method import java.lang.reflect.Method
import java.util.* import java.util.*
@@ -47,7 +48,7 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
abstract fun getFunctions(name: Name): Collection<FunctionDescriptor> 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>() { val visitor = object : DeclarationDescriptorVisitorEmptyBodies<KCallableImpl<*>, Unit>() {
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit): KCallableImpl<*> = override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit): KCallableImpl<*> =
createProperty(descriptor) createProperty(descriptor)
@@ -59,15 +60,13 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
throw IllegalStateException("No constructors should appear in this scope: $descriptor") throw IllegalStateException("No constructors should appear in this scope: $descriptor")
} }
return scope.getContributedDescriptors().asSequence() return scope.getContributedDescriptors().mapNotNull { descriptor ->
.filter { descriptor -> if (descriptor is CallableMemberDescriptor &&
descriptor is CallableMemberDescriptor && descriptor.visibility != Visibilities.INVISIBLE_FAKE &&
descriptor.visibility != Visibilities.INVISIBLE_FAKE && belonginess.accept(descriptor))
belonginess.accept(descriptor) descriptor.accept(visitor, Unit)
} else null
.mapNotNull { descriptor -> }.toReadOnlyList()
descriptor.accept(visitor, Unit)
}
} }
protected enum class MemberBelonginess { 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 packageFragment = callableDescriptor.containingDeclaration as PackageFragmentDescriptor
val source = (packageFragment as? LazyJavaPackageFragment)?.source as? KotlinJvmBinaryPackageSourceElement val source = (packageFragment as? LazyJavaPackageFragment)?.source as? KotlinJvmBinaryPackageSourceElement
(source?.getContainingBinaryClass(callableDescriptor) as? ReflectKotlinClass)?.klass == jClass (source?.getContainingBinaryClass(callableDescriptor) as? ReflectKotlinClass)?.klass == jClass
}.toList() }
} }
} }