Accessing type annotations at runtime
gunnar at hibernate.org
Thu Mar 13 16:03:46 UTC 2014
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 = ...;
AnnotatedParameterizedType type = (AnnotatedParameterizedType)
AnnotatedType typeArg = type.getAnnotatedActualTypeArguments();
Annotation annotation = typeArg.getAnnotations();
2014-03-13 15:39 GMT+01:00 Joe Darcy <joe.darcy at oracle.com>:
> See the methods in java.lang.reflect named "getAnnotedFoo" which return
> java.lang.reflect.AnnotedType or a subinterface.
> On 3/13/2014 6:24 AM, Gunnar Morling wrote:
>> 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
>> 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,
More information about the core-libs-dev