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

gsonpath.generator.adapter.properties.AutoGsonAdapterPropertiesFactory.kt Maven / Gradle / Ivy

Go to download

An annotation processor which generates Type Adapters for the Google Gson library

There is a newer version: 4.0.0
Show newest version
package gsonpath.generator.adapter.properties

import gsonpath.AutoGsonAdapter
import gsonpath.GsonFieldValidationType
import gsonpath.ProcessingException
import javax.lang.model.element.TypeElement

class AutoGsonAdapterPropertiesFactory {

    @Throws(ProcessingException::class)
    fun create(
            modelElement: TypeElement,
            autoGsonAnnotation: AutoGsonAdapter,
            isInterface: Boolean): AutoGsonAdapterProperties {

        val gsonFieldValidationType =
                if (isInterface) {
                    // Interfaces must use field validation to prevent issues with primitives.
                    when (autoGsonAnnotation.fieldValidationType) {
                        GsonFieldValidationType.NO_VALIDATION ->
                            GsonFieldValidationType.VALIDATE_EXPLICIT_NON_NULL

                        else ->
                            autoGsonAnnotation.fieldValidationType
                    }
                } else {
                    autoGsonAnnotation.fieldValidationType
                }

        val pathSubstitutions = autoGsonAnnotation.substitutions

        // Validate the path substitutions. Duplicate keys are not allowed.
        pathSubstitutions.fold(emptySet()) { set: Set, pathSubstitution ->
            if (set.contains(pathSubstitution.original)) {
                throw ProcessingException("PathSubstitution original values must be unique", modelElement)
            }
            set.plus(pathSubstitution.original)
        }

        return AutoGsonAdapterProperties(
                autoGsonAnnotation.ignoreNonAnnotatedFields,
                autoGsonAnnotation.flattenDelimiter,
                autoGsonAnnotation.serializeNulls,
                autoGsonAnnotation.rootField,
                gsonFieldValidationType,
                autoGsonAnnotation.fieldNamingPolicy,
                pathSubstitutions)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy