[type-annos-observers] Storing declaration annotations on a local variable

Alex Buckley alex.buckley at oracle.com
Fri Aug 23 15:49:47 PDT 2013


For the record, it is too late for JSR 308 to consider storing 
declaration annotations on local variables.

Alex

-------- Original Message --------
Subject: Storing declaration annotations on a local variable
Date: Mon, 18 Feb 2013 01:03:26 -0800
From: Werner Dietl <wdietl at gmail.com>
To: type-annotations-spec-comments at openjdk.java.net

Java 1.5 declaration annotations on local variables can be written,
but are not stored in the bytecode.
Java 1.8 type annotations on a local variable are stored in the bytecode.
Should we store declaration annotations on local variables together
with type annotations on local variables?

Let's assume a declaration annotation @DA, a type annotation @TA, and 
this code:

class Test {
     @TA @DA Object f = null;

     void foo() {
	@TA @DA Object l = null;
     }
}

A fragment of the output of javap -v is:

   java.lang.Object f;
     descriptor: Ljava/lang/Object;
     flags:
     RuntimeInvisibleAnnotations:
       0: #8()
     RuntimeInvisibleTypeAnnotations:
       0: #10(): FIELD
   ...
   void foo();
     descriptor: ()V
     ...
     RuntimeInvisibleTypeAnnotations:
       0: #10(): LOCAL_VARIABLE, {start_pc=2, length=1, index=1}

(Where #8 is @DA and #10 is @TA.)
That is, as expected, the field has both the declaration and the type
annotation, whereas the local variable only stores the type
annotation.

Storing declaration annotations for local variables will enable more
use cases for declaration annotations and make the language more
consistent.

It should be very simple to store the declaration annotation together
with the type annotation, giving something like:

     RuntimeInvisibleTypeAnnotations:
       0: #8(): LOCAL_VARIABLE, {start_pc=2, length=1, index=1}
       1: #10(): LOCAL_VARIABLE, {start_pc=2, length=1, index=1}

The obvious disadvantage being that we would now mix type and
declaration annotations. Instead, with a bit more work, we could
introduce a RuntimeInvisibleLocalVariableAnnotation that is used just
for this purpose. It would use the same format as for type annotations
on local variables and should be straightforward to implement.

The reflection API will not be extended to expose these declaration
annotations on local variables.

What do you think of this proposal?
cu, WMD.

-- 
http://www.google.com/profiles/wdietl



More information about the type-annotations-spec-observers mailing list