From bcfce60577f5fb8642c88fcffa330e74bc22b7b6 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Mon, 29 Mar 2021 20:44:55 -0400 Subject: [PATCH] Warn on missing TypeElement rather than error (#1323) One example I found for this is when a property is annotated with android's `@SuppressLint`, which is only available from the android jar and not visible during apt. This should just be a warning (if even?) and not a hard error. --- .../squareup/moshi/kotlin/codegen/metadata.kt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/metadata.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/metadata.kt index e9ff9201d..5444688a0 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/metadata.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/metadata.kt @@ -62,6 +62,7 @@ import javax.lang.model.type.DeclaredType import javax.lang.model.util.Elements import javax.lang.model.util.Types import javax.tools.Diagnostic.Kind.ERROR +import javax.tools.Diagnostic.Kind.WARNING private val JSON_QUALIFIER = JsonQualifier::class.java private val JSON = Json::class.asClassName() @@ -94,7 +95,7 @@ internal fun primaryConstructor( index = index, type = parameter.type, hasDefault = parameter.defaultValue != null, - qualifiers = parameter.annotations.qualifiers(elements), + qualifiers = parameter.annotations.qualifiers(messager, elements), jsonName = parameter.annotations.jsonName() ) } @@ -450,7 +451,7 @@ internal fun TargetProperty.generator( } // Merge parameter and property annotations - val qualifiers = parameter?.qualifiers.orEmpty() + propertySpec.annotations.qualifiers(elements) + val qualifiers = parameter?.qualifiers.orEmpty() + propertySpec.annotations.qualifiers(messager, elements) for (jsonQualifier in qualifiers) { val qualifierRawType = jsonQualifier.typeName.rawType() // Check Java types since that covers both Java and Kotlin annotations. @@ -486,13 +487,17 @@ internal fun TargetProperty.generator( ) } -private fun List?.qualifiers(elements: Elements): Set { +private fun List?.qualifiers( + messager: Messager, + elements: Elements +): Set { if (this == null) return setOf() return filterTo(mutableSetOf()) { - val typeElement = checkNotNull(elements.getTypeElement(it.typeName.rawType().canonicalName)) { - "Could not get the type element of $it" + val typeElement: TypeElement? = elements.getTypeElement(it.typeName.rawType().canonicalName) + if (typeElement == null) { + messager.printMessage(WARNING, "Could not get the TypeElement of $it") } - typeElement.getAnnotation(JSON_QUALIFIER) != null + typeElement?.getAnnotation(JSON_QUALIFIER) != null } }