Get package name and parameterized type from field element - annotation processor


Malodin

How can I get package namethat from generic typeand Parametrized typefrom in the annotation processor?typefield element

Say, if it Element.asTypereturns , I want to getjava.util.List<String>

  • package namejava.util
  • Generic type List<E>or primitive type List(preferably primitive)
  • actual typeString

Is there any way element utilsto type utilsdo this?

Radiodef

Get the package java.util:

Element        e   = processingEnv.getTypeUtils().asElement(type);
PackageElement pkg = processingEnv.getElementUtils().getPackageOf(e);

Get primitive type List:

TypeMirror raw = processingEnv.getTypeUtils().erasure(type);

Get type parameters, for example String:

if (type.getKind() == TypeKind.DECLARED) {
    List<? extends TypeMirror> args =
        ((DeclaredType) type).getTypeArguments();
    args.forEach(t -> {/*...*/});
}

See: , , and .Types.asElementElements.getPackageOfTypes.erasureDeclaredType.getTypeArguments

Related


How to get field name and field type from generic type in Scala?

Code: In Scala, given a generic type T, how do I retrieve a list of field names and field types? For example, if I have case class: case class Person(name: String, age: Int, gender: Boolean) and the generic function: def getFieldNamesAndTypes[T](): Seq[(Strin

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field type without any annotation in annotation processor

Laniz: I've been struggling with this for a while now, relying on various arcane transformations and manipulations of toString to get it to work, but there must be a proper way to do this. I have an annotation processor that looks like this: @SupportedAnnotati

How to get field name and field type from generic type in Scala?

Code: In Scala, given a generic type T, how do I retrieve a list of field names and field types? For example, if I have case class: case class Person(name: String, age: Int, gender: Boolean) and the generic function: def getFieldNamesAndTypes[T](): Seq[(Strin

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field type without any annotation in annotation processor

Laniz: I've been struggling with this for a while now, relying on various arcane transformations and manipulations of toString to get it to work, but there must be a proper way to do this. I have an annotation processor that looks like this: @SupportedAnnotati

Get field type without any annotation in annotation processor

Laniz: I've been struggling with this for a while now, relying on various arcane transformations and manipulations of toString to get it to work, but there must be a proper way to do this. I have an annotation processor that looks like this: @SupportedAnnotati

Java Annotation Processor - Field Field

User 2141889 I'm writing a custom annotation processorand I've been looking for a way to access annotatedthe fields of a class field. So I will have two classes public class InnerClass { private final String innerField = "String"; } and @CustomAnnotation

Get type of array in Java annotation processor

sokokwe I am writing an annotation processor. How to get the type of the array? @MyAnnotation int[] iArray; @MyAnnotation boolean[] bArray; @MyAnnotation FooClass[] fooArray; As far as I know, I can check if it's an array like this: if (element.asType().g

Get type of array in Java annotation processor

sokokwe I am writing an annotation processor. How to get the type of an array? @MyAnnotation int[] iArray; @MyAnnotation boolean[] bArray; @MyAnnotation FooClass[] fooArray; As far as I know, I can check if it's an array like this: if (element.asType().ge

How to get field name and field type from generic type in Scala?

Code: In Scala, given a generic type T, how do I retrieve a list of field names and field types? For example, if I have case class: case class Person(name: String, age: Int, gender: Boolean) and the generic function: def getFieldNamesAndTypes[T](): Seq[(Strin

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field type without any annotation in annotation processor

Laniz: I've been struggling with this for a while now, relying on various arcane transformations and manipulations of toString to get it to work, but there must be a proper way to do this. I have an annotation processor that looks like this: @SupportedAnnotati

Get field type without any annotation in annotation processor

Laniz: I've been struggling with this for a while now, relying on various arcane transformations and manipulations of toString to get it to work, but there must be a proper way to do this. I have an annotation processor that looks like this: @SupportedAnnotati

Get type of array in Java annotation processor

sokokwe I am writing an annotation processor. How to get the type of an array? @MyAnnotation int[] iArray; @MyAnnotation boolean[] bArray; @MyAnnotation FooClass[] fooArray; As far as I know, I can check if it's an array like this: if (element.asType().ge