Accessing type annotations at runtime

Joe Darcy joe.darcy at oracle.com
Thu Mar 13 14:39:34 UTC 2014


Hello,

See the methods in java.lang.reflect named "getAnnotedFoo" which return 
java.lang.reflect.AnnotedType or a subinterface.

-Joe

On 3/13/2014 6:24 AM, Gunnar Morling wrote:
> Hi,
>
> Is it possible to retrieve type annotations (as defined by JSR 308) using
> reflection at runtime? E.g. I would like to retrieve the @NotNull
> annotation from a member declared like this:
>
>      private List<@NotNull String> names;
>
> I assumed that this would be possible using reflection, but I couldn't find
> a way to do it. Is there an example for this somewhere? Or are type
> annotations only meant to be accessed by compile-time tools such as the
> Checker framework?
>
> I noticed that the byte code of my class contains information about the
> annotation, so I guess one could access it by examining the class file if
> it's really not possible via the reflection API.
>
> Many thanks,
>
> --Gunnar




More information about the core-libs-dev mailing list