mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Fix some misc deprecated KotlinPoet APIs (#1249)
* Move off of deprecated AnnotationSpec.className * Simplify generatedType check and avoid deprecated KP API
This commit is contained in:
@@ -17,7 +17,7 @@ package com.squareup.moshi.kotlin.codegen
|
||||
|
||||
import com.google.auto.service.AutoService
|
||||
import com.squareup.kotlinpoet.AnnotationSpec
|
||||
import com.squareup.kotlinpoet.asClassName
|
||||
import com.squareup.kotlinpoet.ClassName
|
||||
import com.squareup.kotlinpoet.classinspector.elements.ElementsClassInspector
|
||||
import com.squareup.moshi.JsonClass
|
||||
import com.squareup.moshi.kotlin.codegen.api.AdapterGenerator
|
||||
@@ -59,10 +59,10 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
|
||||
* * `"javax.annotation.Generated"` (JRE <9)
|
||||
*/
|
||||
const val OPTION_GENERATED = "moshi.generated"
|
||||
private val POSSIBLE_GENERATED_NAMES = setOf(
|
||||
"javax.annotation.processing.Generated",
|
||||
"javax.annotation.Generated"
|
||||
)
|
||||
private val POSSIBLE_GENERATED_NAMES = arrayOf(
|
||||
ClassName("javax.annotation.processing", "Generated"),
|
||||
ClassName("javax.annotation", "Generated")
|
||||
).associateBy { it.canonicalName }
|
||||
}
|
||||
|
||||
private lateinit var types: Types
|
||||
@@ -71,7 +71,7 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
|
||||
private lateinit var messager: Messager
|
||||
private lateinit var cachedClassInspector: MoshiCachedClassInspector
|
||||
private val annotation = JsonClass::class.java
|
||||
private var generatedType: TypeElement? = null
|
||||
private var generatedType: ClassName? = null
|
||||
|
||||
override fun getSupportedAnnotationTypes() = setOf(annotation.canonicalName)
|
||||
|
||||
@@ -82,11 +82,10 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
|
||||
override fun init(processingEnv: ProcessingEnvironment) {
|
||||
super.init(processingEnv)
|
||||
generatedType = processingEnv.options[OPTION_GENERATED]?.let {
|
||||
require(it in POSSIBLE_GENERATED_NAMES) {
|
||||
POSSIBLE_GENERATED_NAMES[it] ?: error(
|
||||
"Invalid option value for $OPTION_GENERATED. Found $it, " +
|
||||
"allowable values are $POSSIBLE_GENERATED_NAMES."
|
||||
}
|
||||
processingEnv.elementUtils.getTypeElement(it)
|
||||
)
|
||||
}
|
||||
this.types = processingEnv.typeUtils
|
||||
this.elements = processingEnv.elementUtils
|
||||
@@ -117,7 +116,8 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
|
||||
.prepare { spec ->
|
||||
spec.toBuilder()
|
||||
.apply {
|
||||
generatedType?.asClassName()?.let { generatedClassName ->
|
||||
@Suppress("DEPRECATION") // This is a Java type
|
||||
generatedType?.let { generatedClassName ->
|
||||
addAnnotation(
|
||||
AnnotationSpec.builder(generatedClassName)
|
||||
.addMember(
|
||||
|
@@ -178,7 +178,7 @@ internal class AdapterGenerator(
|
||||
.mapTo(mutableSetOf()) { prop ->
|
||||
QualifierAdapterProperty(
|
||||
name = prop.name,
|
||||
qualifiers = prop.annotations.mapTo(mutableSetOf()) { it.className }
|
||||
qualifiers = prop.annotations.mapTo(mutableSetOf()) { it.typeName.rawType() }
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -47,7 +47,7 @@ internal data class DelegateKey(
|
||||
propertyName: String
|
||||
): PropertySpec {
|
||||
val qualifierNames = jsonQualifiers.joinToString("") {
|
||||
"At${it.className.simpleName}"
|
||||
"At${it.typeName.rawType().simpleName}"
|
||||
}
|
||||
val adapterName = nameAllocator.newName(
|
||||
"${type.toVariableName().decapitalize()}${qualifierNames}Adapter",
|
||||
|
Reference in New Issue
Block a user