Gradle: Implement new MPP model import
This commit is contained in:
committed by
Sergey Igushkin
parent
caec846330
commit
ecf607d4fa
+8
-4
@@ -23,12 +23,14 @@ import kotlin.reflect.KMutableProperty1
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.KVisibility
|
||||
import kotlin.reflect.full.declaredMemberProperties
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
import kotlin.reflect.full.memberProperties
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> copyBean(bean: T) =
|
||||
copyProperties(bean, bean::class.java.newInstance()!!, true, collectProperties(bean::class as KClass<T>, false))
|
||||
fun <T : Any> copyBean(bean: T) = copyBeanTo(bean, bean::class.java.newInstance()!!)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T : Any> copyBeanTo(from: T, to: T, filter: ((KProperty1<T, Any?>, Any?) -> Boolean)? = null) =
|
||||
copyProperties(from, to, true, collectProperties(from::class as KClass<T>, false), filter)
|
||||
|
||||
fun <From : Any, To : From> mergeBeans(from: From, to: To): To {
|
||||
// TODO: rewrite when updated version of com.intellij.util.xmlb is available on TeamCity
|
||||
@@ -48,7 +50,8 @@ private fun <From : Any, To : Any> copyProperties(
|
||||
from: From,
|
||||
to: To,
|
||||
deepCopyWhenNeeded: Boolean,
|
||||
propertiesToCopy: List<KProperty1<From, Any?>>
|
||||
propertiesToCopy: List<KProperty1<From, Any?>>,
|
||||
filter: ((KProperty1<From, Any?>, Any?) -> Boolean)? = null
|
||||
): To {
|
||||
if (from == to) return to
|
||||
|
||||
@@ -57,6 +60,7 @@ private fun <From : Any, To : Any> copyProperties(
|
||||
val toProperty = to::class.memberProperties.firstOrNull { it.name == fromProperty.name } as? KMutableProperty1<To, Any?>
|
||||
?: continue
|
||||
val fromValue = fromProperty.get(from)
|
||||
if (filter != null && !filter(fromProperty, fromValue)) continue
|
||||
toProperty.set(to, if (deepCopyWhenNeeded) fromValue?.copyValueIfNeeded() else fromValue)
|
||||
}
|
||||
return to
|
||||
|
||||
Reference in New Issue
Block a user