Skip to content

Commit 10633d7

Browse files
committed
Fixed to retain only necessary functions
1 parent 9a54173 commit 10633d7

File tree

1 file changed

+9
-2
lines changed
  • src/main/kotlin/io/github/projectmapk/jackson/module/kogera/jmClass

1 file changed

+9
-2
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/jmClass/JmClass.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.projectmapk.jackson.module.kogera.jmClass
22

3+
import io.github.projectmapk.jackson.module.kogera.reconstructClassOrNull
34
import io.github.projectmapk.jackson.module.kogera.toDescBuilder
45
import io.github.projectmapk.jackson.module.kogera.toKmClass
56
import 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

Comments
 (0)