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

name.remal.gradle_plugins.dsl.extensions.kotlin.Any.kt Maven / Gradle / Ivy

package name.remal.gradle_plugins.dsl.extensions

import groovy.lang.Closure
import name.remal.orNull
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.repositories.ArtifactRepository
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.internal.HasConvention
import org.gradle.api.plugins.Convention
import org.gradle.api.provider.Provider
import java.lang.reflect.Array
import java.util.*
import java.util.concurrent.Callable
import java.util.concurrent.Future
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference
import java.util.function.*

val Any.convention: Convention
    get() = when (this) {
        is Project -> this.convention
        is Task -> this.convention
        else -> (this as HasConvention).convention
    }


fun Any?.unwrapProviders(): Any? = when (this) {
    is Optional<*> -> this.orNull.unwrapProviders()
    is Provider<*> -> this.orNull.unwrapProviders()
    is Callable<*> -> this.call().unwrapProviders()
    is Supplier<*> -> this.get().unwrapProviders()
    is BooleanSupplier -> this.asBoolean.unwrapProviders()
    is IntSupplier -> this.asInt.unwrapProviders()
    is LongSupplier -> this.asLong.unwrapProviders()
    is DoubleSupplier -> this.asDouble.unwrapProviders()
    is Future<*> -> this.get().unwrapProviders()
    is AtomicBoolean -> this.get().unwrapProviders()
    is AtomicInteger -> this.get().unwrapProviders()
    is AtomicLong -> this.get().unwrapProviders()
    is AtomicReference<*> -> this.get().unwrapProviders()
    is Lazy<*> -> this.value.unwrapProviders()
    is Closure<*> -> this.call().unwrapProviders()
    else -> this
}


fun Any?.toStringSmart(): String {
    if (null == this) return "null"

    if (this.javaClass.isArray) {
        return buildString {
            append("[")
            (0 until Array.getLength(this@toStringSmart)).joinTo(this, ", ", transform = { Array.get(this@toStringSmart, it).toStringSmart() })
            append("]")
        }
    }

    if (this is Iterable<*>) {
        return buildString {
            append("[")
            [email protected](this, ", ", transform = { it.toStringSmart() })
            append("]")
        }
    }

    val toStringDeclaringClass = this.javaClass.getMethod("toString").declaringClass
    if (Any::class.java == toStringDeclaringClass || toStringDeclaringClass.isInterface) {
        when (this) {
            is MavenArtifactRepository -> return "$name($url)"
            is ArtifactRepository -> return name
        }
    }

    return toString()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy