I have a DeclaredType
of a field and I would like to get the fully qualified type (raw type?) of the field. For example if the field is:
public static Optional<String> foo;
I would like to get java.util.Optional
.
Currently I can get the package name with:
env.getElementUtils().getPackageOf(declaredType.asElement());
I can get the type arguments of the type however I come back to the same problem I end up with a List
of TypeMirror
which I don't know how to get the qualified name of the types.
I have noticed that I can call TypeMirror#toString()
and that will return (for the above) something like:
java.util.Optional<java.lang.String>
I guess I could then cut of everything in front of <
but that feels like a hack.
For reference this is how I am getting the field:
private VariableElement getFieldWithName(DocletEnvironment environment, TypeElement classDoc, String fieldName) {
for(VariableElement e : ElementFilter.fieldsIn(environment.getElementUtils().getAllMembers(classDoc))) {
if(e.getSimpleName().toString().equals(fieldName)) {
return e;
}
}
return null;
}
TypeElement classElement = env.getElementUtils().getTypeElement(MyClass.class.getCanonicalName());
VariableElement fieldDoc = getFieldWithName(env, classElement, "foo");
DeclaredType declaredType = (DeclaredType) fieldDoc.asType();