gsonpath.util.ExtensionsHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gsonpath-compiler Show documentation
Show all versions of gsonpath-compiler Show documentation
An annotation processor which generates Type Adapters for the Google Gson library
package gsonpath.util
import gsonpath.ProcessingException
import gsonpath.compiler.ExtensionFieldMetadata
import gsonpath.compiler.GsonPathExtension
import gsonpath.model.GsonField
import javax.annotation.processing.ProcessingEnvironment
class ExtensionsHandler(
private val processingEnvironment: ProcessingEnvironment,
private val extensions: List) {
private fun canHandleFieldFunc(
gsonField: GsonField,
variableName: String,
func: (GsonPathExtension, ExtensionFieldMetadata) -> Boolean): Boolean {
val supportedExtensions = extensions
.map { func(it, createMetadata(gsonField, variableName)) }
.filter { it }
.count()
if (supportedExtensions > 1) {
throw ProcessingException("It is not possible to guarantee extension ordering, " +
"therefore it is illegal to use multiple", gsonField.fieldInfo.element)
}
return supportedExtensions == 1
}
fun canHandleFieldRead(gsonField: GsonField, variableName: String): Boolean {
return canHandleFieldFunc(gsonField, variableName) { extension, metadata ->
extension.canHandleFieldRead(processingEnvironment, metadata)
}
}
fun canHandleFieldWrite(gsonField: GsonField, variableName: String): Boolean {
return canHandleFieldFunc(gsonField, variableName) { extension, metadata ->
extension.canHandleFieldWrite(processingEnvironment, metadata)
}
}
fun executeFieldRead(
gsonField: GsonField,
variableName: String,
checkIfResultIsNull: Boolean,
handleFunc: (String, GsonPathExtension.ExtensionResult) -> Unit) {
if (!canHandleFieldRead(gsonField, variableName)) {
throw IllegalStateException("canHandleFieldRead must be checked before calling this method.")
}
extensions.forEach { extension ->
val extensionFieldMetadata = createMetadata(gsonField, variableName)
if (extension.canHandleFieldRead(processingEnvironment, extensionFieldMetadata)) {
handleFunc(extension.extensionName, extension.createCodeReadResult(
processingEnvironment, extensionFieldMetadata, checkIfResultIsNull))
}
}
}
fun executeFieldWrite(gsonField: GsonField, variableName: String, handleFunc: (String, GsonPathExtension.ExtensionResult) -> Unit) {
if (!canHandleFieldWrite(gsonField, variableName)) {
throw IllegalStateException("canHandleFieldWrite must be checked before calling this method.")
}
extensions.forEach { extension ->
val extensionFieldMetadata = createMetadata(gsonField, variableName)
if (extension.canHandleFieldWrite(processingEnvironment, extensionFieldMetadata)) {
handleFunc(extension.extensionName, extension.createCodeWriteResult(
processingEnvironment, extensionFieldMetadata))
}
}
}
fun executePostRead(gsonField: GsonField, variableName: String, handleFunc: (String, GsonPathExtension.ExtensionResult) -> Unit) {
extensions.forEach { extension ->
extension.createCodePostReadResult(processingEnvironment, createMetadata(gsonField, variableName))
?.let {
handleFunc(extension.extensionName, it)
}
}
}
private fun createMetadata(gsonField: GsonField, variableName: String) =
ExtensionFieldMetadata(gsonField.fieldInfo, variableName, gsonField.jsonPath, gsonField.isRequired)
}