我正在生产一个注释处理器,在参数中带有带有KClass的注释。我需要检查在处理器中检索到的KClass是否为数据类。
我不能用
MyDataClass::class.isData
由于要访问批注中的Class或KClass参数时抛出的
MirroredTypeException
,try {
element.getAnnotation(MyAnnotation::class.java).myClass
} catch (mte: MirroredTypeException) {
typeElement = typeUtils.asElement(mte.typeMirror) as TypeElement
}
现在,我只能使用TypeElement。
Kotlinpoet提供的
KModifier
包含DATA
属性,但仅可用于类生成而不是读取。如何有效检查?
最佳答案
不知道它是否仍然与您相关,但我还是会回答:
与某些评论所说的相反,尽管有些复杂,实际上可以在注释处理过程中找到有关元素的Kotlin特定信息。额外的Kotlin数据以@Metadata
-注释存储在 Protocol Buffer 中。您可以使用this library来阅读@Metadata
-annotations,然后按如下所示进行检查:
(element.kotlinMetadata as KotlinClassMetadata).data.classProto.isDataClass
关于kotlin - 检查TypeElement是否为注释处理器中的Kotlin数据类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50852072/