Skip to content

Commit 685c589

Browse files
committed
Reduction of information held in value-parameter-related
1 parent 49ab9bd commit 685c589

File tree

12 files changed

+69
-59
lines changed

12 files changed

+69
-59
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/InternalCommons.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ internal fun String.reconstructClass(): Class<*> {
8383
return Class.forName(String(replaced))
8484
}
8585

86-
internal fun KmType.reconstructClassOrNull(): Class<*>? = (classifier as? KmClassifier.Class)
87-
?.let { kotlin.runCatching { it.name.reconstructClass() }.getOrNull() }
86+
internal fun KmType.reconstructClassOrNull(): Class<*>? = (classifier as? KmClassifier.Class)?.reconstructClassOrNull()
87+
internal fun KmClassifier.Class.reconstructClassOrNull(): Class<*>? =
88+
runCatching { name.reconstructClass() }.getOrNull()
8889

8990
internal fun AnnotatedElement.hasCreatorAnnotation(): Boolean = getAnnotation(JSON_CREATOR_CLASS)
9091
?.let { it.mode != JsonCreator.Mode.DISABLED }

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/annotationIntrospector/KotlinFallbackAnnotationIntrospector.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import io.github.projectmapk.jackson.module.kogera.JSON_K_UNBOX_CLASS
1616
import io.github.projectmapk.jackson.module.kogera.KOTLIN_DURATION_CLASS
1717
import io.github.projectmapk.jackson.module.kogera.ReflectionCache
1818
import io.github.projectmapk.jackson.module.kogera.isUnboxableValueClass
19-
import io.github.projectmapk.jackson.module.kogera.reconstructClassOrNull
19+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
2020
import io.github.projectmapk.jackson.module.kogera.ser.KotlinDurationValueToJavaDurationConverter
2121
import io.github.projectmapk.jackson.module.kogera.ser.KotlinToJavaDurationConverter
2222
import io.github.projectmapk.jackson.module.kogera.ser.SequenceToIteratorConverter
2323
import kotlinx.metadata.KmTypeProjection
24-
import kotlinx.metadata.KmValueParameter
2524
import kotlinx.metadata.isNullable
2625
import java.lang.reflect.Constructor
2726
import java.lang.reflect.Method
@@ -35,7 +34,7 @@ internal class KotlinFallbackAnnotationIntrospector(
3534
private val useJavaDurationConversion: Boolean,
3635
private val cache: ReflectionCache
3736
) : NopAnnotationIntrospector() {
38-
private fun findKotlinParameter(param: AnnotatedParameter): KmValueParameter? =
37+
private fun findKotlinParameter(param: AnnotatedParameter): JmValueParameter? =
3938
when (val owner = param.owner.member) {
4039
is Constructor<*> -> cache.getJmClass(param.declaringClass)?.findJmConstructor(owner)?.valueParameters
4140
is Method -> if (Modifier.isStatic(owner.modifiers)) {
@@ -48,7 +47,7 @@ internal class KotlinFallbackAnnotationIntrospector(
4847
else -> null
4948
}?.let { it[param.index] }
5049

51-
private fun findKotlinParameter(param: Annotated): KmValueParameter? =
50+
private fun findKotlinParameter(param: Annotated): JmValueParameter? =
5251
(param as? AnnotatedParameter)?.let { findKotlinParameter(it) }
5352

5453
// since 2.4
@@ -65,7 +64,7 @@ internal class KotlinFallbackAnnotationIntrospector(
6564
override fun refineDeserializationType(config: MapperConfig<*>, a: Annotated, baseType: JavaType): JavaType =
6665
findKotlinParameter(a)?.let { param ->
6766
val rawType = a.rawType
68-
param.type.reconstructClassOrNull()
67+
param.reconstructedClassOrNull
6968
?.takeIf { it.isUnboxableValueClass() && it != rawType }
7069
?.let { config.constructType(it) }
7170
} ?: baseType
@@ -123,11 +122,11 @@ internal class KotlinFallbackAnnotationIntrospector(
123122
?: super.findSetterInfo(ann)
124123
}
125124

126-
private fun KmValueParameter.isNullishTypeAt(index: Int): Boolean = type.arguments.getOrNull(index)?.let {
125+
private fun JmValueParameter.isNullishTypeAt(index: Int): Boolean = arguments.getOrNull(index)?.let {
127126
// If it is not a StarProjection, type is not null
128127
it === KmTypeProjection.STAR || it.type!!.isNullable
129128
} ?: true // If a type argument cannot be taken, treat it as nullable to avoid unexpected failure.
130129

131-
private fun KmValueParameter.requireStrictNullCheck(type: JavaType): Boolean =
130+
private fun JmValueParameter.requireStrictNullCheck(type: JavaType): Boolean =
132131
((type.isArrayType || type.isCollectionLikeType) && !this.isNullishTypeAt(0)) ||
133132
(type.isMapLikeType && !this.isNullishTypeAt(1))

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/annotationIntrospector/KotlinPrimaryAnnotationIntrospector.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import io.github.projectmapk.jackson.module.kogera.ReflectionCache
1717
import io.github.projectmapk.jackson.module.kogera.hasCreatorAnnotation
1818
import io.github.projectmapk.jackson.module.kogera.jmClass.JmClass
1919
import io.github.projectmapk.jackson.module.kogera.jmClass.JmProperty
20+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
2021
import io.github.projectmapk.jackson.module.kogera.reconstructClass
2122
import io.github.projectmapk.jackson.module.kogera.toSignature
2223
import kotlinx.metadata.KmClassifier
23-
import kotlinx.metadata.KmValueParameter
24-
import kotlinx.metadata.declaresDefaultValue
2524
import kotlinx.metadata.isNullable
2625
import java.lang.reflect.Constructor
2726
import java.lang.reflect.Executable
@@ -97,11 +96,11 @@ internal class KotlinPrimaryAnnotationIntrospector(
9796
// non required if...
9897
return when {
9998
// Argument definition is nullable
100-
paramDef.type.isNullable -> false
99+
paramDef.isNullable -> false
101100
// Default argument are defined
102-
paramDef.declaresDefaultValue -> false
101+
paramDef.isOptional -> false
103102
// vararg is treated as an empty array because undefined input is allowed
104-
paramDef.varargElementType != null -> false
103+
paramDef.isVararg -> false
105104
// The conversion in case of null is defined.
106105
type.hasDefaultEmptyValue() -> false
107106
else -> true
@@ -144,11 +143,11 @@ private fun Constructor<*>.isPrimarilyConstructorOf(jmClass: JmClass): Boolean =
144143
private fun KmClassifier.isString(): Boolean = this is KmClassifier.Class && this.name == "kotlin/String"
145144

146145
private fun isPossibleSingleString(
147-
kotlinParams: List<KmValueParameter>,
146+
kotlinParams: List<JmValueParameter>,
148147
javaFunction: Executable,
149148
propertyNames: Set<String>
150149
): Boolean = kotlinParams.size == 1 &&
151-
kotlinParams[0].let { it.name !in propertyNames && it.type.classifier.isString() } &&
150+
kotlinParams[0].let { it.name !in propertyNames && it.isString } &&
152151
javaFunction.parameters[0].annotations.none { it is JsonProperty }
153152

154153
private fun hasCreatorConstructor(clazz: Class<*>, jmClass: JmClass, propertyNames: Set<String>): Boolean {

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/argumentBucket/ArgumentBucket.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.argu
22

33
import io.github.projectmapk.jackson.module.kogera.ValueClassUnboxConverter
44
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.calcMaskSize
5-
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.creator.ValueParameter
5+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
66
import java.lang.reflect.Array as ReflectArray
77

88
private fun defaultPrimitiveValue(type: Class<*>): Any = when (type) {
@@ -47,7 +47,7 @@ private fun IntArray.update(index: Int, operation: MaskOperation) {
4747
// @see https://github.com/JetBrains/kotlin/blob/4c925d05883a8073e6732bca95bf575beb031a59/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt#L114
4848
internal class BucketGenerator(
4949
parameterTypes: List<Class<*>>,
50-
valueParameters: List<ValueParameter>,
50+
valueParameters: List<JmValueParameter>,
5151
private val converters: List<ValueClassUnboxConverter<Any>?>
5252
) {
5353
private val valueParameterSize: Int = parameterTypes.size

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/creator/ConstructorValueCreator.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.argum
88
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.calcMaskSize
99
import io.github.projectmapk.jackson.module.kogera.getDeclaredConstructorBy
1010
import io.github.projectmapk.jackson.module.kogera.jmClass.JmClass
11+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
1112
import java.lang.reflect.Constructor
1213

1314
internal class ConstructorValueCreator<T : Any>(
@@ -19,21 +20,19 @@ internal class ConstructorValueCreator<T : Any>(
1920

2021
override val isAccessible: Boolean = constructor.isAccessible
2122
override val callableName: String = constructor.name
22-
override val valueParameters: List<ValueParameter>
23+
override val valueParameters: List<JmValueParameter>
2324
override val bucketGenerator: BucketGenerator
2425

2526
init {
2627
// To prevent the call from failing, save the initial value and then rewrite the flag.
2728
if (!isAccessible) constructor.isAccessible = true
2829

29-
val constructorParameters = declaringJmClass.findJmConstructor(constructor)!!.valueParameters
30-
31-
valueParameters = constructorParameters.map { ValueParameter(it) }
30+
valueParameters = declaringJmClass.findJmConstructor(constructor)!!.valueParameters
3231
val rawTypes = constructor.parameterTypes.asList()
3332
bucketGenerator = BucketGenerator(
3433
rawTypes,
3534
valueParameters,
36-
constructorParameters.mapToConverters(rawTypes, cache)
35+
valueParameters.mapToConverters(rawTypes, cache)
3736
)
3837
}
3938

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/creator/MethodValueCreator.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.argum
88
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.calcMaskSize
99
import io.github.projectmapk.jackson.module.kogera.getDeclaredMethodBy
1010
import io.github.projectmapk.jackson.module.kogera.jmClass.JmClass
11+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
1112
import java.lang.reflect.Method
1213

1314
internal class MethodValueCreator<T>(
@@ -18,22 +19,21 @@ internal class MethodValueCreator<T>(
1819
private val companion: JmClass.CompanionObject = declaringJmClass.companion!!
1920
override val isAccessible: Boolean = method.isAccessible && companion.isAccessible
2021
override val callableName: String = method.name
21-
override val valueParameters: List<ValueParameter>
22+
override val valueParameters: List<JmValueParameter>
2223
override val bucketGenerator: BucketGenerator
2324

2425
init {
2526
// To prevent the call from failing, save the initial value and then rewrite the flag.
2627
if (!method.isAccessible) method.isAccessible = true
2728

2829
val function = companion.findFunctionByMethod(method)!!
29-
val kmParameters = function.valueParameters
3030

31-
valueParameters = kmParameters.map { ValueParameter(it) }
31+
valueParameters = function.valueParameters
3232
val rawTypes = method.parameterTypes.asList()
3333
bucketGenerator = BucketGenerator(
3434
rawTypes,
3535
valueParameters,
36-
kmParameters.mapToConverters(rawTypes, cache)
36+
valueParameters.mapToConverters(rawTypes, cache)
3737
)
3838
}
3939

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/creator/ValueCreator.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import io.github.projectmapk.jackson.module.kogera.ValueClassUnboxConverter
77
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.argumentBucket.ArgumentBucket
88
import io.github.projectmapk.jackson.module.kogera.deser.valueInstantiator.argumentBucket.BucketGenerator
99
import io.github.projectmapk.jackson.module.kogera.isUnboxableValueClass
10-
import io.github.projectmapk.jackson.module.kogera.reconstructClassOrNull
11-
import kotlinx.metadata.KmValueParameter
10+
import io.github.projectmapk.jackson.module.kogera.jmClass.JmValueParameter
1211

1312
/**
1413
* A class that abstracts the creation of instances by calling KFunction.
@@ -28,7 +27,7 @@ internal sealed class ValueCreator<T> {
2827
/**
2928
* ValueParameters of the KFunction to be called.
3029
*/
31-
abstract val valueParameters: List<ValueParameter>
30+
abstract val valueParameters: List<JmValueParameter>
3231

3332
protected abstract val bucketGenerator: BucketGenerator
3433

@@ -61,12 +60,12 @@ internal sealed class ValueCreator<T> {
6160
}
6261

6362
@Suppress("UNCHECKED_CAST")
64-
internal fun List<KmValueParameter>.mapToConverters(
63+
internal fun List<JmValueParameter>.mapToConverters(
6564
rawTypes: List<Class<*>>,
6665
cache: ReflectionCache
6766
): List<ValueClassUnboxConverter<Any>?> =
6867
mapIndexed { i, param ->
69-
param.type.reconstructClassOrNull()
68+
param.reconstructedClassOrNull
7069
?.takeIf { it.isUnboxableValueClass() && rawTypes[i] != it }
7170
?.let { cache.getValueClassUnboxConverter(it) }
7271
} as List<ValueClassUnboxConverter<Any>?> // Cast to cheat generics

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/deser/valueInstantiator/creator/ValueParameter.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package io.github.projectmapk.jackson.module.kogera.jmClass
22

33
import kotlinx.metadata.KmConstructor
4-
import kotlinx.metadata.KmValueParameter
54
import kotlinx.metadata.isSecondary
65
import kotlinx.metadata.jvm.JvmMethodSignature
76
import kotlinx.metadata.jvm.signature
87

98
internal data class JmConstructor(
109
val isSecondary: Boolean,
1110
val signature: JvmMethodSignature?,
12-
val valueParameters: List<KmValueParameter>
11+
val valueParameters: List<JmValueParameter>
1312
) {
1413
constructor(constructor: KmConstructor) : this(
1514
isSecondary = constructor.isSecondary,
1615
signature = constructor.signature,
17-
valueParameters = constructor.valueParameters
16+
valueParameters = constructor.valueParameters.map { JmValueParameter(it) }
1817
)
1918
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package io.github.projectmapk.jackson.module.kogera.jmClass
22

33
import kotlinx.metadata.KmFunction
4-
import kotlinx.metadata.KmValueParameter
54
import kotlinx.metadata.jvm.JvmMethodSignature
65
import kotlinx.metadata.jvm.signature
76

87
internal class JmFunction(
98
val signature: JvmMethodSignature?,
10-
val valueParameters: List<KmValueParameter>
9+
val valueParameters: List<JmValueParameter>
1110
) {
12-
constructor(function: KmFunction) : this(function.signature, function.valueParameters)
11+
constructor(function: KmFunction) : this(function.signature, function.valueParameters.map { JmValueParameter(it) })
1312
}

0 commit comments

Comments
 (0)