Storing declaration annotations on a local variable
Werner Dietl
wdietl at gmail.com
Mon Feb 18 01:03:26 PST 2013
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-comments
mailing list