0

Inside an annotation processor that extends AbstractProcessor I have:

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
}

What am I missing? Is there a way to get the Element representing the data class with the @FxBean annotation being processed?

Vitaly
  • 4,358
  • 7
  • 47
  • 59

1 Answers1

1

Root elements aren't guaranteed to be annotated elements, they are roots. Annotation processing roots can be individual elements, annotated with annotations, targeted via getSupportedAnnotationTypes(), or classes/packages, encompassing the set of those elements, — the API is somewhat poorly specified, so exact nature of root elements is vague.

The situation is further complicated by the fact, that Kotlin and, as of the latest version, Gradle may wrap annotation processor APIs and provide their own implementations of RoundEnvironment. Those implementations might not behave in manner, consistent with javac defaults.

user1643723
  • 4,109
  • 1
  • 24
  • 48