Refactor compiler arguments to configuration conversion:

extract into independent functions, rearrange logic
This commit is contained in:
Ilya Chernikov
2019-01-08 18:00:45 +01:00
parent 298aaf999e
commit 04b04ea0ee
11 changed files with 431 additions and 333 deletions
@@ -331,7 +331,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
}
}
fun configureLanguageVersionSettings(collector: MessageCollector): LanguageVersionSettings {
fun toLanguageVersionSettings(collector: MessageCollector): LanguageVersionSettings {
// If only "-api-version" is specified, language version is assumed to be the latest stable
val languageVersion = parseVersion(collector, languageVersion, "language") ?: LanguageVersion.LATEST_STABLE
@@ -40,25 +40,25 @@ fun <From : Any, To : From> mergeBeans(from: From, to: To): To {
@Suppress("UNCHECKED_CAST")
fun <From : Any, To : Any> copyInheritedFields(from: From, to: To) =
copyProperties(from, to, true, collectProperties(from::class as KClass<From>, true))
copyProperties(from, to, true, collectProperties(from::class as KClass<From>, true))
@Suppress("UNCHECKED_CAST")
fun <From : Any, To : Any> copyFieldsSatisfying(from: From, to: To, predicate: (KProperty1<From, Any?>) -> Boolean) =
copyProperties(from, to, true, collectProperties(from::class as KClass<From>, false).filter(predicate))
copyProperties(from, to, true, collectProperties(from::class as KClass<From>, false).filter(predicate))
private fun <From : Any, To : Any> copyProperties(
from: From,
to: To,
deepCopyWhenNeeded: Boolean,
propertiesToCopy: List<KProperty1<From, Any?>>,
filter: ((KProperty1<From, Any?>, Any?) -> Boolean)? = null
from: From,
to: To,
deepCopyWhenNeeded: Boolean,
propertiesToCopy: List<KProperty1<From, Any?>>,
filter: ((KProperty1<From, Any?>, Any?) -> Boolean)? = null
): To {
if (from == to) return to
for (fromProperty in propertiesToCopy) {
@Suppress("UNCHECKED_CAST")
val toProperty = to::class.memberProperties.firstOrNull { it.name == fromProperty.name } as? KMutableProperty1<To, Any?>
?: continue
?: continue
val fromValue = fromProperty.get(from)
if (filter != null && !filter(fromProperty, fromValue)) continue
toProperty.set(to, if (deepCopyWhenNeeded) fromValue?.copyValueIfNeeded() else fromValue)
@@ -69,14 +69,14 @@ private fun <From : Any, To : Any> copyProperties(
private fun Any.copyValueIfNeeded(): Any {
@Suppress("UNCHECKED_CAST")
return when (this) {
is ByteArray -> Arrays.copyOf(this, size)
is CharArray -> Arrays.copyOf(this, size)
is ShortArray -> Arrays.copyOf(this, size)
is IntArray -> Arrays.copyOf(this, size)
is LongArray -> Arrays.copyOf(this, size)
is FloatArray -> Arrays.copyOf(this, size)
is DoubleArray -> Arrays.copyOf(this, size)
is BooleanArray -> Arrays.copyOf(this, size)
is ByteArray -> this.copyOf(size)
is CharArray -> this.copyOf(size)
is ShortArray -> this.copyOf(size)
is IntArray -> this.copyOf(size)
is LongArray -> this.copyOf(size)
is FloatArray -> this.copyOf(size)
is DoubleArray -> this.copyOf(size)
is BooleanArray -> this.copyOf(size)
is Array<*> -> java.lang.reflect.Array.newInstance(this::class.java.componentType, size).apply {
this as Array<Any?>
@@ -100,8 +100,8 @@ fun <T : Any> collectProperties(kClass: KClass<T>, inheritedOnly: Boolean): List
if (inheritedOnly) {
properties.removeAll(kClass.declaredMemberProperties)
}
return properties.filter {
it.visibility == KVisibility.PUBLIC && (it.annotations.firstOrNull { it is Transient } as Transient?) == null
return properties.filter { property ->
property.visibility == KVisibility.PUBLIC && (property.annotations.firstOrNull { it is Transient } as Transient?) == null
}
}
@@ -109,4 +109,5 @@ fun CommonCompilerArguments.setApiVersionToLanguageVersionIfNeeded() {
if (languageVersion != null && VersionComparatorUtil.compare(languageVersion, apiVersion) < 0) {
apiVersion = languageVersion
}
}
}