se.ansman.kotshi.kapt.MetadataAccessor.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.TypeSpec
import com.squareup.kotlinpoet.metadata.ImmutableKmClass
import com.squareup.kotlinpoet.metadata.specs.ClassInspector
import com.squareup.kotlinpoet.metadata.specs.toTypeSpec
import com.squareup.kotlinpoet.metadata.toImmutableKmClass
import javax.lang.model.element.Element
class MetadataAccessor(private val classInspector: ClassInspector) {
private val metadataPerType = mutableMapOf()
private val typeSpecPerType = mutableMapOf()
fun getMetadata(type: Element): ImmutableKmClass =
metadataPerType.getOrPut(type) {
type.metadata.toImmutableKmClass()
}
fun getTypeSpec(type: Element): TypeSpec =
typeSpecPerType.getOrPut(type) {
getMetadata(type).toTypeSpec(classInspector)
}
}