optimize getting hasRequiredMarker process on CorrespondingAccessor
MODIFY
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt
@@ -13,7 +13,6 @@ import java.lang.reflect.Field
import java.lang.reflect.Method
import kotlin.reflect.KFunction
import kotlin.reflect.KMutableProperty1
-import kotlin.reflect.KProperty1
import kotlin.reflect.KType
import kotlin.reflect.full.createType
import kotlin.reflect.full.declaredMemberProperties
@@ -100,17 +99,7 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
private fun AnnotatedMethod.hasRequiredMarker(): Boolean? {
// This could be a setter or a getter of a class property or
// a setter-like/getter-like method.
- val paramGetter = this.getCorrespondingGetter()
- if (paramGetter != null) {
- val byAnnotation = paramGetter.javaGetter?.isRequiredByAnnotation()
- return requiredAnnotationOrNullability(byAnnotation, paramGetter.returnType.isRequired())
- }
-
- val paramSetter = this.getCorrespondingSetter()
- if (paramSetter != null) {
- val byAnnotation = paramSetter.javaMethod?.isRequiredByAnnotation()
- return requiredAnnotationOrNullability(byAnnotation, paramSetter.isMethodParameterRequired(0))
- }
+ this.getFromCorrespondingAccessor()?.let { return it }
// Is the member method a regular method of the data class or
val method = this.member.kotlinFunction
@@ -132,21 +121,23 @@ internal class KotlinAnnotationIntrospector(private val context: Module.SetupCon
private fun KFunction<*>.isSetterLike(): Boolean =
parameters.size == 2 && returnType == Unit::class.createType()
-
- private fun AnnotatedMethod.getCorrespondingGetter(): KProperty1<out Any, Any?>? =
- member.declaringClass.kotlin.declaredMemberProperties.find {
- it.getter.javaMethod == this.member
+ private fun AnnotatedMethod.getFromCorrespondingAccessor(): Boolean? {
+ member.declaringClass.kotlin.declaredMemberProperties.forEach { kProperty1 ->
+ kProperty1.javaGetter
+ ?.takeIf { it == this.member }
+ ?.let {
+ val byAnnotation = it.isRequiredByAnnotation()
+ return requiredAnnotationOrNullability(byAnnotation, kProperty1.returnType.isRequired())
}
- @Suppress("UNCHECKED_CAST")
- private fun AnnotatedMethod.getCorrespondingSetter(): KMutableProperty1.Setter<out Any, Any?>? {
- val mutableProperty = member.declaringClass.kotlin.declaredMemberProperties.find {
- when (it) {
- is KMutableProperty1 -> it.javaSetter == this.member
- else -> false
+ (kProperty1 as? KMutableProperty1)?.javaSetter
+ ?.takeIf { it == this.member }
+ ?.let {
+ val byAnnotation = it.isRequiredByAnnotation()
+ return requiredAnnotationOrNullability(byAnnotation, kProperty1.setter.isMethodParameterRequired(0))
}
}
- return (mutableProperty as? KMutableProperty1<out Any, Any?>)?.setter
+ return null
}
private fun AnnotatedParameter.hasRequiredMarker(): Boolean? {

optimize getting hasRequiredMarker process on CorrespondingAccessor