0

How can I get the annotation declared inside a type parameter during the annotation processing?

interface HelloWorld {
   void fooBar(List<@NonNull String> params);
}

(Assume @NonNull is a custom annotation declared with

@Retention(RUNTIME)
@Target({TYPE_USE, TYPE_PARAMETER})

How to get the @NonNull from the 'VariableElement' params during the annotation processing?

  • See https://stackoverflow.com/a/55288602/860630 for at least half of your question - in short, TYPE_USE doesn't work that way. I am not 100% certain that TYPE_PARAMETER is also limited in this way, but it does make sense, since even TypeParameterElement exposes its generic args/bounds as TypeMirrors, which cannot be annotated. Instead, I suggest checking out the compiler plugin or task listener approach to see if that gives you access beyond the simple declaration. – Colin Alworth Feb 04 '20 at 14:23
  • Thank you for pointing there! It is unfortunate that this is not exposed to the annotation processor. I thought the JSR 308 specification that added this had a requirment to add this to annotation processing as specified [here](https://dada.cs.washington.edu/types/jsr308/specification/java-annotation-design-20120703.html#asts-and-annotation-processing) – John Stuart Feb 05 '20 at 00:55
  • Right, but as the last paragraph in that section notes, the APT model must be updated to get past types and their members, and that so far only seems to exist in the form of the task listener, which can be plugged into an annotation processor. – Colin Alworth Feb 05 '20 at 01:26

0 Answers0