Type Annotations Specification (JSR 308)Michael D. Ernst |
The JSR 308 webpage is https://checkerframework.org/jsr308/. It contains the latest version of this document, along with other information such as a FAQ, the reference implementation, and sample annotation processors.
This document is available in PDF format at https://checkerframework.org/jsr308/specification/java-annotation-design.pdf.
JSR 308 extends Java’s annotation system [Blo04] so that annotations may appear on any use of a type. (By contrast, Java SE 7 permits annotations only on declarations; JSR 308 is backward-compatible and continues to permit those annotations.) Such a generalization removes limitations of Java’s annotation system, and it enables new uses of annotations.
This document specifies the syntax of extended Java annotations, but it makes no commitment as to their semantics. As with Java’s existing annotations [Blo04], the semantics is dependent on annotation processors (compiler plug-ins), and not every annotation is necessarily sensible in every location where it is syntactically permitted to appear. This proposal is compatible with existing annotations, such as those specified in JSR 250, “Common Annotations for the Java Platform” [Mor06], and proposed annotations, such as those to be specified in (the now-defunct) JSR 305, “Annotations for Software Defect Detection” [Pug06].
This proposal does not change the compile-time, load-time, or run-time semantics of Java. It does not change the abilities of Java annotation processors as defined in JSR 269 [Dar06]. The proposal merely makes annotations more general — and thus more useful for their current purposes, and also usable for new purposes that are compatible with the original vision for annotations [Blo04].
This document has two parts: a normative part and a non-normative part. The normative part specifies the changes to the Java language syntax (Section 2) and the class file format (Section 3). The non-normative part motivates annotations on types by presenting one possible use, type qualifiers (Appendix A), along with related work. It lists tools that must be updated to accommodate the Java and class file modifications (Appendix B), along with some requirements on those tool changes.
A JSR, or Java Specification Request, is a proposed specification for some aspect of the Java platform — the Java language, virtual machine, libraries, etc. For more details, see the Java Community Process FAQ at https://jcp.org/en/introduction/faq.
A FAQ (Frequently Asked Questions) document complements this specification; see https://checkerframework.org/jsr308/jsr308-faq.html.
In Java SE 7, annotations can be written only on method formal parameters and the declarations of packages, classes, methods, fields, and local variables. JSR 308 extends Java to allow annotations on any use of a type, and on type parameter declarations. JSR 308 does not extend Java to allow annotations on type names that are not type uses, such as the type names that appear in import statements, class literals, static member accesses, and annotation uses (see Section 2.1.2). JSR 308 uses a simple prefix syntax for type annotations, as illustrated in the below examples, with a special case for receiver types (item #5 below). In addition to supporting type annotations, JSR 308 also makes one extension to declaration annotations (item #6 below).
Map<@NonNull String, @NonEmpty List<@Readonly Document>> files;
o.<@NonNull String>m("...");
class Folder<F extends @Existing File> { ... } Collection<? super @Existing File>
class UnmodifiableList<T> implements @Readonly List<@Readonly T> { ... }
void monitorTemperature() throws @Critical TemperatureException { ... }
new @Interned MyObject() new @NonEmpty @Readonly List<String>(myNonEmptyStringSet) myVar.new @Tainted NestedClass()
For generic constructors (JLS §8.8.4), the annotation follows the explicit type arguments (JLS §15.9):
new <String> @Interned MyObject()
Map.@NonNull Entry
myString = (@NonNull String) myObject;It is not permitted to omit the Java type, as in myString = (@NonNull) myObject;.
boolean isNonNull = myString instanceof @NonNull String;It is not permitted to omit the Java type, as in myString instanceof @NonNull.
@Vernal Date::getDay List<@English String>::size Arrays::<@NonNegative Integer>sort
@Readonly Document [][] docs1 = new @Readonly Document [2][12]; // array of arrays of read-only documents Document @Readonly [][] docs2 = new Document @Readonly [2][12]; // read-only array of arrays of documents Document[] @Readonly [] docs3 = new Document[2] @Readonly [12]; // array of read-only arrays of documents
This syntax permits independent annotations for each distinct level of array, and for the elements.
class Invocation { @Immutable Invocation() { ... } ... }
(Note that the result of a constructor is different from the receiver. The receiver only exists for inner class constructors, as in theReceiver.new InnerClass(). The receiver is the containing object, and in the body of the constructor it is referred to as Supertype.this. In the constructor body, the result is referred to as this. In any non-constructor, the receiver (if any) is referred to as this.)
Outer class annotations for a constructor result must be identical to those on the receiver, so they can be inferred from the annotations on the receiver.
Only the first formal parameter may be named this, and such a formal parameter is permitted only on an instance method. It is forbidden on a static method or a lambda expression. The type of the this formal parameter must be the same as the class that contains the method and may include type arguments if that class has any. A receiver this formal parameter is also permitted on a constructor of an inner class, in which case its type is that of the class that contains the inner class.
In a method in an inner type, the receiver type can be written as (for example) either Inner or as Outer.Inner, and in the latter case annotations on both parts are possible, as in @ReadOnly Outer.@Mutable Inner. In a constructor in an inner type, the receiver has a name such as Outer.this.
The optional receiver parameter has no effect on execution — it only serves as a place to write annotations on the receiver. The compiler generates the same bytecodes, and reflection returns the same results regarding number of method formal parameters, whether or not the optional receiver parameter is present.
As an example, here are the standard definitions for toString and equals:
class MyClass { ... public String toString() { ... } public boolean equals(Object other) { ... } }
It is equivalent to write instead
class MyClass { ... public String toString(MyClass this) { ... } public boolean equals(MyClass this, Object other) { ... } }
The only purpose of writing the receiver explicitly is to make it possible to annotate the receiver’s type:
class MyClass { ... public String toString(@Readonly MyClass this) { ... } public boolean equals(@Readonly MyClass this, @Readonly Object other) { ... } }
An anonymous class has no name for its innermost class, so it is not possible to annotate the receiver of its constructor or methods. If you need to do so, re-write the anonymous class into a named class and then use a receiver parameter.
A method in an inner class (or a constructor in an inner class) has multiple receivers: one for each containing class. For a method, the receivers are named this, Outer.this, etc. Only the innermost receiver may be written as a formal parameter. This restriction does not limit expressiveness, because the annotations on the other receivers can be inferred from the annotations on the fully-qualified type of the innermost receiver, just as for outer class annotations on a constructor result.
For example, consider method innerMethod:
class Outer { class Middle { class Inner { void innerMethod(@A Outer.@B Middle.@C Inner this) { ... } } } }
It has three receivers (JLS §15.9). One is referred to as this and has type @C Inner. One is referred to as Middle.this and has type @B Middle. One is referred to as Outer.this and has type @A Outer.
A constructor has no receiver, unless the constructor is in an inner class. Within an inner class constructor, the receiver has a name such as Outer.this, and this refers to the result. (By contrast, within a method this refers to the receiver; JLS notes the two distinct uses for this.) An example of an inner constructor with both a result and a receiver annotation is:
class Outer { class Inner { @Result Inner(@Receiver Outer Outer.this, boolean b) { } } }
It is not permitted to write a declaration annotation on the receiver (this) formal parameter declaration. More specifically, it is a compile-time error if an annotation is written on the this formal parameter but is not applicable to type uses (is not meta-annotated with @Target({ElementType.TYPE_USE, ...})).
There are places that Java implicitly uses a type but does not express it in the source code. For example, consider an array type such as String[]. This array takes an int as an index, and the array can be thought of as mapping ints to Strings. However, there is no way to write a type annotation on the int type of the index. Analogously to arrays, there is no convenient way to annotate the int argument type that indexes a given List. These restrictions make it more difficult, and syntactically clumsy, to write an annotation processor that ensures proper use of array or list indices.
The Java language uses type names in three different ways: in type definitions/declarations; in type uses; and in other contexts that are not a type declaration or use. JSR 308 permits annotations on type uses (and also on type parameter declarations). JSR 308 does not support annotations on type names that syntactically look like, but are not, type uses. In the JLS grammar, type uses have a non-terminal name ending in Type.
Here are examples of such type names that are not annotatable.
An annotation use cannot itself be annotated. (An annotation declaration can be annotated by a so-called meta-annotation.) For instance, in
@MyAnnotation Object x;
there is no way to annotate the use of MyAnnotation.
It is not permitted to annotate the type name in a class literal, as in
@Even int.class // illegal! int @NonEmpty [].class // illegal!
This type name refers to a class, not a type. The expression evaluates to a Class, which does not reflect the type of the annotation, so there is no point in being able to write the annotation, which would have no effect.
It is not permitted to annotate the type name in an import statement.
import java.util.@NotAllowed Date; // illegal! import @IllegalSyntax java.util.Date; // illegal syntax
This use of a type name is not a type use and is more properly viewed as a scoping mechanism.
Static member accesses are preceded by a type name, but that type name may not be annotated:
@Illegal Outer.StaticNestedClass // illegal! @Illegal Outer.staticField // illegal!
The type name in front of a static member access is a scoping mechanism, not a use of a type — there’s nothing of type Outer in the above examples. Furthermore, since there is only one instance of any static member, the static member cannot be affected by an annotation on the name of the class being used to access it. Affecting the type of that single thing, depending on the annotation on the class name being used to access it, feels unnatural.
A static member access may itself be a type use, in which case the used type may be annotated by annotating the last component of the static member access, which is the simple name of the type being used.
Outer.@Legal StaticNestedClass x = ...; // legal
In a field access expression such as MyType.super.fieldname or a method reference expression like MyType.super::methodname, MyType is a scoping mechanism rather than a use of a type.
This section gives changes to the grammar of the Java language [GJS+12, ch. 18], organized in the same way as the rules of Section 2.1. Additions are underlined.
Infelicities in the Java grammar make this section somewhat long. Some improvements are possible (for instance, by slightly refactoring the Java grammar), but this version attempts to minimize changes to existing grammar productions.
Type: |
[Annotations] UnannType |
UnannType: |
( byte | short | char | int | long | float | double | boolean ) {[Annotations] []} |
UnannReferenceType {[Annotations] []} |
MethodOrFieldDecl: |
UnannType Identifier MethodOrFieldRest |
InterfaceMethodOrFieldDecl: |
UnannType Identifier InterfaceMethodOrFieldRest |
Resource: |
{VariableModifier} UnannReferenceType VariableDeclaratorId = Expression |
ForVarControl: |
{VariableModifier} UnannType VariableDeclaratorId ForVarControlRest |
QualifiedTypeList: |
[Annotations] UnannReferenceType { , [Annotations] UnannReferenceType } |
Annotations are permitted on the simple name in a static nested class.
ReferenceType: |
[Annotations] UnannReferenceType |
UnannReferenceType: |
Identifier [TypeArguments] { . [Annotations] Identifier [TypeArguments]} |
TypeArgument: |
[Annotations] ? [( extends | super ) Type] |
VariableDeclaratorRest: |
{[Annotations] []} [= VariableInitializer] |
VariableDeclaratorId: |
Identifier {[Annotations] []} |
MethodDeclaratorRest: |
FormalParameters {[Annotations] []} [throws QualifiedTypeList] ( Block | ; ) |
InterfaceMethodDeclaratorRest: |
FormalParameters {[Annotations] []} [throws QualifiedTypeList] ; |
ConstantDeclaratorRest: |
{[Annotations] []} [= VariableInitializer] |
Annotations may also appear on varargs (...):
FormalParameterDecls: |
{VariableModifier} UnannType FormalParameterDeclsRest |
FormalParameterDeclsRest: |
VariableDeclaratorId [, FormalParameterDecls] |
[Annotations] ... VariableDeclaratorId |
FormalParameters: |
( [FormalParameterOrReceiverDecls] ) |
FormalParameterOrReceiverDecls: |
Type [Identifier .] this [, FormalParameterDecls] |
FormalParameterDecls |
TypeParameter: |
[Annotations] Identifier [extends Bound] |
As discussed in Section A.3, it is desirable to be able to independently annotate both the base type and each distinct level of a nested array. Forbidding annotations on arbitrary levels of an array would simplify the syntax, but it would reduce expressiveness to an unacceptable degree. The syntax of array annotations follows the same general prefix rule as other annotations — it looks slightly different because the syntax of array types is different than the syntax of other Java types. (Arrays are less commonly used than generics, so even if you don’t like the array syntax, it need not bother you in most cases.)
Most programmers read the Java type String[][] as “array of arrays of Strings”. Analogously, the Java expression new String[2][5] is “new length-2 array of length-5 array of Strings”. After a = new String[2][5], a is an array with 2 elements, and a[1] is a 5-element array.
In other words, the order in which a programmer reads an array type or expression is left-to-right for the brackets, then left-to-right for the base type.
type: String [] [] order of reading: 2-------------> 1 ------------------------>
To more fully describe the 2x5 array, a programmer could use the type “length-2 array of length-5 array of Strings”:
type: String @Length(2) [] @Length(5) [] order of reading: 2-------------> 1 ------------------------>
The prefix notation is natural, because the type is read in exactly the same order as any Java array type. As another example, to express “non-null array of length-10 arrays of English Strings” a programmer would write
type: @English String @NonNull [] @Length(10) [] order of reading: 2-------------> 1 ----------------------->
An important property of this syntax is that, in two declarations that differ only in the number of array levels, the annotations mean the same thing. For example, var1 has the same annotations as the elements of arr2:
@NonNull String var1; @NonNull String[] arr2;
because in each case @NonNull refers to the String, not the array. This consistency is especially important since the two variables may appear in a single declaration:
@NonNull String var1, arr2[];
A potential criticism is that a type annotation at the very beginning of a declaration does not refer to the full type, even though declaration annotations (which also occur at the beginning of the declaration) do refer to the entire variable. As an example, in @NonNull String[] arr2; the variable arr2 is not non-null. This is actually a criticism of the fact that in a Java declaration such as String[] arr2;, the top-level type constructor does not appear on the far left. An annotation on the whole type (the array) should appear on the syntax that indicates the array — that is, on the brackets.
Other array syntaxes can be imagined, but they are less consistent with Java syntax and therefore harder to read and write. Examples include making annotations at the beginning of the type refer to the whole type, using a postfix syntax rather than a prefix syntax, and postfix syntax within angle brackets as for generics.
Java uses the @Target meta-annotation as a machine-checked way of expressing where an annotation is intended to appear. The ElementType enum classifies the places an annotation may appear in a Java program. JSR 308 adds two new constants to the ElementType enum.
ElementType.TYPE_PARAMETER and ElementType.TYPE_USE are distinct from the existing ElementType.TYPE enum element of Java SE 7, which indicates that an annotation may appear on a type declaration (a class, interface, or enum declaration). The program elements denoted by ElementType.TYPE_PARAMETER, ElementType.TYPE_USE, and ElementType.TYPE are disjoint (except for the special cases about class and type parameter declarations, noted above).
As an example, in this declaration:
@Target(ElementType.TYPE_USE) public @interface NonNull { ... }
the @Target(ElementType.TYPE_USE) meta-annotation indicates that @NonNull may appear on any use of a type.
If an annotation is not meta-annotated with @Target (which would be poor style!), then the compiler treats the annotation as if it is meta-annotated with all of the ElementType enum constants that appear in Java 7: ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, and TYPE.
As in Java SE 7, the compiler issues an error if a programmer places an annotation in a location not permitted by its @Target meta-annotation. (The compiler issues the error even if no annotation processor is being run.)
A programmer could write a @Target meta-annotation indicating that an annotation is both a type annotation and a declaration annotation. We have not found an example where such a meta-annotation is desirable; although it is legal, it is considered bad style. For example, suppose that a programmer wrote
@Target({ ElementType.TYPE_USE, ElementType.METHOD }) public @interface SillyAnnotation { ... }
If @SillyAnnotation were written before a non-void method, then the annotation would apply to both the return type and the method declaration. The annotation appears twice in the AST during annotation processing, and it appears twice in the classfile. A tool that reads a classfile and writes Java-like output, such as Javadoc or a decompiler, must take care not to write an annotation twice in the decompiled code, as in “@SillyAnnotation @SillyAnnotation int m()…”. If you write @SillyAnnotation on a void-returning method, then it applies only to the declaration, because it is not written in a type annotation position.
This section defines how to store type annotations in a Java class file. It also defines how to store local variable annotations, which are permitted in Java SE 7 source code but are discarded by the compiler.
The class file stores only annotations that are explicitly written in the Java source file. Annotations that are inferred are not stored in the class file. One location where Java infers types is the “diamond” used in class instance creation expressions (JLS §15.9). Lambda leads to other examples. The inference of type annotations can depend on the annotation processor, and it would be undesirable for the annotations in the classfile to depend on whether a given annotation processor is being run.
The class file format represents the type of every variable and expression in a Java class, including all temporaries and values stored on the stack. (Sometimes the representation is explicit, such as via the StackMapTable attribute, and sometimes it is implicit.) Since JSR 308 permits annotations to be added to a type, the class file format should be updated to continue to represent the full, annotated type of each expression.
More pragmatically, Java annotations must be stored in the class file for two reasons.
First, annotated signatures (public members) must be available to tools that read class files. For example, a type-checking compiler plug-in [Dar06, PAC+08] needs to read annotations when compiling a client of the class file. The Checker Framework (https://checkerframework.org/) is one way to create such plug-ins.
Second, annotated method bodies must be present to permit checking the class file against the annotations. This is necessary to give confidence in an entire program, since its parts (class files) may originate from any source. Otherwise, it would be necessary to simply trust annotated classes of unknown provenance [BHP07].
In Java SE 7, an annotation is stored in the class file in an attribute [Blo04, LBBY12]. An attribute associates data with a program element (a method’s bytecodes, for instance, are stored in a Code attribute of the method). The RuntimeVisibleParameterAnnotations attribute stores formal parameter annotations that are accessible at runtime using reflection, and the RuntimeInvisibleParameterAnnotations attribute stores formal parameter annotations that are not accessible at runtime. RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations are analogous, but for annotations on fields, methods, and classes.
These attributes contain arrays of annotation structure elements, which in turn contain arrays of element_value pairs. The element_value pairs store the names and values of an annotation’s arguments.
Annotations on a field are stored as attributes of the field’s field_info structure [LBBY12, §4.6]. Annotations on a method are stored as attributes of the method’s method_info structure [LBBY12, §4.7]. Annotations on a class are stored as attributes of the class’s attributes structure [LBBY12, §4.2].
Generic type information is stored in a different way in the class file, in a signature attribute. Its details are not germane to the current discussion.
JSR 308 introduces two new attributes: RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations. These attributes are structurally identical to the RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations attributes described above with one exception: rather than an array of annotation elements, RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations contain an array of type_annotation elements, which are described in Section 3.1.
Runtime[In]VisibleTypeAnnotations_attribute { u2 attribute_name_index; // "Runtime[In]VisibleTypeAnnotation" u4 attribute_length; u2 num_annotations; type_annotation annotations[num_annotations]; }
A type annotation is stored in a Runtime[In]visibleTypeAnnotations attribute on the smallest enclosing class, field, method, or Code structure. Type annotations in the body of an initializer appear with the code that performs the initialization, not on the field that is being initialized. Type annotations in the body of instance initializer appear on on all initial constructors, and type annotations in the body of a class initializer appear on the clinit symbol.
A type annotation written on a declaration’s type (e.g., on a field type or on a method return type) appears in the Runtime[In]visibleTypeAnnotations attribute of that declaration. JSR 308 does not add a Runtime[In]visibleTypeParameterAnnotations attribute; annotations on type parameters are stored with the method or class.
In the annotations array, annotations that target instructions must be sorted in increasing order of bytecode offset. Other annotations can be in any order, and may be interleaved with instruction annotations. Annotations that target instructions are those in Figure 1 with target_type ≥ 0x40; see below for details.
No changes are made to the StackMapTable that stores the types of elements on the stack. No changes are made to the Exception table. Local variable type annotations are stored in the class file, see Section 3.3.7.
The VM does not have to validate all the constraints in Section 3.3, such as that offsets point to the right type of instruction. The VM does not do format checking (JVMS 4.8) on attributes used solely for reflection.
The JSR 308 changes apply to class file version 52 and higher.
For backward compatibility, JSR 308 uses new attributes for storing the type annotations. In other words, JSR 308 merely reserves the names of a few new attributes and specifies their layout. JVMs ignore unknown attributes. JSR 308 does not alter the way that existing annotations on classes, methods, method formal parameters, and fields are stored in the class file. JSR 308 mandates no changes to the processing of existing annotation locations; in the absence of other changes to the class file format, class files generated from programs that use no new annotations will be identical to those generated by a standard Java SE 7 compiler. Furthermore, the bytecode array will be identical between two programs that differ only in their annotations. Attributes have no effect on the bytecode array, because they exist outside it; however, they can represent properties of it by referring to the bytecode (including referring to specific instructions, or bytecode offsets).
The type_annotation structure has the following format:
type_annotation { // New fields in JSR 308: u1 target_type; // the type of the targeted program element, see Section 3.2 union { type_parameter_target; supertype_target; type_parameter_bound_target; empty_target; method_formal_parameter_target; throws_target; localvar_target; catch_target; offset_target; type_argument_target; method_reference_target; } target_info; // identifies the targeted program element, see Section 3.3 type_path target_path; // identifies targeted type in a compound type (array, generic, etc.), see Section 3.4 // Original fields from "annotation" structure: u2 type_index; u2 num_element_value_pairs; { u2 element_name_index; element_value value; } element_value_pairs[num_element_value_pairs]; }
We first briefly recap the three fields of annotation [LBBY12, §4.8.15].
Compared to annotation, the type_annotation structure contains two additional fields. These fields implement a discriminated (tagged) union type: field target_type is the tag (see Section 3.2), and its value determines the size and contents of target_info (see Section 3.3). Section 3.4 describes how the classfile indicates a part of a compound type.
The target_type field denotes the type of program element that the annotation targets, such as whether the annotation is on a field, a method receiver, a cast, or some other location. Figure 1 gives the value of target_type for every possible annotation location.
Targets for type parameter declarations (ElementType.TYPE_PARAMETER): Annotation target TargetType enum constant target_type value target_info definition class type parameter CLASS_TYPE_PARAMETER 0x00 §3.3.1 method type parameter METHOD_TYPE_PARAMETER 0x01 §3.3.1 Targets for type uses that may be externally visible in classes and members (ElementType.TYPE_USE): Annotation target TargetType enum constant target_type value target_info definition class extends/implements CLASS_EXTENDS 0x10 §3.3.2 class type parameter bound CLASS_TYPE_PARAMETER_BOUND 0x11 §3.3.3 method type parameter bound METHOD_TYPE_PARAMETER_BOUND 0x12 §3.3.3 field type FIELD 0x13 §3.3.4 method return type METHOD_RETURN 0x14 §3.3.4 method receiver type METHOD_RECEIVER 0x15 §3.3.4 method formal parameter type METHOD_FORMAL_PARAMETER 0x16 §3.3.5 exception type in throws THROWS 0x17 §3.3.6 Targets for type uses that occur only within code blocks (ElementType.TYPE_USE): Annotation target TargetType enum constant target_type value target_info definition local variable type LOCAL_VARIABLE 0x40 §3.3.7 resource variable type RESOURCE_VARIABLE 0x41 §3.3.7 exception parameter type EXCEPTION_PARAMETER 0x42 §3.3.8 type test (instanceof) INSTANCEOF 0x43 §3.3.9 object creation (new) NEW 0x44 §3.3.9 constructor reference receiver CONSTRUCTOR_REFERENCE 0x45 §3.3.9 method reference receiver METHOD_REFERENCE 0x46 §3.3.9 cast CAST 0x47 §3.3.10 type argument in constructor call CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT 0x48 §3.3.10 type argument in method call METHOD_INVOCATION_TYPE_ARGUMENT 0x49 §3.3.10 type argument in constructor reference CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT 0x4A §3.3.10 type argument in method reference METHOD_REFERENCE_TYPE_ARGUMENT 0x4B §3.3.10
target_info is a structure that contains enough information to uniquely identify the target of a given annotation. A different target_type may require a different set of fields, so the structure of the target_info is determined by the value of target_type.
All indexes count from zero.
See Section 3.4 for indicating a specific part of a compound type: a parameterized, wildcard, array, or nested type.
When the annotation’s target is a type parameter of a class or method, target_info contains one type_parameter_target:
type_parameter_target { u1 type_parameter_index; };
type_parameter_index specifies the 0-based index of the type parameter.
A Runtime[In]visibleTypeAnnotations attribute containing a type_parameter_target appears in the attributes table of a method_info structure if it targets a method type parameter, otherwise, in the attributes table of a ClassFile structure if it targets a class declaration type parameter.
When the annotation’s target is a type in an extends or implements clause, target_info contains one supertype_target:
supertype_target { u2 supertype_index; };
supertype_index is -1 (65535) if the annotation is on the superclass type. Otherwise, supertype_index specifies the 0-based index of the targeted type in the interfaces array field of the ClassFile structure; simply the value i is used if the annotation is on the ith superinterface type.
A Runtime[In]visibleTypeAnnotations attribute containing a supertype_target appears in the attributes table of a ClassFile structure.
When the annotation’s target is a bound of a type parameter of a class or method, target_info contains one type_parameter_bound_target:
type_parameter_bound_target { u1 type_parameter_index; u1 bound_index; };
type_parameter_index specifies the index of the type parameter, while bound_index specifies the index of the bound. Indexes start at 0. Bound index 0 is always a class, not interface, type. If the programmer-supplied upper bound of the type variable is an interface, it is treated as the second bound, and the implicit first bound is java.lang.Object.
Consider the following example:
<T extends @A Object & @B Comparable, U extends @C Cloneable>
Here @A has type_parameter_index 0 and bound_index 0, @B has type_parameter_index 0 and bound_index 1, and @C has type_parameter_index 1 and bound_index 1.
A Runtime[In]visibleTypeAnnotations attribute containing a type_parameter_bound_target appears in the attributes table of a method_info structure if it targets a method type parameter bound, otherwise, in the attributes table of a ClassFile structure if it targets a class declaration type parameter bound.
When the type annotation’s target is a method return type, a constructor result (which is stored in the same place, using the METHOD_RETURN enum of TargetType), a receiver type (for an instance method or for an inner class constructor), or a field, target_info is empty. More formally, it contains empty_target:
empty_target { };
A Runtime[In]visibleTypeAnnotations attribute targeting a field type appears in the attributes table of a field_info structure. A Runtime[In]visibleTypeAnnotations attribute targeting a method return type, constructor result type, or receiver appears in the attributes table of a method_info structure.
When the annotation’s target is a method formal parameter type, target_info contains one method_formal_parameter_target, which indicates which of the method’s formal parameters is being annotated:
method_formal_parameter_target { u1 method_formal_parameter_index; };
The index indicates the ith formal parameter type of the method descriptor of the enclosing method_info structure.
A Runtime[In]visibleTypeAnnotations attribute containing a method_formal_parameter_target appears in the attributes table of a method_info structure.
When the annotation’s target is a type in a throws clause, target_info contains one throws_target:
throws_target { u2 throws_type_index; };
throws_type_index specifies the index of the exception type in the clause in exception_index_table of Exceptions_attribute; simply the value i denotes an annotation on the ith exception type.
A Runtime[In]visibleTypeAnnotations attribute containing a throws_target appears in the attributes table of a method_info structure.
When the annotation’s target is a local variable or resource variable type, target_info contains one localvar_target:
localvar_target { u2 table_length; { u2 start_pc; u2 length; u2 index; } table[table_length]; };
The table_length field specifies the number of entries in the table array; multiple entries are necessary because a compiler is permitted to break a single variable into multiple live ranges with different local variable indices. The start_pc and length fields specify the variable’s live range in the bytecodes of the local variable’s containing method (from offset start_pc, inclusive, to offset start_pc + length, exclusive). The index field stores the local variable’s index in that method. These fields are similar to those of the optional LocalVariableTable attribute [LBBY12, §4.8.12].
Storing local variable type annotations in the class file raises certain challenges. For example, live ranges are not isomorphic to local variables. Note that a local variable with no live range might not appear in the class file; that is OK, because it is irrelevant to the program.
A Runtime[In]visibleTypeAnnotations attribute containing a localvar_target appears in the attributes table of a Code attribute.
When the annotation’s target is an exception parameter (the exception type in a catch statement), target_info indicates an offset in the exception table (which appears in the exception_table slot of the Code attribute).
catch_target { u2 exception_table_index; };
A Runtime[In]visibleTypeAnnotations attribute containing a catch_target appears in the attributes table of a Code attribute.
When the annotation’s target is an instanceof expression, a new expression, or a method or constructor reference receiver, target_info contains one offset_target:
offset_target { u2 offset; };
The offset field denotes the offset (i.e., within the bytecodes of the containing method) of the instanceof bytecode emitted for the type tests, the new bytecode emitted for the object creation expression, or the instruction that implements a method/constructor reference. These annotations are attached to a single bytecode, not a bytecode range (or ranges): the annotation provides information about the type of a single value, not about the behavior of a code block.
A Runtime[In]visibleTypeAnnotations attribute containing an offset_target appears in the attributes table of a Code attribute.
When the annotation’s target is a cast or a type argument in a constructor/method call/reference, target_info contains one type_argument_target:
type_argument_target { u2 offset; u1 type_argument_index; };
The offset field denotes the offset (i.e., within the bytecodes of the containing method) of the new bytecode emitted for constructor call, or the instruction that implements a method invocation or a method reference. These annotations are attached to a single bytecode, not a bytecode range.
type_argument_index specifies the index of the type argument in the expression.
For annotated casts, the attribute may be attached to a checkcast bytecode, or to any other bytecode. The rationale for this is that the Java compiler is permitted to omit checkcast bytecodes for casts that are guaranteed to be no-ops. For example, a cast from String to @NonNull String may be a no-op for the underlying Java type system (which sees a cast from String to String). If the compiler omits the checkcast bytecode, the @NonNull attribute would be attached to the (last) bytecode that creates the target expression instead. This approach permits code generation for existing compilers to be unaffected.
If the compiler eliminates an annotated cast, it is required to retain the annotations on the cast in the class file. When a cast is removed, the compiler may need to adjust (the locations of) the annotations, to account for the relationship between the expression’s type and the casted-to type. Consider:
class C<S, T> { ... } class D<A, B> extends C<B, A> { ... } ... ... (C<@A1 X, @A2 Y>) myD ...
The compiler may leave out the upcast, but in that case it must record that @A1 is attached to the second type argument of D, even though it was originally attached to the first type argument of C.
A Runtime[In]visibleTypeAnnotations attribute containing a type_argument_target appears in the attributes table of a Code attribute.
TODO: type_argument_index specifies the index of a type argument in the list of explicit non-wildcard type arguments passed to the constructor/method invocation. e.g. x.<@Foo String>m() e.g. new <@Foo String> @Bar A()
@A Map<@B ? extends @C String, @D List<@E Object>>
@I String @F [] @G [] @H []
@M O1.@L O2.@K O3.@J NestedStatic
Anno. type_path @A path_length: 0; path: [] @B path_length: 1; path: [TYPE_ARGUMENT(3, 0)] @C path_length: 2; path: [TYPE_ARGUMENT(3, 0), WILDCARD(2, 0)] @D path_length: 1; path: [TYPE_ARGUMENT(3, 1)] @E path_length: 2; path: [TYPE_ARGUMENT(3, 1), TYPE_ARGUMENT(3, 0)] @F path_length: 0; path: [] @G path_length: 1; path: [ARRAY(0, 0)] @H path_length: 2; path: [ARRAY(0, 0), ARRAY(0, 0)] @I path_length: 3; path: [ARRAY(0, 0), ARRAY(0, 0), ARRAY(0, 0)] @J path_length: 3; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0), INNER_TYPE(1, 0)] @K path_length: 2; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0)] @L path_length: 1; path: [INNER_TYPE(1, 0)] @M path_length: 0; path: [] @A Map<@B Comparable<@F Object @C [] @D [] @E []>, @G List<@H Document>>
Anno. type_path @A path_length: 0; path: [] @B path_length: 1; path: [TYPE_ARGUMENT(3, 0)] @C path_length: 2; path: [TYPE_ARGUMENT(3, 0), TYPE_ARGUMENT(3, 0)] @D path_length: 3; path: [TYPE_ARGUMENT(3, 0), TYPE_ARGUMENT(3, 0), ARRAY(0, 0)] @E path_length: 4; path: [TYPE_ARGUMENT(3, 0), TYPE_ARGUMENT(3, 0), ARRAY(0, 0), ARRAY(0, 0)] @F path_length: 5; path: [TYPE_ARGUMENT(3, 0), TYPE_ARGUMENT(3, 0), ARRAY(0, 0), ARRAY(0, 0), ARRAY(0, 0)] @G path_length: 1; path: [TYPE_ARGUMENT(3, 1)] @H path_length: 2; path: [TYPE_ARGUMENT(3, 1), TYPE_ARGUMENT(3, 0)] @H O1.@E O2<@F S, @G T>.@D O3.@A Nested<@B U, @C V>
Anno. type_path @A path_length: 3; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0), INNER_TYPE(1, 0)] @B path_length: 4; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0), INNER_TYPE(1, 0), TYPE_ARGUMENT(3, 0)] @C path_length: 4; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0), INNER_TYPE(1, 0), TYPE_ARGUMENT(3, 1)] @D path_length: 2; path: [INNER_TYPE(1, 0), INNER_TYPE(1, 0)] @E path_length: 1; path: [INNER_TYPE(1, 0)] @F path_length: 2; path: [INNER_TYPE(1, 0), TYPE_ARGUMENT(3, 0)] @G path_length: 2; path: [INNER_TYPE(1, 0), TYPE_ARGUMENT(3, 1)] @H path_length: 0; path: []
The type_path structure identifies a part of a compound type. In a compound type (a parameterized, wildcard, array, or nested type), there are multiple places that an annotation may appear. For example, consider the difference among @X Map<String, Object>, Map<@X String, Object>, and Map<String, @X Object>; or the difference among Foo<@X ? extends T> and Foo<? extends @X T>; or the difference among @X String [] [], String @X [] [], and String [] @X []; or the difference among @X Outer.Inner and Outer.@X Inner. The type_path structure distinguishes among these locations.
struct type_path { u1 path_length; type_path_entry path[path_length]; } struct type_path_entry { u1 type_path_kind; // 0: annotation is deeper in this array type // 1: annotation is deeper in this nested type // 2: annotation is on the bound of this wildcard type arg // 3: annotation is on the i'th type arg of this parameterized type u1 type_argument_index; // 0: ignore me // non-0: the 1st, 2nd, etc type arg of this parameterized type }
When type_path_kind is 0, 1, or 2, then type_argument_index must be 0.
If the compound type is viewed as a tree, then the type_path structure represents a path in that tree.
Figure 2 shows some examples.
The type_path that is stored in the class file is with respect to the full type, not the source code representation. In the source code, a type can be abbreviated or partially written out, such as a nested type written without the outer type or a raw type written without the type arguments.
In the class file, all annotations on the full type appear. For example, the classfile would record the type in this new expression as @A Outer.@B Inner:
@A Outer f1 = ... ... f1.new @B Inner() ...
One example use of annotation on types is to create custom type qualifiers for Java, such as @NonNull, @ReadOnly, @Interned, or @Tainted. Type qualifiers are modifiers on a type; a declaration that uses a qualified type provides extra information about the declared variable. A designer can define new type qualifiers using Java annotations, and can provide compiler plug-ins to check their semantics (for instance, by issuing lint-like warnings during compilation). A programmer can then use these type qualifiers throughout a program to obtain additional guarantees at compile time about the program.
The type system defined by the type qualifiers does not change Java semantics, nor is it used by the Java compiler or run-time system. Rather, it is used by the checking tool, which can be viewed as performing type-checking on this richer type system. (The qualified type is usually treated as a subtype or a supertype of the unqualified type.) As an example, a variable of type Boolean has one of the values null, TRUE, or FALSE (more precisely, it is null or it refers to a value that is equal to TRUE or to FALSE). A programmer can depend on this, because the Java compiler guarantees it. Likewise, a compiler plug-in can guarantee that a variable of type @NonNull Boolean has one of the values TRUE or FALSE (but not null), and a programmer can depend on this. Note that a type qualifier such as @NonNull refers to a type, not a variable, though JSR 308 could be used to write annotations on variables as well.
Type qualifiers can help prevent errors and make possible a variety of program analyses. Since they are user-defined, developers can create and use the type qualifiers that are most appropriate for their software.
A system for custom type qualifiers requires extensions to Java’s annotation system, described in this document; the existing Java SE 7 annotations are inadequate. Similarly to type qualifiers, other pluggable type systems [Bra04] and similar lint-like checkers also require these extensions to Java’s annotation system.
Our key goal is to create a type qualifier system that is compatible with the Java language, VM, and toolchain. Previous proposals for Java type qualifiers are incompatible with the existing Java language and tools, are too inexpressive, or both. The use of annotations for custom type qualifiers has a number of benefits over new Java keywords or special comments. First, Java already implements annotations, and Java SE 7 features a framework for compile-time annotation processing. This allows JSR 308 to build upon existing stable mechanisms and integrate with the Java toolchain, and it promotes the maintainability and simplicity of the modifications. Second, since annotations do not affect the runtime semantics of a program, applications written with custom type qualifiers are backward-compatible with the vanilla JDK. No modifications to the virtual machine are necessary.
Four compiler plug-ins that perform type qualifier type-checking, all built using JSR 308, are distributed at the JSR 308 webpage, https://checkerframework.org/jsr308/. The four checkers, respectively, help to prevent and detect null pointer errors (via a @NonNull annotation), equality-checking errors (via a @Interned annotation), mutation errors (via the Javari [BE04, TE05] type system), and mutation errors (via the IGJ [ZPA+07] type system). A paper [PAC+08] discusses experience in which these plug-ins exposed bugs in real programs.
The ability to place annotations on arbitrary occurrences of a type improves the expressiveness of annotations, which has many benefits for Java programmers. Here we mention just one use that is enabled by extended annotations, namely the creation of type qualifiers. (Figure 3 gives an example of the use of type qualifiers.)
As an example of how JSR 308 might be used, consider a @NonNull type qualifier that signifies that a variable should never be assigned null [Det96, Eva96, DLNS98, FL03, CMM05]. A programmer can annotate any use of a type with the @NonNull annotation. A compiler plug-in would check that a @NonNull variable is never assigned a possibly-null value, thus enforcing the @NonNull type system.
@Readonly and @Immutable are other examples of useful type qualifiers [ZPA+07, BE04, TE05, GF05, KT01, SW01, PBKM00]. Similar to C’s const, an object’s internal state may not be modified through references that are declared @Readonly. A type qualifier designer would create a compiler plug-in (an annotation processor) to check the semantics of @Readonly. For instance, a method may only be called on a @Readonly object if the method was declared with a @Readonly receiver. (Each non-static method has an implicit formal parameter, this, which is called the receiver.) @Readonly’s immutability guarantee can help developers avoid accidental modifications, which are often manifested as run-time errors. An immutability annotation can also improve performance. The Access Intents mechanism of WebSphere Application Server already incorporates such functionality: a programmer can indicate that a particular method (or all methods) on an Enterprise JavaBean is readonly.
Additional examples of useful type qualifiers abound. We mention just a few others. C uses the const, volatile, and restrict type qualifiers. Type qualifiers YY for two-digit year strings and YYYY for four-digit year strings helped to detect, then verify the absence of, Y2K errors [EFA99]. Expressing units of measurement (e.g., SI units such as meter, kilogram, second) can prevent errors in which a program mixes incompatible quantities; units such as dollars can prevent other errors. Range constraints, also known as ranged types, can indicate that a particular int has a value between 0 and 10; these are often desirable in realtime code and in other applications, and are supported in languages such as Ada and Pascal. Type qualifiers can indicate data that originated from an untrustworthy source [PØ95, VS97]; examples for C include user vs. kernel indicating user-space and kernel-space pointers in order to prevent attacks on operating systems [JW04], and tainted for strings that originated in user input and that should not be used as a format string [STFW01]. A localizable qualifier can indicate where translation of user-visible messages should be performed. Annotations can indicate other properties of its contents, such as the format or encoding of a string (e.g., XML, SQL, human language, etc.). Local and remote qualifiers can indicate whether particular resources are available on the same machine or must be retrieved over the network. An interned qualifier can indicate which objects have been converted to canonical form and thus may be compared via reference equality. Type qualifiers such as unique and unaliased can express properties about pointers and aliases [Eva96, CMM05]; other qualifiers can detect and prevent deadlock in concurrent programs [FTA02, AFKT03]. A ThreadSafe qualifier [GPB+06] could indicate that a given field should contain a thread-safe implementation of a given interface; this is more flexible than annotating the interface itself to require that all implementations must be thread-safe. Annotations can identify performance characteristics or goals; for example, some collections should not be iterated over, and others should not be used for random access. Annotations (both type qualifiers and others) can specify cut points in aspect-oriented programming (AOP) [EM04]. Flow-sensitive type qualifiers [FTA02] can express typestate properties such as whether a file is in the open, read, write, readwrite, or closed state, and can guarantee that a file is opened for reading before it is read, etc. The Vault language’s type guards and capability states are similar [DF01].
The Checker Framework (https://checkerframework.org/) gives a way to create pluggable type-checkers. A pluggable type-checker verifies absence of certain bugs (and also verifies correct usage of type qualifiers). The Checker Framework is distributed with a set of example type-checkers. The Checker Framework is built on the Type Annotations (JSR 308) syntax, though it also permits annotations to be written in comments for compatibility with previous versions of Java.
This section gives examples of annotations that a programmer may wish to place on a type. Each of these uses is either impossible or extremely inconvenient in the absence of the new locations for annotations proposed in this document. For brevity, we do not give examples of uses for every type annotation. The specific annotation names used in this section, such as @NonNull, are examples only; this document does not define any annotations, merely specifying where they can appear in Java code.
It is worthwhile to permit annotations on all uses of types (even those for which no immediate use is apparent) for consistency, expressiveness, and support of unforeseen future uses. An annotation need not utilize every possible annotation location. For example, a system that fully specifies type qualifiers in signatures but infers them for implementations [GF05] may not need annotations on casts, object creation, local variables, or certain other locations. Other systems may forbid top-level (non-type-argument, non-array) annotations on object creation (new) expressions, such as new @Interned Object().
Generic collection classes are declared one level at a time, so it is easy to annotate each level individually.
It is desirable that the syntax for arrays be equally expressive. Here are examples of uses for annotations on array levels:
A simple example is a method that accepts a list of files to search. null may be used to indicate that there were no files to search, but when a list is provided, then the Files themselves must be non-null. Using JSR 308, a programmer would declare the formal parameter as @NonNull File @Nullable [] filesToSearch — more concisely, depending on the default nullness, as either File @Nullable [] filesToSearch or @NonNull File [] filesToSearch
The opposite example, of a non-null array with nullable elements, is typical of fields in which, when an array element is no longer relevant, it is set to null to permit garbage collection.
A type qualifier on a formal parameter is a contract regarding what the method may (or may not) do with that formal parameter. Since the method receiver (this) is an implicit formal parameter, programmers should be able to express type qualifiers on it, for consistency and expressiveness. An annotation on the receiver is a contract regarding what the method may (or may not) do with its receiver.
For example, consider the following method:
package javax.xml.bind; class Marshaller { void marshal(@Readonly Marshaller this, @Readonly Object jaxbElement, @Mutable Writer writer) { ... } }
The annotations indicate that marshal modifies its second formal parameter but does not modify its first formal parameter nor its receiver.
The syntax also permits expressing constraints on the generic type parameters of the receiver. Here are some examples:
class Collection<E> { // The elements of the result have the same annotation as the elements // of the receiver. (In fact, they are the same elements.) public @PolyNull Object[] toArray(Collection<@PolyNull E> this) { ... } } interface List<T> { // The size() method changes neither the receiver nor any of the elements. public int size(@Readonly List<@Readonly T> this) { ... } } class MyMap<T,U> { // The map's values must be non-null, but the keys may be arbitrary. public void requiresNonNullValues(MyMap<T, @NonNull U> this) { ... } }
A method in an inner class has two this formal parameters: that for the inner class, and that for the enclosing instance (which is this in the currently-executing method in the outer class). It is desirable to specify the types for both. One use case for this is a read-only type system, where you want to make the distinction between the outer and inner object. This can be specified as
void m(@ReadOnly Outer.@ReadWrite Inner this) {}
A receiver annotation is different than a class annotation, a method annotation, or a return value annotation:
Since a receiver annotation is distinct from other annotations, new syntax is required for the receiver annotation.
As with Java’s annotations on formal parameters, annotations on the receiver do not affect the Java signature, compile-time resolution of overloading, or run-time resolution of overriding. The Java type of every receiver in a class is the same — but their annotations, and thus their qualified type in a type qualifier framework, may differ.
Some people question the need for receiver annotations. In case studies [PAC+08], every type system required some receiver annotations. Even the Nullness type system required them to express whether the receiver was fully initialized (only in a fully-initialized object can fields be guaranteed to be non-null). So, the real question is how to express receiver annotations, not whether they should exist.
There are two distinct reasons to annotate the type in a type cast: to fully specify the casted type (including annotations that are retained without change), or to indicate an application-specific invariant that is beyond the reasoning capability of the Java type system. Because a user can apply a type cast to any expression, a user can annotate the type of any expression. (This is different than annotating the expression itself, which is not legal.)
@Readonly Object x; ... (@Readonly Date) x ...
the cast preserves the annotation part of the type and changes only the Java type. If a cast could not be annotated, then a cast would remove the annotation:
@Readonly Object x; ... (Date) x ... // annotation processor issues warning due to casting away @Readonly
This cast changes the annotation; it uses x as a non-@Readonly object, which changes its type and would require a run-time mechanism to enforce type safety.
An annotation processor could permit the unannotated cast syntax but implicitly add the annotation, treating the cast type as @Readonly Date. This has the advantage of brevity, but the disadvantage of being less explicit and of interfering somewhat with the second use of cast annotations. Experience will indicate which design is better in practice.
As a trivial example, the following cast changes the annotation but is guaranteed to be safe at run time:
final Object x = new Object(); ... (@NonNull Object) x ...
An annotation processing tool could trust such type casts, perhaps issuing a warning to remind users to verify their safety by hand or in some other manner. An alternative approach would be to check the type cast dynamically, as Java casts are, but we do not endorse such an approach, because annotations are not intended to change the run-time behavior of a Java program and because there is not generally a run-time representation of the annotations.
Annotations on type tests (instanceof) allow the programmer to specify the full type, as in the first justification for annotations on type casts, above. However, the annotation is not tested at run time — the JVM only checks the base Java type. In the implementation, there is no run-time representation of the annotations on an object’s type, so dynamic type test cannot determine whether an annotation is present. This abides by the intention of the Java annotation designers, that annotations should not change the run-time behavior of a Java program.
Annotation of the type test permits the idiom
if (x instanceof MyType) { ... (MyType) x ... }
to be used with the same annotated type T in both occurrences. By contrast, using different types in the type test and the type cast might be confusing.
To prevent confusion caused by incompatible annotations, an annotation processor could require the annotation parts of the operand and the type to be the same:
@Readonly Object x; if (x instanceof Date) { ... } // error: incompatible annotations if (x instanceof @Readonly Date) { ... } // OK Object y; if (y instanceof Date) { ... } // OK if (y instanceof @NonNull Date) { ... } // error: incompatible annotations
(As with type casts, an annotation processor could implicitly add a missing annotation; this would be more concise but less explicit, and experience will dictate which is better for users.)
As a consequence of the fact that the annotation is not checked at run time, in the following
if (x instanceof @A1 T) { ... } else if (x instanceof @A2 T) { ... }
the second conditional is always dead code. An annotation processor may warn that one or both of the instanceof tests is a compile-time type error.
A non-null qualifier is a special case because it is possible to check at run time whether a given value can have a non-null type. A type-checker for a non-null type system could take advantage of this fact, for instance to perform flow-sensitive type analysis in the presence of a x != null test, but JSR 308 makes no special allowance for it.
Java’s new operator indicates the type of the object being created. As with other Java syntax, programmers should be able to indicate the full type, even if in some cases (part of) the type can be inferred. In some cases, the annotation cannot be inferred; for instance, it is impossible to tell whether a particular object is intended to be mutated later in the program or not, and thus whether it should have a @Mutable or @Immutable annotation. Annotations on object creation expressions could also be statically verified (at compile time) to be compatible with the annotations on the constructor.
Annotations on type parameter bounds (extends) and wildcard bounds (extends and super) allow the programmer to fully constrain generic types. Creation of objects with constrained generic types could be statically verified to comply with the annotated bounds.
Annotations on class inheritance (extends and implements) are necessary to allow a programmer to fully specify a supertype. It would otherwise be impossible to extend the annotated version of a particular type t (which is often a valid subtype or supertype of t) without using an anonymous class.
These annotations also provide a convenient way to alias otherwise cumbersome types. For instance, a programmer might declare
final class MyStringMap extends @Readonly Map<@NonNull String, @NonEmpty List<@NonNull @Readonly String>> {}
so that MyStringMap may be used in place of the full, unpalatable supertype. However, this does somewhat limit reusability since a method declared to take a MyStringMap cannot take a Map of the appropriate type.
Annotations in the throws clauses of method declarations allow programmers to enhance exception types. For instance, programs that use the @Critical annotation from the above examples could be statically checked to ensure that catch blocks that can catch a @Critical exceptions are not empty.
There is no need for special syntax to permit annotations on the type of a caught exception; it is already permitted, as in
catch (@NonCritical Exception e) { ... }
In this example case, a tool could warn if any @Critical exception can reach the catch clause.
Section A.1 gave many examples of how type qualifiers have been used in the past. Also see the related work section of [PAC+08].
C#’s attributes [ECM06, chap. 24] play the same role as Java’s annotations: they attach metadata to specific parts of a program, and are carried through to the compiled bytecode representation, where they can be accessed via reflection. The syntax is different: C# uses [AnnotationName] or [AnnotationName: data] where Java uses @AnnotationName or @AnnotationName(data); C# uses AttributeUsageAttribute where Java uses Target; and so forth. However, C# permits metadata on generic arguments, and C# permits multiple metadata instances of the same type to appear at a given location.
Like Java, C# does not permit metadata on elements within a method body. (The “[a]C#” language [CCC05], whose name is pronounced “annotated C sharp”, is an extension to C# that permits annotation of statements and code blocks.)
Harmon and Klefstad [HK07] propose a standard for worst-case execution time annotations.
Pechtchanski’s dissertation [Pec03] uses annotations in the aid of dynamic program optimization. Pechtchanski implemented an extension to the Jikes compiler that supports stylized comments, and uses these annotations on classes, fields, methods, formals, local variable declarations, object creation (new) expressions, method invocations (calls), and program points (empty statements). The annotations are propagated by the compiler to the class file.
Mathias Ricken’s LAPT-javac (https://ricken.us/research/laptjavac/) is a version of javac (version 1.5.0_06) that encodes annotations on local variables in the class file, in new Runtime{Inv,V}isibleLocalVariableAnnotations attributes. The class file format of LAPT-javac differs from that proposed in this document. Ricken’s xajavac (Extended Annotation Enabled javac) permits subtyping of annotations (https://ricken.us/research/xajavac/).
The Java Modeling Language, JML [LBR06], is a behavioral modeling language for writing specifications for Java code. It uses stylized comments as annotations, some of which apply to types.
Ownership types [CPN98, Boy04, Cla01, CD02, PNCB06, NVP98, DM05, LM04, LP06] permit programmers to control aliasing and access among objects. Ownership types can be expressed with type annotations and have been applied to program verification [LM04, Mül02, MPHL06], thread synchronization [BLR02, JPLS05], memory management [ACG+06, BSBR03], and representation independence [BN02].
JavaCOP [ANMM06] is a framework for implementing pluggable type systems in Java. Whereas JSR 308 uses standard interfaces such as the Tree API and the JSR 269 annotation processing framework, JavaCOP defines its own incompatible variants. A JavaCOP type checker must be programmed in a combination of Java and JavaCOP’s own declarative pattern-matching and rule-based language. JavaCOP’s authors have defined parts of over a dozen type-checkers in their language. Their paper does not report that they have run any of these type-checkers on a real program; this is due to limitations that make JavaCOP impractical (so far) for real use.
JACK makes annotations on array brackets refer to the array, not the elements of the array [MPPD08].
As a consequence of JSR 308’s changes to the Java syntax and class file format (Sections 2 and 3), several tools need to be updated.
These tools include:
When producing bytecode for an earlier version of the virtual machine, via the -target command-line option, a Java 8 compiler is permitted to place type annotations in declaration attributes.
Oracle’s Tree API, which exposes the AST (including annotations) to authors of javac annotation processors (compile-time plug-ins), represents type annotations, per Section 2. The same goes for other Java compilers, such as that of Eclipse.
The JSR 269 (annotation processing) model represents type annotations that are visible down to class member declarations (the top of Figure 1), but not within a method, such as on individual statements and expressions.
The javap disassembler recognizes the new class file format and outputs annotations.
The pack200/unpack200 tool preserves the new attributes through a compress-decompress cycle.
The co-spec-lead, Alex Buckley, made significant contributions throughout the process, to both the design and this document. The JSR 308 Expert Group also have helpful feedback and suggestions.
Matt Papi, Mahmood Ali, and Werner Dietl designed and implemented the JSR 308 compiler as modifications to Oracle’s OpenJDK javac compiler, and contributed to the JSR 308 design.
The members of the JSR 308 mailing list (http://groups.google.com/group/jsr308-discuss) provided valuable comments and suggestions. Additional feedback is welcome.
JSR 308 received the Most Innovative Java SE/EE JSR of the Year award in 2007, at the 5th annual JCP Program Awards. JSR 308’s spec leads (Michael Ernst and Alex Buckley) were nominated as Most Outstanding Spec Lead for Java SE/EE in 2008, at the 6th annual JCP Program Awards. Michael Ernst won a Java Rock Star award for a presentation on the Checker Framework, which builds on the Type Annotations syntax, at JavaOne 2009.