8

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) ?

Mishax
  • 4,442
  • 5
  • 39
  • 63
  • Despite allocating a bounty I have learned nothing. I am no wiser than I was before allocating the bounty. – Mishax Apr 18 '14 at 09:04

1 Answers1

8

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.

Eugene Evdokimov
  • 2,220
  • 2
  • 28
  • 33
  • Can you provide some evidence please – Mishax Apr 16 '14 at 07:49
  • @Mishax I've just cited the specifications. I agree that there is no direct references from one to another in the context of this question. But to my mind the meaning of the description of `RetentionPolicy` values and annotation attributes in `ClassFile` structure is clear, and relationship between them are quite obvious. If you need evidences that specific JVM implementation complies with the specifications I'm afraid there is no way other than analysing of the source code of that implementation or writing tests. – Eugene Evdokimov Apr 17 '14 at 08:12
  • This is the answer; It can be verified by compiling a class that uses annotations with various RetentionPolicies and using javap to disassemble the class to show how those annotations end up in the .class file. – 80x25 Aug 04 '15 at 16:24