se.ansman.kotshi.kapt.KotlinPoetExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compiler Show documentation
Show all versions of compiler Show documentation
An annotations processor that generates Moshi adapters from Kotlin data classes
package se.ansman.kotshi.kapt
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.tag
import com.squareup.moshi.JsonQualifier
import se.ansman.kotshi.JSON_UNSET_NAME
import se.ansman.kotshi.Types
import se.ansman.kotshi.kapt.AnnotationModelValueVisitor.Companion.toAnnotationModel
import se.ansman.kotshi.model.AnnotationModel
import javax.lang.model.element.AnnotationMirror
fun List.find(typeName: ClassName): AnnotationSpec? = find { it.typeName == typeName }
fun List.jsonName(): String? =
(find(Types.Kotshi.jsonProperty) ?: find(Types.Moshi.json))?.let { spec ->
requireNotNull(spec.tag()).getValueOrNull("name")
?.takeUnless { it == JSON_UNSET_NAME }
}
fun List.isJsonIgnore(): Boolean? =
find(Types.Moshi.json)?.let { spec ->
requireNotNull(spec.tag()).getValueOrNull("ignore")
}
fun List.qualifiers(metadataAccessor: MetadataAccessor): Set =
mapNotNullTo(mutableSetOf()) { spec ->
val mirror = requireNotNull(spec.tag())
if (mirror.annotationType.asElement().getAnnotation(JsonQualifier::class.java) == null) {
return@mapNotNullTo null
}
mirror.toAnnotationModel(metadataAccessor)
}