Accessing type annotations at runtime

Gunnar Morling gunnar at
Thu Mar 13 16:03:46 UTC 2014

Hi Joe,

Thanks for the reply and hinting me the right direction!

I had seen these methods but was missing the down-cast to
AnnotatedParameterizedType which gives access to
getAnnotatedActualTypeArguments(). In case it's helpful to others, that's
what I ended up with:

    Field myField = ...;

    // List<String>
    AnnotatedParameterizedType type = (AnnotatedParameterizedType)

    // String
    AnnotatedType typeArg = type.getAnnotatedActualTypeArguments()[0];

    // @NotNull
    Annotation annotation = typeArg.getAnnotations()[0];

Thanks again,


2014-03-13 15:39 GMT+01:00 Joe Darcy <joe.darcy at>:

> 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