com.fasterxml.jackson.module.kotlin.ArgumentBucket.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-module-kotlin Show documentation
Show all versions of jackson-module-kotlin Show documentation
Add-on module for Jackson (https://github.com/FasterXML/jackson/) to support
Kotlin language, specifically introspection of method/constructor parameter names,
without having to add explicit property name annotation.
package com.fasterxml.jackson.module.kotlin
import kotlin.reflect.KParameter
internal class BucketGenerator private constructor(paramSize: Int, instanceParameter: KParameter?, instance: Any?) {
private val originalParameters = arrayOfNulls(paramSize)
private val originalArguments = arrayOfNulls(paramSize)
private val initialCount: Int
init {
if (instance != null) {
originalParameters[0] = instanceParameter
originalArguments[0] = instance
initialCount = 1
} else {
initialCount = 0
}
}
fun generate(): ArgumentBucket = ArgumentBucket(
parameters = originalParameters.clone(),
arguments = originalArguments.clone(),
count = initialCount
)
companion object {
fun forConstructor(paramSize: Int): BucketGenerator = BucketGenerator(paramSize, null, null)
fun forMethod(paramSize: Int, instanceParameter: KParameter, instance: Any): BucketGenerator =
BucketGenerator(paramSize, instanceParameter, instance)
}
}
internal class ArgumentBucket(
private val parameters: Array,
val arguments: Array,
private var count: Int
) : Map {
operator fun set(key: KParameter, value: Any?) {
arguments[key.index] = value
parameters[key.index] = key
// Multiple calls are not checked because internally no calls are made more than once per argument.
count++
}
val isFullInitialized: Boolean get() = count == arguments.size
private class Entry(override val key: KParameter, override val value: Any?) : Map.Entry
override val entries: Set>
get() = parameters.mapNotNull { key -> key?.let { Entry(it, arguments[it.index]) } }.toSet()
override val keys: Set
get() = parameters.filterNotNull().toSet()
override val size: Int
get() = count
override val values: Collection
get() = keys.map { arguments[it.index] }
override fun isEmpty(): Boolean = this.size == 0
// Skip the check here, as it is only called after the check for containsKey.
override fun get(key: KParameter): Any? = arguments[key.index]
override fun containsValue(value: Any?): Boolean = keys.any { arguments[it.index] == value }
override fun containsKey(key: KParameter): Boolean = parameters.any { it?.index == key.index }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy