org.jetbrains.kotlin.arguments.CompilerArgumentsContentProspector.kt Maven / Gradle / Ivy
// 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
import kotlin.reflect.jvm.javaField
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.javaField?.getAnnotation(Argument::class.java) != null }
}
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
}