In java.lang.reflect
, one would do:
Field someField = ...;
Class<?> fieldType = someField.getType();
But what do I do with javax.lang.model
's VariableElement
(which may or may not represent a field)? A corresponding return value would be (I guess) TypeElement
.
VariableElement someField = ...;
TypeElement fieldType = someField.???;
So, in javax.lang.model
, how do I get the type (or TypeElement
) of a field, represented by VariableElement
?
BTW, there is not a single Stackoverflow-tag which would fit to javax.lang.model ;)
Well, I don't know, it that's the right way to do this.
Would be nice if someone, who actually understands this API, told me.
But well, seams to work.
public class SomeClass {
private final ProcessingEnvironment pe = /* get it somewhere */;
private final Types typeUtils = pe.getTypeUtils();
public TypeElement getExtracted(VariableElement ve) {
TypeMirror typeMirror = ve.asType();
Element element = typeUtils.asElement(typeMirror);
// instanceof implies null-ckeck
return (element instanceof TypeElement)
? (TypeElement)element : null;
}
}
It seems that the class Types
has to be got from current ProcessingEnvironment
because some of its internals depend on it, so it's not a usual utility class.