@@ -19,12 +19,10 @@ import kotlinx.metadata.KmProperty
19
19
import kotlinx.metadata.jvm.fieldSignature
20
20
import kotlinx.metadata.jvm.getterSignature
21
21
import kotlinx.metadata.jvm.setterSignature
22
+ import kotlinx.metadata.jvm.signature
22
23
import java.lang.reflect.AccessibleObject
23
24
import java.lang.reflect.Constructor
24
25
import java.lang.reflect.Method
25
- import kotlin.reflect.KFunction
26
- import kotlin.reflect.jvm.javaType
27
- import kotlin.reflect.jvm.kotlinFunction
28
26
29
27
internal class KotlinAnnotationIntrospector (
30
28
private val context : Module .SetupContext ,
@@ -150,36 +148,36 @@ internal class KotlinAnnotationIntrospector(
150
148
}
151
149
152
150
private fun AnnotatedParameter.hasRequiredMarker (): Boolean? {
153
- val member = this .member
154
151
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
155
167
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
+ }
160
179
}
161
180
162
181
return requiredAnnotationOrNullability(byAnnotation, byNullability)
163
182
}
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
- }
185
183
}
0 commit comments