Skip to content

Commit 01c8ac9

Browse files
committed
Remove reflect
1 parent 579d92c commit 01c8ac9

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import kotlinx.metadata.KmProperty
1919
import kotlinx.metadata.jvm.fieldSignature
2020
import kotlinx.metadata.jvm.getterSignature
2121
import kotlinx.metadata.jvm.setterSignature
22+
import kotlinx.metadata.jvm.signature
2223
import java.lang.reflect.AccessibleObject
2324
import java.lang.reflect.Constructor
2425
import java.lang.reflect.Method
25-
import kotlin.reflect.KFunction
26-
import kotlin.reflect.jvm.javaType
27-
import kotlin.reflect.jvm.kotlinFunction
2826

2927
internal 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

Comments
 (0)