From 9ef38caf0cb4ccb307a013a6994ac5a661026558 Mon Sep 17 00:00:00 2001 From: Eitan Liu Date: Wed, 19 Jul 2023 05:37:18 +0800 Subject: [PATCH] fix PropertyGraph create --- .../dart/mappable/extensions/PropertyGraphExt.kt | 11 +++++++---- src/main/resources/META-INF/plugin.xml | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/eitanliu/dart/mappable/extensions/PropertyGraphExt.kt b/src/main/kotlin/com/eitanliu/dart/mappable/extensions/PropertyGraphExt.kt index 842d3e9..5a788c5 100644 --- a/src/main/kotlin/com/eitanliu/dart/mappable/extensions/PropertyGraphExt.kt +++ b/src/main/kotlin/com/eitanliu/dart/mappable/extensions/PropertyGraphExt.kt @@ -29,7 +29,10 @@ inline fun KMutableProperty0.toGraphProperty( } } -fun createPropertyGraph() = PropertyGraph::class.java.createInstance { +fun createPropertyGraph() = PropertyGraph::class.java.createInstance({ sequence -> + sequence.sortedByDescending { it.parameterCount } + .first { it.parameterCount <= 2 } +}) { when (it.parameterCount) { 2 -> arrayOf(null, true) else -> null @@ -38,11 +41,11 @@ fun createPropertyGraph() = PropertyGraph::class.java.createInstance { @Suppress("UNCHECKED_CAST") fun Class.createInstance( - predicate: ((Constructor<*>) -> Boolean)? = null, + predicate: ((sequence: Sequence>) -> Constructor<*>?)? = null, params: ((Constructor<*>) -> Array<*>?)? = null, ): T = run { - val constructor = constructors.asSequence() - .first(predicate ?: { true }) + val sequence = constructors.asSequence() + val constructor = predicate?.invoke(sequence) ?: sequence.first() val args = params?.invoke(constructor) ?: arrayOfNulls(constructors[0].parameterCount) constructor.newInstance(*args) as T } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index e03e491..365fd04 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -52,7 +52,7 @@ - +