11package io.github.projectmapk.jackson.module.kogera.jmClass
22
3+ import io.github.projectmapk.jackson.module.kogera.reconstructClassOrNull
34import io.github.projectmapk.jackson.module.kogera.toDescBuilder
45import io.github.projectmapk.jackson.module.kogera.toKmClass
56import io.github.projectmapk.jackson.module.kogera.toSignature
@@ -20,7 +21,13 @@ internal sealed interface JmClass {
2021 private val companionField: Field = declaringClass.getDeclaredField(companionObject)
2122 val type: Class <* > = companionField.type
2223 val isAccessible: Boolean = companionField.isAccessible
23- private val functions by lazy { type.toKmClass()!! .functions }
24+ private val factoryFunctions by lazy {
25+ // Since it is a user-defined factory function that is processed,
26+ // it always has arguments and the return value is the same as declaringClass.
27+ type.toKmClass()!! .functions.filter {
28+ it.valueParameters.isNotEmpty() && it.returnType.reconstructClassOrNull() == declaringClass
29+ }
30+ }
2431 val instance: Any by lazy {
2532 // To prevent the call from failing, save the initial value and then rewrite the flag.
2633 if (! companionField.isAccessible) companionField.isAccessible = true
@@ -29,7 +36,7 @@ internal sealed interface JmClass {
2936
3037 fun findFunctionByMethod (method : Method ): KmFunction ? {
3138 val signature = method.toSignature()
32- return functions .find { it.signature == signature }
39+ return factoryFunctions .find { it.signature == signature }
3340 }
3441 }
3542
0 commit comments