@@ -19,12 +19,10 @@ import kotlinx.metadata.KmProperty
1919import kotlinx.metadata.jvm.fieldSignature
2020import kotlinx.metadata.jvm.getterSignature
2121import kotlinx.metadata.jvm.setterSignature
22+ import kotlinx.metadata.jvm.signature
2223import java.lang.reflect.AccessibleObject
2324import java.lang.reflect.Constructor
2425import java.lang.reflect.Method
25- import kotlin.reflect.KFunction
26- import kotlin.reflect.jvm.javaType
27- import kotlin.reflect.jvm.kotlinFunction
2826
2927internal class KotlinAnnotationIntrospector (
3028 private val context : Module .SetupContext ,
@@ -150,36 +148,36 @@ internal class KotlinAnnotationIntrospector(
150148 }
151149
152150 private fun AnnotatedParameter.hasRequiredMarker (): Boolean? {
153- val member = this .member
154151 val byAnnotation = this .getAnnotation(JsonProperty ::class .java)?.required
152+ val byNullability: Boolean? = member.declaringClass.toKmClass()?.let { kmClass ->
153+ when (val member = member) {
154+ is Constructor <* > -> {
155+ val signature = member.toSignature()
156+ val paramDef = kmClass.constructors.find { it.signature?.desc == signature.desc }
157+ ?.let { it.valueParameters[index] }
158+ ? : return @let null
159+
160+ paramDef to member.parameterTypes[index]
161+ }
162+ is Method -> {
163+ val signature = member.toSignature()
164+ val paramDef = kmClass.functions.find { it.signature == signature }
165+ ?.let { it.valueParameters[index] }
166+ ? : return @let null
155167
156- val byNullability = when (member) {
157- is Constructor <* > -> member.kotlinFunction?.isConstructorParameterRequired(index)
158- is Method -> member.kotlinFunction?.isMethodParameterRequired(index)
159- else -> null
168+ paramDef to member.parameterTypes[index]
169+ }
170+ else -> null
171+ }?.let { (paramDef, paramType) ->
172+ val isPrimitive = paramType.isPrimitive
173+ val isOptional = Flag .ValueParameter .DECLARES_DEFAULT_VALUE (paramDef.flags)
174+ val isMarkedNullable = Flag .Type .IS_NULLABLE (paramDef.type.flags)
175+
176+ ! isMarkedNullable && ! isOptional &&
177+ ! (isPrimitive && ! context.isEnabled(DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES ))
178+ }
160179 }
161180
162181 return requiredAnnotationOrNullability(byAnnotation, byNullability)
163182 }
164-
165- private fun KFunction <* >.isConstructorParameterRequired (index : Int ): Boolean {
166- return isParameterRequired(index)
167- }
168-
169- private fun KFunction <* >.isMethodParameterRequired (index : Int ): Boolean {
170- return isParameterRequired(index + 1 )
171- }
172-
173- private fun KFunction <* >.isParameterRequired (index : Int ): Boolean {
174- val param = parameters[index]
175- val paramType = param.type
176- val javaType = paramType.javaType
177- val isPrimitive = when (javaType) {
178- is Class <* > -> javaType.isPrimitive
179- else -> false
180- }
181-
182- return ! paramType.isMarkedNullable && ! param.isOptional &&
183- ! (isPrimitive && ! context.isEnabled(DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES ))
184- }
185183}
0 commit comments