All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.jackson.module.kotlin.ValueCreator.kt Maven / Gradle / Ivy

Go to download

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.

The newest version!
package com.fasterxml.jackson.module.kotlin

import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.MapperFeature
import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.valueParameters

/**
 * A class that abstracts the creation of instances by calling KFunction.
 * @see KotlinValueInstantiator
 */
internal sealed class ValueCreator {
    /**
     * Function to be call.
     */
    protected abstract val callable: KFunction

    /**
     * Initial value for accessibility by reflection.
     */
    protected abstract val accessible: Boolean

    protected abstract val bucketGenerator: BucketGenerator

    fun generateBucket(): ArgumentBucket = bucketGenerator.generate()

    /**
     * ValueParameters of the KFunction to be called.
     */
    // If this result is cached, it will coexist with the SoftReference managed value in kotlin-reflect,
    // and there is a risk of doubling the memory consumption, so it should not be cached.
    // @see #584
    val valueParameters: List get() = callable.valueParameters

    /**
     * Checking process to see if access from context is possible.
     * @throws  IllegalAccessException
     */
    fun checkAccessibility(ctxt: DeserializationContext) {
        if ((!accessible && ctxt.config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) ||
            (accessible && ctxt.config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS))) {
            return
        }

        throw IllegalAccessException("Cannot access to function or companion object instance, target: $callable")
    }

    /**
     * Function call with default values enabled.
     */
    fun callBy(args: ArgumentBucket): T = if (args.isFullInitialized) {
        callable.call(*args.arguments)
    } else {
        callable.callBy(args)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy