Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations - How to access from code?

Section 4.7.16 of the JVM specification includes a description of "RuntimeVisibleAnnotations". I am wondering what can cause an attribute to be included in this attributes table, is this only by applying @Retention(RetentionPolicy.RUNTIME) on an attribute? Conversely, for "RuntimeInvisibleAnnotations" (see further in 4.7.17) is this @Retention(RetentionPolicy.CLASS) only or is it also @Retention(RetentionPolicy.SOURCE) ?

like image 952
Mishax Avatar asked Apr 08 '14 11:04

Mishax


1 Answers

Compiling information from the JVM and JLS specifications gives us the following picture:

  1. Annotations meta-annotated with the @Retention whose value is RetentionPolicy.SOURCE must not be present in the binary representation of the class or interface in which they appear, i.e. they are not to be recorded in the class file at all.

  2. Annotations with the RetentionPolicy.CLASS must be represented in the binary representation of the class or interface in which they appear, unless they annotate a local variable declaration. An annotation on a local variable declaration is never retained in the binary representation.

    So this is what the RuntimeInvisibleAnnotations attribute is designed for.

    They need not be retained by the VM at run time, unless the Java Virtual Machine has been instructed to retain these annotations via some implementation-specific mechanism such as a command line flag.

  3. Annotations with the RetentionPolicy.RUNTIME are to be recorded in the class file by the compiler and must be available at run time via reflection libraries. This is for the RuntimeVisibleAnnotations attribute.

like image 161
Eugene Evdokimov Avatar answered Oct 12 '22 16:10

Eugene Evdokimov