[type-annos-observers] RetentionPolicy.CLASS too broad for TYPE_USE annotations

Markus Keller markus_keller at ch.ibm.com
Mon May 6 03:49:14 PDT 2013


The RetentionPolicy.CLASS looks too broad for many TYPE_USE annotations. 
For annotations that are meant to serve as API contracts, it's enough to 
include the externally visible annotations (target_type values 0x1*) in 
the class file. The internal annotations (0x4*) are not interesting for 
this use case and only bloat the class file. Neither the user who compiles 
the code nor the provider of a TYPE_USE annotation currently have the 
means to selectively enable retention only for the interesting group.

I see two possible solutions to this problem:

1) Compilers offer an opt-in mechanism to store internal annotations. 
Similar to JEP 118's proposed "-parameters" command-line switch, this 
could be called "-internalAnnotations".

2) Introduce new RetentionPolicy values CLASS_INTERNAL and 
RUNTIME_INTERNAL to tell that non-visible annotations should be stored in 
the class file as well. The existing CLASS and RUNTIME policies could be 
kept for externally visible annotations only (the main use case). The 
suffix could also be "_INCLUDING_CODE_BLOCKS".

With both solutions, e.g. an @NonNull annotation could stay at 
@Retention(RetentionPolicy.CLASS).

Markus



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