Fold the kotlin-codegen-runtime into Moshi itself.

Rename @MoshiSerializable to @JsonClass. Like @Json, I'm anticipating
a future where there are other interesting properties on this annotation.
Perhaps a future feature where Moshi is strict and only adapts types that
have a '@JsonClass' annotation.

Also rename MoshiKotlinCodeGenProcessor to JsonClassCodeGenProcessor. We
may later support other ways of generating code here; perhaps for regular
Java types.
This commit is contained in:
Jesse Wilson
2018-03-24 23:19:37 -04:00
parent d045947ea7
commit 982f9c94f6
9 changed files with 155 additions and 227 deletions

View File

@@ -50,23 +50,25 @@ import javax.tools.Diagnostic.Kind.ERROR
* adapter will also be internal).
*
* If you define a companion object, a jsonAdapter() extension function will be generated onto it.
* If you don't want this though, you can use the runtime [MoshiSerializable] factory implementation.
* If you don't want this though, you can use the runtime [JsonClass] factory implementation.
*/
@AutoService(Processor::class)
class MoshiKotlinCodeGenProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
class JsonClassCodeGenProcessor : KotlinAbstractProcessor(), KotlinMetadataUtils {
private val annotationName = MoshiSerializable::class.java.canonicalName
private val annotation = JsonClass::class.java
override fun getSupportedAnnotationTypes() = setOf(annotationName)
override fun getSupportedAnnotationTypes() = setOf(annotation.canonicalName)
override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latest()
override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {
val annotationElement = elementUtils.getTypeElement(annotationName)
roundEnv.getElementsAnnotatedWith(annotationElement)
.asSequence()
.mapNotNull { processElement(it) }
.forEach { it.generateAndWrite() }
for (type in roundEnv.getElementsAnnotatedWith(annotation)) {
val jsonClass = type.getAnnotation(annotation)
if (jsonClass.generateAdapter) {
val adapterGenerator = processElement(type) ?: continue
adapterGenerator.generateAndWrite()
}
}
return true
}
@@ -178,7 +180,7 @@ class MoshiKotlinCodeGenProcessor : KotlinAbstractProcessor(), KotlinMetadataUti
private fun errorMustBeDataClass(element: Element) {
messager.printMessage(ERROR,
"@${MoshiSerializable::class.java.simpleName} can't be applied to $element: must be a Kotlin data class",
"@${JsonClass::class.java.simpleName} can't be applied to $element: must be a Kotlin data class",
element)
}