Skip to content

Commit 1832684

Browse files
committed
Fix to use kotlinx.metadata
1 parent 01c8ac9 commit 1832684

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector
1212
import kotlinx.metadata.jvm.fieldSignature
1313
import kotlinx.metadata.jvm.getterSignature
1414
import kotlinx.metadata.jvm.setterSignature
15+
import kotlinx.metadata.jvm.signature
1516
import java.lang.reflect.Constructor
1617
import java.lang.reflect.Method
1718
import kotlin.reflect.KClass
1819
import kotlin.reflect.KFunction
19-
import kotlin.reflect.KParameter
2020
import kotlin.reflect.full.companionObject
2121
import kotlin.reflect.full.declaredFunctions
2222
import kotlin.reflect.full.hasAnnotation
2323
import kotlin.reflect.full.memberProperties
2424
import kotlin.reflect.full.primaryConstructor
25-
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
2625
import kotlin.reflect.jvm.javaType
27-
import kotlin.reflect.jvm.kotlinFunction
2826

2927
internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val cache: ReflectionCache, val ignoredClassesForImplyingJsonCreator: Set<KClass<*>>) : NopAnnotationIntrospector() {
3028
// since 2.4
@@ -110,37 +108,27 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
110108

111109
@Suppress("UNCHECKED_CAST")
112110
private fun findKotlinParameterName(param: AnnotatedParameter): String? {
113-
return if (param.declaringClass.isKotlinClass()) {
114-
val member = param.owner.member
115-
if (member is Constructor<*>) {
116-
val ctor = (member as Constructor<Any>)
117-
val ctorParmCount = ctor.parameterTypes.size
118-
val ktorParmCount = try { ctor.kotlinFunction?.parameters?.size ?: 0 } catch (ex: KotlinReflectionInternalError) { 0 } catch (ex: UnsupportedOperationException) { 0 }
119-
if (ktorParmCount > 0 && ktorParmCount == ctorParmCount) {
120-
ctor.kotlinFunction?.parameters?.get(param.index)?.name
121-
} else {
122-
null
111+
val declaringClass = param.declaringClass
112+
113+
return declaringClass.toKmClass()?.let { kmClass ->
114+
when (val member = param.owner.member) {
115+
is Constructor<*> -> {
116+
val signature = member.toSignature()
117+
118+
kmClass.constructors.find { it.signature?.desc == signature.desc }
119+
?.let { it.valueParameters[param.index].name }
123120
}
124-
} else if (member is Method) {
125-
try {
126-
val temp = member.kotlinFunction
127-
128-
val firstParamKind = temp?.parameters?.firstOrNull()?.kind
129-
val idx = if (firstParamKind != KParameter.Kind.VALUE) param.index + 1 else param.index
130-
val parmCount = temp?.parameters?.size ?: 0
131-
if (parmCount > idx) {
132-
temp?.parameters?.get(idx)?.name
133-
} else {
134-
null
135-
}
136-
} catch (ex: KotlinReflectionInternalError) {
137-
null
121+
is Method -> {
122+
val companionKmClass = declaringClass.getDeclaredField(kmClass.companionObject!!)
123+
.type
124+
.toKmClass()!!
125+
val signature = member.toSignature()
126+
127+
companionKmClass.functions.find { it.signature == signature }
128+
?.let { it.valueParameters[param.index].name }
138129
}
139-
} else {
140-
null
130+
else -> null
141131
}
142-
} else {
143-
null
144132
}
145133
}
146134
}

0 commit comments

Comments
 (0)