Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get class name and package of VariableElement

Tags:

java

I have a VariableElement object ve (which I got from getting the parameters of an ExecutableElement object) at compile time in an annotations processor (in the process() method). The class represented by this VariableElement object can be different in different cases.

How can I get the full package and classname as a string of ve at compile time?

(e.g. if ve represents a parameter which is of class String, I need to get the string "java.lang.String")

edit: based on the answers so far, it seems I have not been clear enough. ve is not a String object, but is actually a VariableElement object which represents a parameter element inside a method call (ExecutableElement). I need to get information about what is being represented by the VariableElement ve (specifically what the class information is about the class of the parameter), rather than the ve object itself.

like image 390
Aman Singh Avatar asked Oct 19 '25 20:10

Aman Singh


1 Answers

You can use the VariableElements asType() method to get a TypeMirror object and then the toString() method should return a "source code" representation of the type. There's also some utility function in Types that can be used with TypeMirrors.

like image 167
mroman Avatar answered Oct 22 '25 12:10

mroman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!