From d3abc83b4ca9a49c1807c3897ca5225f9b236276 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 6 Sep 2016 18:19:56 +0300 Subject: [PATCH] Merge DescriptorBasedProperty into KPropertyImpl, make the latter a class --- .../jvm/internal/DescriptorBasedProperty.kt | 89 ------------------- .../reflect/jvm/internal/KProperty0Impl.kt | 2 +- .../reflect/jvm/internal/KProperty1Impl.kt | 2 +- .../reflect/jvm/internal/KProperty2Impl.kt | 2 +- .../reflect/jvm/internal/KPropertyImpl.kt | 81 ++++++++++++++--- 5 files changed, 72 insertions(+), 104 deletions(-) delete mode 100644 core/reflection.jvm/src/kotlin/reflect/jvm/internal/DescriptorBasedProperty.kt diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/DescriptorBasedProperty.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/DescriptorBasedProperty.kt deleted file mode 100644 index d09a1802391..00000000000 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/DescriptorBasedProperty.kt +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package kotlin.reflect.jvm.internal - -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.PropertyDescriptor -import org.jetbrains.kotlin.load.java.JvmAbi -import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil -import java.lang.reflect.Field -import kotlin.reflect.jvm.internal.JvmPropertySignature.* - -internal abstract class DescriptorBasedProperty protected constructor( - override val container: KDeclarationContainerImpl, - name: String, - val signature: String, - descriptorInitialValue: PropertyDescriptor? -) : KCallableImpl { - constructor(container: KDeclarationContainerImpl, name: String, signature: String) : this( - container, name, signature, null - ) - - constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : this( - container, - descriptor.name.asString(), - RuntimeTypeMapper.mapPropertySignature(descriptor).asString(), - descriptor - ) - - private val descriptor_ = ReflectProperties.lazySoft(descriptorInitialValue) { - container.findPropertyDescriptor(name, signature) - } - - override val descriptor: PropertyDescriptor get() = descriptor_() - - private val javaField_ = ReflectProperties.lazySoft { - val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor) - when (jvmSignature) { - is KotlinProperty -> { - val descriptor = jvmSignature.descriptor - JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver, jvmSignature.typeTable)?.let { - val owner = if (JvmAbi.isCompanionObjectWithBackingFieldsInOuter(descriptor.containingDeclaration)) { - container.jClass.enclosingClass - } - else descriptor.containingDeclaration.let { containingDeclaration -> - if (containingDeclaration is ClassDescriptor) containingDeclaration.toJavaClass() - else container.jClass - } - - try { - owner?.getDeclaredField(it.name) - } - catch (e: NoSuchFieldException) { - null - } - } - } - is JavaField -> jvmSignature.field - is JavaMethodProperty -> null - } - } - - @Suppress("unused") // Used in subclasses as an implementation of an irrelevant property from KPropertyImpl - val javaField: Field? get() = javaField_() - - override fun equals(other: Any?): Boolean { - val that = other.asKPropertyImpl() ?: return false - return container == that.container && name == that.name && signature == that.signature - } - - override fun hashCode(): Int = - (container.hashCode() * 31 + name.hashCode()) * 31 + signature.hashCode() - - override fun toString(): String = - ReflectionObjectRenderer.renderProperty(descriptor) -} diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty0Impl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty0Impl.kt index 347ca934532..db06741b1c6 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty0Impl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty0Impl.kt @@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor import kotlin.reflect.KMutableProperty0 import kotlin.reflect.KProperty0 -internal open class KProperty0Impl : DescriptorBasedProperty, KProperty0, KPropertyImpl { +internal open class KProperty0Impl : KProperty0, KPropertyImpl { constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor) constructor(container: KDeclarationContainerImpl, name: String, signature: String) : super(container, name, signature) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty1Impl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty1Impl.kt index 4007bc060d6..f77551b9d84 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty1Impl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty1Impl.kt @@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor import kotlin.reflect.KMutableProperty1 import kotlin.reflect.KProperty1 -internal open class KProperty1Impl : DescriptorBasedProperty, KProperty1, KPropertyImpl { +internal open class KProperty1Impl : KProperty1, KPropertyImpl { constructor(container: KDeclarationContainerImpl, name: String, signature: String) : super(container, name, signature) constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty2Impl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty2Impl.kt index d04f1555fa5..e86ce9d65f1 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty2Impl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KProperty2Impl.kt @@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor import kotlin.reflect.KMutableProperty2 import kotlin.reflect.KProperty2 -internal open class KProperty2Impl : DescriptorBasedProperty, KProperty2, KPropertyImpl { +internal open class KProperty2Impl : KProperty2, KPropertyImpl { constructor(container: KDeclarationContainerImpl, name: String, signature: String) : super(container, name, signature) constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor) diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt index 8a20e8e1bd5..1752a79c465 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KPropertyImpl.kt @@ -18,8 +18,10 @@ package kotlin.reflect.jvm.internal import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.resolve.DescriptorFactory import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil import org.jetbrains.kotlin.types.TypeUtils import java.lang.reflect.Field import java.lang.reflect.Modifier @@ -27,15 +29,61 @@ import kotlin.reflect.KFunction import kotlin.reflect.KMutableProperty import kotlin.reflect.KProperty import kotlin.reflect.KotlinReflectionInternalError +import kotlin.reflect.jvm.internal.JvmPropertySignature.* -internal interface KPropertyImpl : KProperty, KCallableImpl { - val javaField: Field? +internal abstract class KPropertyImpl private constructor( + override val container: KDeclarationContainerImpl, + name: String, + val signature: String, + descriptorInitialValue: PropertyDescriptor? +) : KProperty, KCallableImpl { + constructor(container: KDeclarationContainerImpl, name: String, signature: String) : this( + container, name, signature, null + ) - val signature: String + constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : this( + container, + descriptor.name.asString(), + RuntimeTypeMapper.mapPropertySignature(descriptor).asString(), + descriptor + ) - override val getter: Getter + private val javaField_ = ReflectProperties.lazySoft { + val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor) + when (jvmSignature) { + is KotlinProperty -> { + val descriptor = jvmSignature.descriptor + JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver, jvmSignature.typeTable)?.let { + val owner = if (JvmAbi.isCompanionObjectWithBackingFieldsInOuter(descriptor.containingDeclaration)) { + container.jClass.enclosingClass + } + else descriptor.containingDeclaration.let { containingDeclaration -> + if (containingDeclaration is ClassDescriptor) containingDeclaration.toJavaClass() + else container.jClass + } - override val descriptor: PropertyDescriptor + try { + owner?.getDeclaredField(it.name) + } + catch (e: NoSuchFieldException) { + null + } + } + } + is JavaField -> jvmSignature.field + is JavaMethodProperty -> null + } + } + + val javaField: Field? get() = javaField_() + + override abstract val getter: Getter + + private val descriptor_ = ReflectProperties.lazySoft(descriptorInitialValue) { + container.findPropertyDescriptor(name, signature) + } + + override val descriptor: PropertyDescriptor get() = descriptor_() override val name: String get() = descriptor.name.asString() @@ -43,11 +91,20 @@ internal interface KPropertyImpl : KProperty, KCallableImpl { override val defaultCaller: FunctionCaller<*>? get() = getter.defaultCaller - override val isLateinit: Boolean - get() = descriptor.isLateInit + override val isLateinit: Boolean get() = descriptor.isLateInit - override val isConst: Boolean - get() = descriptor.isConst + override val isConst: Boolean get() = descriptor.isConst + + override fun equals(other: Any?): Boolean { + val that = other.asKPropertyImpl() ?: return false + return container == that.container && name == that.name && signature == that.signature + } + + override fun hashCode(): Int = + (container.hashCode() * 31 + name.hashCode()) * 31 + signature.hashCode() + + override fun toString(): String = + ReflectionObjectRenderer.renderProperty(descriptor) abstract class Accessor : KCallableImpl, KProperty.Accessor, KFunction { @@ -129,7 +186,7 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool val jvmSignature = RuntimeTypeMapper.mapPropertySignature(property.descriptor) return when (jvmSignature) { - is JvmPropertySignature.KotlinProperty -> { + is KotlinProperty -> { val accessorSignature = jvmSignature.signature.run { when { isGetter -> if (hasGetter()) getter else null @@ -152,10 +209,10 @@ private fun KPropertyImpl.Accessor<*, *>.computeCallerForAccessor(isGetter: Bool else -> FunctionCaller.StaticMethod(accessor) } } - is JvmPropertySignature.JavaField -> { + is JavaField -> { computeFieldCaller(jvmSignature.field) } - is JvmPropertySignature.JavaMethodProperty -> { + is JavaMethodProperty -> { val method = if (isGetter) jvmSignature.getterMethod else jvmSignature.setterMethod ?: throw KotlinReflectionInternalError(