All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.reflect.jvm.internal.KProperty1Impl.kt Maven / Gradle / Ivy

/*
 * 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.PropertyDescriptor
import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KProperty1

internal open class KProperty1Impl : KProperty1, KPropertyImpl {
    constructor(container: KDeclarationContainerImpl, name: String, signature: String, boundReceiver: Any?) : super(
        container, name, signature, boundReceiver
    )

    constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor)

    private val _getter = ReflectProperties.lazy { Getter(this) }

    override val getter: Getter get() = _getter()

    override fun get(receiver: T): V = getter.call(receiver)

    private val delegateField = lazy(PUBLICATION) { computeDelegateField() }

    override fun getDelegate(receiver: T): Any? = getDelegate(delegateField.value, receiver)

    override fun invoke(receiver: T): V = get(receiver)

    class Getter(override val property: KProperty1Impl) : KPropertyImpl.Getter(), KProperty1.Getter {
        override fun invoke(receiver: T): V = property.get(receiver)
    }
}

internal class KMutableProperty1Impl : KProperty1Impl, KMutableProperty1 {
    constructor(container: KDeclarationContainerImpl, name: String, signature: String, boundReceiver: Any?) : super(
        container, name, signature, boundReceiver
    )

    constructor(container: KDeclarationContainerImpl, descriptor: PropertyDescriptor) : super(container, descriptor)

    private val _setter = ReflectProperties.lazy { Setter(this) }

    override val setter: Setter get() = _setter()

    override fun set(receiver: T, value: V) = setter.call(receiver, value)

    class Setter(override val property: KMutableProperty1Impl) : KPropertyImpl.Setter(), KMutableProperty1.Setter {
        override fun invoke(receiver: T, value: V): Unit = property.set(receiver, value)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy