How can I get package name
, generic type
and Parametrized type
from a type
from a field element
in Annotation processor?
Say, if Element.asType
returns java.util.List<String>
, I want to get
java.util
List<E>
or raw type List
(preferably raw type) String
Is there any method in element utils
, type utils
?
Getting the package java.util
:
Element e = processingEnv.getTypeUtils().asElement(type);
PackageElement pkg = processingEnv.getElementUtils().getPackageOf(e);
Getting the raw type List
:
TypeMirror raw = processingEnv.getTypeUtils().erasure(type);
Getting the type arguments e.g. String
:
if (type.getKind() == TypeKind.DECLARED) {
List<? extends TypeMirror> args =
((DeclaredType) type).getTypeArguments();
args.forEach(t -> {/*...*/});
}
See: Types.asElement
, Elements.getPackageOf
, Types.erasure
and DeclaredType.getTypeArguments
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With