Skip to content

Commit e6fe727

Browse files
committed
Variableize properties that are accessed many times
1 parent 075e133 commit e6fe727

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException
1414
import java.lang.reflect.TypeVariable
1515
import kotlin.reflect.KParameter
1616
import kotlin.reflect.KType
17+
import kotlin.reflect.KTypeProjection
1718
import kotlin.reflect.jvm.javaType
1819

1920
internal class KotlinValueInstantiator(
@@ -29,6 +30,8 @@ internal class KotlinValueInstantiator(
2930

3031
private fun KType.isGenericTypeVar() = javaType is TypeVariable<*>
3132

33+
private fun List<KTypeProjection>.markedNonNullAt(index: Int) = getOrNull(index)?.type?.isMarkedNullable == false
34+
3235
override fun createFromObjectWith(
3336
ctxt: DeserializationContext,
3437
props: Array<out SettableBeanProperty>,
@@ -64,14 +67,15 @@ internal class KotlinValueInstantiator(
6467
return@forEachIndexed
6568
}
6669

70+
val paramType = paramDef.type
6771
var paramVal = if (!isMissing || paramDef.isPrimitive() || jsonProp.hasInjectableValueId()) {
6872
val tempParamVal = buffer.getParameter(jsonProp)
6973
if (nullIsSameAsDefault && tempParamVal == null && paramDef.isOptional) {
7074
return@forEachIndexed
7175
}
7276
tempParamVal
7377
} else {
74-
if(paramDef.type.isMarkedNullable) {
78+
if(paramType.isMarkedNullable) {
7579
// do not try to create any object if it is nullable and the value is missing
7680
null
7781
} else {
@@ -80,46 +84,51 @@ internal class KotlinValueInstantiator(
8084
}
8185
}
8286

87+
val propType = jsonProp.type
88+
8389
if (paramVal == null) {
84-
if (jsonProp.type.requireEmptyValue()) {
90+
if (propType.requireEmptyValue()) {
8591
paramVal = NullsAsEmptyProvider(jsonProp.valueDeserializer).getNullValue(ctxt)
8692
} else {
8793
val isMissingAndRequired = isMissing && jsonProp.isRequired
8894

8995
// Since #310 reported that the calculation cost is high, isGenericTypeVar is determined last.
90-
if (isMissingAndRequired || (!paramDef.type.isMarkedNullable && !paramDef.type.isGenericTypeVar())) {
96+
if (isMissingAndRequired || (!paramType.isMarkedNullable && !paramType.isGenericTypeVar())) {
9197
throw MismatchedInputException.from(
9298
ctxt.parser,
93-
jsonProp.type,
99+
propType,
94100
"Instantiation of $valueTypeDesc value failed for JSON property ${jsonProp.name} " +
95101
"due to missing (therefore NULL) value for creator parameter ${paramDef.name} " +
96102
"which is a non-nullable type"
97103
).wrapWithPath(this.valueClass, jsonProp.name)
98104
}
99105
}
100106
} else if (strictNullChecks) {
101-
var paramType: String? = null
107+
val arguments = paramType.arguments
108+
109+
var paramTypeStr: String? = null
102110
var itemType: KType? = null
103-
if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) {
104-
paramType = "collection"
105-
itemType = paramDef.type.arguments[0].type
111+
112+
if (propType.isCollectionLikeType && arguments.markedNonNullAt(0) && (paramVal as Collection<*>).any { it == null }) {
113+
paramTypeStr = "collection"
114+
itemType = arguments[0].type
106115
}
107116

108-
if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) {
109-
paramType = "map"
110-
itemType = paramDef.type.arguments[1].type
117+
if (propType.isMapLikeType && arguments.markedNonNullAt(1) && (paramVal as Map<*, *>).any { it.value == null }) {
118+
paramTypeStr = "map"
119+
itemType = arguments[1].type
111120
}
112121

113-
if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) {
114-
paramType = "array"
115-
itemType = paramDef.type.arguments[0].type
122+
if (propType.isArrayType && arguments.markedNonNullAt(0) && (paramVal as Array<*>).any { it == null }) {
123+
paramTypeStr = "array"
124+
itemType = arguments[0].type
116125
}
117126

118-
if (paramType != null && itemType != null) {
127+
if (paramTypeStr != null && itemType != null) {
119128
throw MismatchedInputException.from(
120129
ctxt.parser,
121-
jsonProp.type,
122-
"Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
130+
propType,
131+
"Instantiation of $itemType $paramTypeStr failed for JSON property ${jsonProp.name} due to null value in a $paramTypeStr that does not allow null values"
123132
).wrapWithPath(this.valueClass, jsonProp.name)
124133
}
125134
}

0 commit comments

Comments
 (0)