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

org.jetbrains.kotlin.arguments.CompilerArgumentsContentProspector.kt Maven / Gradle / Ivy

There is a newer version: 2.0.20-Beta2
Show newest version
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.arguments

import org.jetbrains.kotlin.cli.common.arguments.Argument
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.cli.common.arguments.InternalArgument
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.KType
import kotlin.reflect.full.memberProperties

object CompilerArgumentsContentProspector {
    private val argumentPropertiesCache: MutableMap, Collection>> =
        mutableMapOf()

    private val flagArgumentPropertiesCache: MutableMap, Collection>> =
        mutableMapOf()

    private val stringArgumentPropertiesCache: MutableMap, Collection>> =
        mutableMapOf()

    private val arrayArgumentPropertiesCache: MutableMap, Collection?>>> =
        mutableMapOf()

    private fun getCompilerArgumentsProperties(kClass: KClass) = argumentPropertiesCache.getOrPut(kClass) {
        kClass.memberProperties.filter { prop -> prop.annotations.any { it is Argument } }
    }

    private inline fun  Collection>.filterByReturnType(predicate: (KType?) -> Boolean) =
        filter { predicate(it.returnType) }.mapNotNull { it.safeAs>() }

    fun getFlagCompilerArgumentProperties(kClass: KClass): Collection> =
        flagArgumentPropertiesCache.getOrPut(kClass) { getCompilerArgumentsProperties(kClass).filterByReturnType { it?.classifier == Boolean::class } }

    fun getStringCompilerArgumentProperties(kClass: KClass): Collection> =
        stringArgumentPropertiesCache.getOrPut(kClass) { getCompilerArgumentsProperties(kClass).filterByReturnType { it?.classifier == String::class } }

    fun getArrayCompilerArgumentProperties(kClass: KClass): Collection?>> =
        arrayArgumentPropertiesCache.getOrPut(kClass) { getCompilerArgumentsProperties(kClass).filterByReturnType { (it?.classifier as? KClass<*>)?.java?.isArray == true } }

    val freeArgsProperty: KProperty1>
        get() = CommonToolArguments::freeArgs
    val internalArgumentsProperty: KProperty1>
        get() = CommonToolArguments::internalArguments
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy