Fix jvmTarget default divergence between KGP & IDE after change to 1.8
This commit is contained in:
@@ -29,21 +29,43 @@ 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.cli.common.arguments.ParseCommandLineArgumentsKt;
|
||||
import org.jetbrains.kotlin.idea.ExplicitDefaultSubstitutor;
|
||||
import org.jetbrains.kotlin.idea.ExplicitDefaultSubstitutorsKt;
|
||||
import org.jetbrains.kotlin.utils.StringsKt;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
public class ArgumentUtils {
|
||||
private ArgumentUtils() {}
|
||||
private ArgumentUtils() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> convertArgumentsToStringList(@NotNull CommonToolArguments arguments)
|
||||
throws InstantiationException, IllegalAccessException, InvocationTargetException {
|
||||
List<String> convertedArguments = convertArgumentsToStringListInternal(arguments);
|
||||
|
||||
Map<KClass<? extends CommonToolArguments>, Collection<ExplicitDefaultSubstitutor>> defaultSubstitutorsMap =
|
||||
ExplicitDefaultSubstitutorsKt.getDefaultSubstitutors();
|
||||
KClass<? extends CommonToolArguments> argumentsKClass = JvmClassMappingKt.getKotlinClass(arguments.getClass());
|
||||
Collection<ExplicitDefaultSubstitutor> defaultSubstitutors = defaultSubstitutorsMap.get(argumentsKClass);
|
||||
if (defaultSubstitutors != null) {
|
||||
for (ExplicitDefaultSubstitutor substitutor : defaultSubstitutors) {
|
||||
if (substitutor.isSubstitutable(convertedArguments)) convertedArguments.addAll(substitutor.getNewSubstitution());
|
||||
}
|
||||
}
|
||||
return convertedArguments;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> convertArgumentsToStringListNoDefaults(@NotNull CommonToolArguments arguments)
|
||||
throws InstantiationException, IllegalAccessException, InvocationTargetException {
|
||||
return convertArgumentsToStringListInternal(arguments);
|
||||
}
|
||||
|
||||
private static List<String> convertArgumentsToStringListInternal(@NotNull CommonToolArguments arguments)
|
||||
throws InstantiationException, IllegalAccessException, InvocationTargetException {
|
||||
List<String> result = new ArrayList<>();
|
||||
Class<? extends CommonToolArguments> argumentsClass = arguments.getClass();
|
||||
convertArgumentsToStringList(arguments, argumentsClass.newInstance(), JvmClassMappingKt.getKotlinClass(argumentsClass), result);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.arguments.Argument
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
|
||||
import org.jetbrains.kotlin.config.JvmTarget
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
|
||||
val defaultSubstitutors: Map<KClass<out CommonToolArguments>, Collection<ExplicitDefaultSubstitutor>> =
|
||||
mapOf(K2JVMCompilerArguments::class to listOf(JvmTargetDefaultSubstitutor))
|
||||
|
||||
sealed class ExplicitDefaultSubstitutor {
|
||||
abstract val substitutedProperty: KProperty1<out CommonToolArguments, String?>
|
||||
abstract val oldSubstitution: List<String>
|
||||
abstract val newSubstitution: List<String>
|
||||
abstract fun isSubstitutable(args: List<String>): Boolean
|
||||
|
||||
protected val argument: Argument by lazy {
|
||||
substitutedProperty.findAnnotation() ?: error("Property \"${substitutedProperty.name}\" has no Argument annotation")
|
||||
}
|
||||
}
|
||||
|
||||
object JvmTargetDefaultSubstitutor : ExplicitDefaultSubstitutor() {
|
||||
override val substitutedProperty
|
||||
get() = K2JVMCompilerArguments::jvmTarget
|
||||
private val oldDefault: String
|
||||
get() = JvmTarget.JVM_1_6.description
|
||||
private val newDefault: String
|
||||
get() = JvmTarget.JVM_1_8.description
|
||||
|
||||
private fun prepareSubstitution(default: String): List<String> = listOf(argument.value, default)
|
||||
|
||||
override val oldSubstitution: List<String>
|
||||
get() = prepareSubstitution(oldDefault)
|
||||
override val newSubstitution: List<String>
|
||||
get() = prepareSubstitution(newDefault)
|
||||
|
||||
override fun isSubstitutable(args: List<String>): Boolean = argument.value !in args
|
||||
}
|
||||
Reference in New Issue
Block a user