In Annotation Processing, the class to be processed can be handled only by Element and TypeMirror, and cannot be handled as Class. Therefore, when comparing with a class that is not the processing target (determining whether the target class inherits a specific class, etc.), it is necessary to obtain TypeElement or TypeMirror from Class. If you can get TypeElement, you can get TypeMirror with ʻElement # asType ()`, so if you can get TypeElement from Class, it will be solved.
The method to use is java.lang.model.util.Elements # getTypeElement (CharSequence name)
java.lang.model.util.Elements
is an interface.
You can get an instance with ProcessingEnvironment # getElementUtils ()
.
Therefore, you can get it by referring to processingEnv in the class that inherits AbstractProcessor.
So the code looks like this:
By the way, Java is Java 8.
ExampleProcessor.java
Elements elementUtils = processingEnv.getElementUtils()
TypeElement element = elementUtils.getTypeElement("java.lang.String")
ExampleProcessor.kt
val elementUtils = processingEnv.elementUtils
val element = elementUtils.getTypeElement("java.lang.String")
The element is now a TypeElement of the java.lang.String
class.
Recommended Posts