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.
This commit is contained in:
Zac Sweers
2021-03-29 20:44:55 -04:00
committed by GitHub
parent 7ef4a32d52
commit bcfce60577

View File

@@ -62,6 +62,7 @@ import javax.lang.model.type.DeclaredType
import javax.lang.model.util.Elements import javax.lang.model.util.Elements
import javax.lang.model.util.Types import javax.lang.model.util.Types
import javax.tools.Diagnostic.Kind.ERROR import javax.tools.Diagnostic.Kind.ERROR
import javax.tools.Diagnostic.Kind.WARNING
private val JSON_QUALIFIER = JsonQualifier::class.java private val JSON_QUALIFIER = JsonQualifier::class.java
private val JSON = Json::class.asClassName() private val JSON = Json::class.asClassName()
@@ -94,7 +95,7 @@ internal fun primaryConstructor(
index = index, index = index,
type = parameter.type, type = parameter.type,
hasDefault = parameter.defaultValue != null, hasDefault = parameter.defaultValue != null,
qualifiers = parameter.annotations.qualifiers(elements), qualifiers = parameter.annotations.qualifiers(messager, elements),
jsonName = parameter.annotations.jsonName() jsonName = parameter.annotations.jsonName()
) )
} }
@@ -450,7 +451,7 @@ internal fun TargetProperty.generator(
} }
// Merge parameter and property annotations // 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) { for (jsonQualifier in qualifiers) {
val qualifierRawType = jsonQualifier.typeName.rawType() val qualifierRawType = jsonQualifier.typeName.rawType()
// Check Java types since that covers both Java and Kotlin annotations. // Check Java types since that covers both Java and Kotlin annotations.
@@ -486,13 +487,17 @@ internal fun TargetProperty.generator(
) )
} }
private fun List<AnnotationSpec>?.qualifiers(elements: Elements): Set<AnnotationSpec> { private fun List<AnnotationSpec>?.qualifiers(
messager: Messager,
elements: Elements
): Set<AnnotationSpec> {
if (this == null) return setOf() if (this == null) return setOf()
return filterTo(mutableSetOf()) { return filterTo(mutableSetOf()) {
val typeElement = checkNotNull(elements.getTypeElement(it.typeName.rawType().canonicalName)) { val typeElement: TypeElement? = elements.getTypeElement(it.typeName.rawType().canonicalName)
"Could not get the type element of $it" if (typeElement == null) {
messager.printMessage(WARNING, "Could not get the TypeElement of $it")
} }
typeElement.getAnnotation(JSON_QUALIFIER) != null typeElement?.getAnnotation(JSON_QUALIFIER) != null
} }
} }