/* * Copyright 2016 the original author or authors. * * 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 org.gradle.kotlin.dsl import org.gradle.api.NamedDomainObjectCollection import org.gradle.api.NamedDomainObjectProvider import org.gradle.api.UnknownDomainObjectException import org.gradle.kotlin.dsl.support.illegalElementType import kotlin.reflect.KClass import kotlin.reflect.KProperty import kotlin.reflect.full.safeCast /** * Returns a collection containing the objects in this collection of the given type. The * returned collection is live, so that when matching objects are later added to this * collection, they are also visible in the filtered collection. * * @param S The type of objects to find. * @return The matching objects. Returns an empty collection if there are no such objects * in this collection. * @see [NamedDomainObjectCollection.withType] */ inline fun NamedDomainObjectCollection.withType(): NamedDomainObjectCollection = withType(S::class.java) /** * Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property. * * `tasks { val jar by existing }` * * @param T the domain object type * @param C the concrete container type */ inline val > C.existing: ExistingDomainObjectDelegateProvider get() = ExistingDomainObjectDelegateProvider.of(this) /** * Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property. * * `tasks { val jar by existing { ... } }` * * @param T the domain object type * @param C the concrete container type * @param action the configuration action */ fun > C.existing(action: T.() -> Unit): ExistingDomainObjectDelegateProviderWithAction = ExistingDomainObjectDelegateProviderWithAction.of(this, action) /** * Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property. * * `tasks { val jar by existing(Jar::class) }` * * @param T the domain object type * @param C the concrete container type * @param type the domain object type */ fun , U : T> C.existing(type: KClass): ExistingDomainObjectDelegateProviderWithType = ExistingDomainObjectDelegateProviderWithType.of(this, type) /** * Idiomatic way of referring to the provider of a well-known element of a collection via a delegate property. * * `tasks { val jar by existing(Jar::class) { ... } }` * * @param T the domain object type * @param C the concrete container type * @param type the domain object type * @param action the configuration action */ fun , U : T> C.existing(type: KClass, action: U.() -> Unit): ExistingDomainObjectDelegateProviderWithTypeAndAction = ExistingDomainObjectDelegateProviderWithTypeAndAction.of(this, type, action) /** * Holds the delegate provider for the `existing` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class ExistingDomainObjectDelegateProvider private constructor( internal val delegateProvider: T ) { companion object { fun of(delegateProvider: T) = ExistingDomainObjectDelegateProvider(delegateProvider) } } /** * Holds the delegate provider for the `existing` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class ExistingDomainObjectDelegateProviderWithAction private constructor( internal val delegateProvider: C, internal val action: T.() -> Unit ) { companion object { fun of(delegateProvider: C, action: T.() -> Unit) = ExistingDomainObjectDelegateProviderWithAction(delegateProvider, action) } } /** * Holds the delegate provider and expected element type for the `existing` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class ExistingDomainObjectDelegateProviderWithType private constructor( internal val delegateProvider: T, internal val type: KClass ) { companion object { fun of(delegateProvider: T, type: KClass) = ExistingDomainObjectDelegateProviderWithType(delegateProvider, type) } } /** * Holds the delegate provider and expected element type for the `existing` property delegate with * the purpose of providing specialized implementations for the `provideDelegate` operator * based on the static type of the provider. */ class ExistingDomainObjectDelegateProviderWithTypeAndAction private constructor( internal val delegateProvider: T, internal val type: KClass, internal val action: U.() -> Unit ) { companion object { fun of(delegateProvider: T, type: KClass, action: U.() -> Unit) = ExistingDomainObjectDelegateProviderWithTypeAndAction(delegateProvider, type, action) } } /** * Provides access to the [NamedDomainObjectProvider] for the element of the given * property name from the container via a delegated property. */ operator fun > ExistingDomainObjectDelegateProvider.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.named(property.name) ) /** * Provides access to the [NamedDomainObjectProvider] for the element of the given * property name from the container via a delegated property. */ operator fun > ExistingDomainObjectDelegateProviderWithAction.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.named(property.name).apply { configure(action) } ) /** * Provides access to the [NamedDomainObjectProvider] for the element of the given * property name from the container via a delegated property. */ operator fun , U : T> ExistingDomainObjectDelegateProviderWithType.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.named(property.name, type) ) /** * Provides access to the [NamedDomainObjectProvider] for the element of the given * property name from the container via a delegated property. */ operator fun , U : T> ExistingDomainObjectDelegateProviderWithTypeAndAction.provideDelegate( receiver: Any?, property: KProperty<*> ) = ExistingDomainObjectDelegate.of( delegateProvider.named(property.name, type, action) ) /** * Holds a property delegate with the purpose of providing specialized implementations for the * `getValue` operator based on the static type of the delegate. */ class ExistingDomainObjectDelegate private constructor( internal val delegate: T ) { companion object { fun of(delegate: T) = ExistingDomainObjectDelegate(delegate) } } /** * Gets the delegate value. */ operator fun ExistingDomainObjectDelegate.getValue(receiver: Any?, property: KProperty<*>): T = delegate /** * Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @see [NamedDomainObjectCollection.named] */ @Suppress("extension_shadowed_by_member") inline fun NamedDomainObjectCollection.named(name: String): NamedDomainObjectProvider = named(name, T::class) /** * Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @see [NamedDomainObjectCollection.named] */ @Suppress("unchecked_cast") fun NamedDomainObjectCollection.named(name: String, type: KClass): NamedDomainObjectProvider = (this as NamedDomainObjectCollection).named(name, type.java) /** * Configures an object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @see [NamedDomainObjectCollection.named] * @see [NamedDomainObjectProvider.configure] */ @Suppress("unchecked_cast") inline fun NamedDomainObjectCollection.named(name: String, noinline configuration: T.() -> Unit): NamedDomainObjectProvider = (this as NamedDomainObjectCollection).named(name, T::class.java, configuration) /** * Configures an object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @see [NamedDomainObjectCollection.named] * @see [NamedDomainObjectProvider.configure] */ @Suppress("unchecked_cast") fun NamedDomainObjectCollection.named(name: String, type: KClass, configuration: T.() -> Unit): NamedDomainObjectProvider = (this as NamedDomainObjectCollection).named(name, type.java, configuration) /** * Locates an object by name and casts it to the expected type [T]. * * If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown. * If the object is found but cannot be cast to the expected type [T], [IllegalArgumentException] is thrown. * * @param name object name * @return the object, never null * @throws [UnknownDomainObjectException] When the given object is not found. * @throws [IllegalArgumentException] When the given object cannot be cast to the expected type. */ @Suppress("extension_shadowed_by_member") inline fun NamedDomainObjectCollection.getByName(name: String) = getByName(name).let { it as? T ?: throw illegalElementType(this, name, T::class, it::class) } /** * Locates an object by name and casts it to the expected [type]. * * If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown. * If the object is found but cannot be cast to the expected [type], [IllegalArgumentException] is thrown. * * @param name object name * @param type expected type * @return the object, never null * @throws [UnknownDomainObjectException] When the given object is not found. * @throws [IllegalArgumentException] When the given object cannot be cast to the expected type. */ fun NamedDomainObjectCollection.getByName(name: String, type: KClass): T = getByName(name).let { type.safeCast(it) ?: throw illegalElementType(this, name, type, it::class) } /** * Locates an object by name and casts it to the expected [type] then configures it. * * If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown. * If the object is found but cannot be cast to the expected [type], [IllegalArgumentException] is thrown. * * @param name object name * @param configure configuration action to apply to the object before returning it * @return the object, never null * @throws [UnknownDomainObjectException] When the given object is not found. * @throws [IllegalArgumentException] When the given object cannot be cast to the expected type. */ fun NamedDomainObjectCollection.getByName(name: String, type: KClass, configure: T.() -> Unit): T = getByName(name, type).also(configure) /** * Locates an object by name and casts it to the expected type [T] then configures it. * * If an object with the given [name] is not found, [UnknownDomainObjectException] is thrown. * If the object is found but cannot be cast to the expected type [T], [IllegalArgumentException] is thrown. * * @param name object name * @param configure configuration action to apply to the object before returning it * @return the object, never null * @throws [UnknownDomainObjectException] When the given object is not found. * @throws [IllegalArgumentException] When the given object cannot be cast to the expected type. */ inline fun NamedDomainObjectCollection.getByName(name: String, configure: T.() -> Unit) = getByName(name).also(configure) /** * Idiomatic way of referring to an existing element in a collection * via a delegate property. * * `tasks { val jar by getting }` */ inline val > U.getting get() = NamedDomainObjectCollectionDelegateProvider.of(this) /** * Idiomatic way of referring and configuring an existing element in a collection * via a delegate property. * * `tasks { val jar by getting { group = "My" } }` */ fun > U.getting(configuration: T.() -> Unit) = NamedDomainObjectCollectionDelegateProvider.of(this, configuration) /** * Enables typed access to container elements via delegated properties. */ class NamedDomainObjectCollectionDelegateProvider private constructor( internal val collection: NamedDomainObjectCollection, internal val configuration: (T.() -> Unit)? ) { companion object { fun of( collection: NamedDomainObjectCollection, configuration: (T.() -> Unit)? = null ) = NamedDomainObjectCollectionDelegateProvider(collection, configuration) } operator fun provideDelegate(thisRef: Any?, property: kotlin.reflect.KProperty<*>) = ExistingDomainObjectDelegate.of( when (configuration) { null -> collection.getByName(property.name) else -> collection.getByName(property.name, configuration) } ) } /** * Locates an object by name, failing if there is no such object. * * @param name The object name * @return The object with the given name. * @throws [UnknownDomainObjectException] when there is no such object in this collection. * * @see [NamedDomainObjectCollection.getByName] */ operator fun NamedDomainObjectCollection.get(name: String): T = getByName(name) /** * Allows a [NamedDomainObjectCollection] to be used as a property delegate. * * @see [NamedDomainObjectCollection.named] */ operator fun NamedDomainObjectCollection.provideDelegate(thisRef: Any?, property: KProperty<*>): NamedDomainObjectProvider = named(property.name) /** * Allows a [NamedDomainObjectProvider] to be used as a property delegate. * * @see [NamedDomainObjectProvider.get] */ @Suppress("nothing_to_inline", "unchecked_cast") inline operator fun NamedDomainObjectProvider.getValue(thisRef: Any?, property: KProperty<*>): U = get().let { it as? U ?: throw illegalElementType(this, property.name, U::class, it::class) } /** * Required due to [KT-25810](https://youtrack.jetbrains.com/issue/KT-25810). */ operator fun T.provideDelegate(receiver: Any?, property: KProperty<*>): T = this