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:
Zac Sweers
2020-10-04 18:03:45 -04:00
committed by GitHub
parent 850dc206cf
commit 5ce092857a
3 changed files with 12 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ package com.squareup.moshi.kotlin.codegen
import com.google.auto.service.AutoService import com.google.auto.service.AutoService
import com.squareup.kotlinpoet.AnnotationSpec 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.kotlinpoet.classinspector.elements.ElementsClassInspector
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import com.squareup.moshi.kotlin.codegen.api.AdapterGenerator import com.squareup.moshi.kotlin.codegen.api.AdapterGenerator
@@ -59,10 +59,10 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
* * `"javax.annotation.Generated"` (JRE <9) * * `"javax.annotation.Generated"` (JRE <9)
*/ */
const val OPTION_GENERATED = "moshi.generated" const val OPTION_GENERATED = "moshi.generated"
private val POSSIBLE_GENERATED_NAMES = setOf( private val POSSIBLE_GENERATED_NAMES = arrayOf(
"javax.annotation.processing.Generated", ClassName("javax.annotation.processing", "Generated"),
"javax.annotation.Generated" ClassName("javax.annotation", "Generated")
) ).associateBy { it.canonicalName }
} }
private lateinit var types: Types private lateinit var types: Types
@@ -71,7 +71,7 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
private lateinit var messager: Messager private lateinit var messager: Messager
private lateinit var cachedClassInspector: MoshiCachedClassInspector private lateinit var cachedClassInspector: MoshiCachedClassInspector
private val annotation = JsonClass::class.java private val annotation = JsonClass::class.java
private var generatedType: TypeElement? = null private var generatedType: ClassName? = null
override fun getSupportedAnnotationTypes() = setOf(annotation.canonicalName) override fun getSupportedAnnotationTypes() = setOf(annotation.canonicalName)
@@ -82,11 +82,10 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
override fun init(processingEnv: ProcessingEnvironment) { override fun init(processingEnv: ProcessingEnvironment) {
super.init(processingEnv) super.init(processingEnv)
generatedType = processingEnv.options[OPTION_GENERATED]?.let { 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, " + "Invalid option value for $OPTION_GENERATED. Found $it, " +
"allowable values are $POSSIBLE_GENERATED_NAMES." "allowable values are $POSSIBLE_GENERATED_NAMES."
} )
processingEnv.elementUtils.getTypeElement(it)
} }
this.types = processingEnv.typeUtils this.types = processingEnv.typeUtils
this.elements = processingEnv.elementUtils this.elements = processingEnv.elementUtils
@@ -117,7 +116,8 @@ class JsonClassCodegenProcessor : AbstractProcessor() {
.prepare { spec -> .prepare { spec ->
spec.toBuilder() spec.toBuilder()
.apply { .apply {
generatedType?.asClassName()?.let { generatedClassName -> @Suppress("DEPRECATION") // This is a Java type
generatedType?.let { generatedClassName ->
addAnnotation( addAnnotation(
AnnotationSpec.builder(generatedClassName) AnnotationSpec.builder(generatedClassName)
.addMember( .addMember(

View File

@@ -178,7 +178,7 @@ internal class AdapterGenerator(
.mapTo(mutableSetOf()) { prop -> .mapTo(mutableSetOf()) { prop ->
QualifierAdapterProperty( QualifierAdapterProperty(
name = prop.name, name = prop.name,
qualifiers = prop.annotations.mapTo(mutableSetOf()) { it.className } qualifiers = prop.annotations.mapTo(mutableSetOf()) { it.typeName.rawType() }
) )
} }

View File

@@ -47,7 +47,7 @@ internal data class DelegateKey(
propertyName: String propertyName: String
): PropertySpec { ): PropertySpec {
val qualifierNames = jsonQualifiers.joinToString("") { val qualifierNames = jsonQualifiers.joinToString("") {
"At${it.className.simpleName}" "At${it.typeName.rawType().simpleName}"
} }
val adapterName = nameAllocator.newName( val adapterName = nameAllocator.newName(
"${type.toVariableName().decapitalize()}${qualifierNames}Adapter", "${type.toVariableName().decapitalize()}${qualifierNames}Adapter",