Skip to content

Commit

Permalink
Fix some misc deprecated KotlinPoet APIs (#1249)
Browse files Browse the repository at this point in the history
* Move off of deprecated AnnotationSpec.className

* Simplify generatedType check and avoid deprecated KP API
  • Loading branch information
ZacSweers authored Oct 4, 2020
1 parent 850dc20 commit 5ce0928
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5ce0928

Please sign in to comment.