【发布时间】:2018-01-28 06:31:55
【问题描述】:
我正在为 Kotlin 开发注解处理器,因为处理的元素是在 Java 中,所以我没有收到可以为 ? 的可空值,而是使用 @Nullable 注解,这很好,但我在接收时遇到了问题对于普通参数,类型和高阶函数中的 null 参数。
var someNullField: String? = ""
我将在过程中收到java.lang.String,并在其注释中添加@org.jetbrains.annotations.Nullable。
但是 List<String?> 例如会返回我 java.util.List<java.lang.String> 没有任何注释,不在主元素中,不在类型参数中,导致未知的可空性状态
我尝试使用javax.lang.model.util.Types 找到某种结果,但什么也没有。
我现在使用的一些代码:
val utils = processingEnvironment.typeUtils
val type = fieldElement.asType()
if (type is DeclaredType) {
val typeElement = utils.asElement(type)
type.typeArguments
.forEach {
//Trying different ways and just printing for possible results
val capture = utils.capture(it)
val erasure = utils.erasure(it)
val element = utils.asElement(it)
printMessage("element: $element isNullable: ${element.isNullable()} isNotNull: ${element.isNotNull()}\ncapture: $capture isNullable: ${capture.isNullable()} isNotNull: ${capture.isNotNull()}\nerasure: $erasure isNullable: ${erasure.isNullable()} isNotNull: ${erasure.isNotNull()}")
}
}
我们将不胜感激。
【问题讨论】:
标签: java annotations kotlin annotation-processing kapt