Kotlin数据类定义不在roundEnv.rootElements(注释处理)中

时间:2018-04-25 00:22:14

标签: kotlin annotations annotation-processing data-class

在扩展AbstractProcessor的注释处理器中,我有:

override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
    roundEnv.getElementsAnnotatedWith(FxBean::class.java)
        .forEach {
            val className = it.simpleName.toString()

            val metaDataClass = Class.forName("kotlin.Metadata").asSubclass(Annotation::class.java)
            val isKotlinClass = it.getAnnotation(metaDataClass) != null

            if (isKotlinClass) {
                // Trying to get the list of methods
                // of the data class with the @FxBean annotation.
                val methods = roundEnv.rootElements.find {
                    it.simpleName.toString() == className // This is never true
                }?.let {
                    val methods = mutableListOf<ExecutableElement>()
                    it.enclosedElements.forEach {
                        if (it is ExecutableElement) {
                            methods.add(it)
                        }
                    }
                    methods
                }
            }
        }
    return true
}

我错过了什么?有没有办法让Element表示正在处理@FxBean注释的数据类?

1 个答案:

答案 0 :(得分:1)

根元素不能保证是带注释的元素,它们是。注释处理根可以是单独的元素,带有注释注释,通过getSupportedAnnotationTypes()或类/包定向,包含这些元素的集合, - API指定得有些糟糕,因此根元素的确切性质是模糊的。 / p>

事实是,Kotlin和最新版本的Gradle可以包装注释处理器API并提供他们自己的RoundEnvironment实现,这种情况更加复杂。这些实现可能不会以与javac默认值一致的方式运行。