Accessing type annotations at runtime

Gunnar Morling gunnar at hibernate.org
Thu Mar 13 13:24:53 UTC 2014


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