Wherever a type is used in a declaration or expression, the type_path structure identifies which part of the type is annotated. An annotation may appear on the type itself, but if the type is a reference type, then there are additional locations where an annotation may appear:
- If an array type T
[]is used in a declaration or expression, then an annotation may appear on any component type of the array type, including the element type. - If a nested type T1
.T2 is used in a declaration or expression, then an annotation may appear on the name of the innermost member type and any enclosing type for which a type annotation is admissible (JLS §9.7.4). - If a parameterized type T
<A>or T<? extends A>or T<? super A>is used in a declaration or expression, then an annotation may appear on any type argument or on the bound of any wildcard type argument.
For example, consider the different parts of String[][] that are annotated in:
@Foo String[][] // Annotates the class type String
String @Foo [][] // Annotates the array type String[][]
String[] @Foo [] // Annotates the array type String[]
or the different parts of the nested type Outer.Middle.Inner that are annotated in:
@Foo Outer.Middle.Inner
Outer.@Foo Middle.Inner
Outer.Middle.@Foo Inner
or the different parts of the parameterized types Map<String,Object> and List<...> that are annotated in:
@Foo Map<String,Object>
Map<@Foo String,Object>
Map<String,@Foo Object>
List<@Foo ? extends String>
List<? extends @Foo String>
The type_path structure has the following format:
type_path {
u1 path_length;
{ u1 type_path_kind;
u1 type_argument_index;
} path[path_length];
}
The value of the path_length item gives the number of entries in the path array:
-
If the value of
path_lengthis0, and the type being annotated is a nested type, then the annotation applies to the outermost part of the type for which a type annotation is admissible. -
If the value of
path_lengthis0, and the type being annotated is not a nested type, then the annotation appears directly on the type itself. -
If the value of
path_lengthis non-zero, then each entry in thepatharray represents an iterative, left-to-right step towards the precise location of the annotation in an array type, nested type, or parameterized type. (In an array type, the iteration visits the array type itself, then its component type, then the component type of that component type, and so on, until the element type is reached.) Each entry contains the following two items:-
type_path_kind
The legal values for the
type_path_kinditem are listed in Table 4.7.20.2-A.Table 4.7.20.2-A. Interpretation of
type_path_kindvaluesValue Interpretation 0Annotation is deeper in an array type 1Annotation is deeper in a nested type 2Annotation is on the bound of a wildcard type argument of a parameterized type 3Annotation is on a type argument of a parameterized type -
type_argument_index
If the value of the
type_path_kinditem is0,1, or2, then the value of thetype_argument_indexitem is0.If the value of the
type_path_kinditem is3, then the value of thetype_argument_indexitem specifies which type argument of a parameterized type is annotated, where0indicates the first type argument of a parameterized type.
-
Table 4.7.20.2-B. type_path structures for @A Map<@B ? extends @C String, @D List<@E Object>>
| Annotation | path_length |
path |
|---|---|---|
@A |
0 |
[] |
@B |
1 |
[{type_path_kind: 3; type_argument_index: 0}] |
@C |
2 |
[{type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 2; type_argument_index: 0}] |
@D |
1 |
[{type_path_kind: 3; type_argument_index: 1}] |
@E |
2 |
[{type_path_kind: 3; type_argument_index: 1}, {type_path_kind: 3; type_argument_index: 0}] |
Table 4.7.20.2-C. type_path structures for @I String @F [] @G [] @H []
| Annotation | path_length |
path |
|---|---|---|
@F |
0 |
[] |
@G |
1 |
[{type_path_kind: 0; type_argument_index: 0}] |
@H |
2 |
[{type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |
@I |
3 |
[{type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |
Table 4.7.20.2-D. type_path structures for @A List<@B Comparable<@F Object @C [] @D [] @E []>>
| Annotation | path_length |
path |
|---|---|---|
@A |
0 |
[] |
@B |
1 |
[{type_path_kind: 3; type_argument_index: 0}] |
@C |
2 |
[{type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}] |
@D |
3 |
[{type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |
@E |
4 |
[{type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |
@F |
5 |
[{type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |
Table 4.7.20.2-E. type_path structures for @A Outer . @B Middle . @C Inner
Assuming:
class Outer {
class Middle {
class Inner {}
}
}
| Annotation | path_length |
path |
|---|---|---|
@A |
0 |
[] |
@B |
1 |
[{type_path_kind: 1; type_argument_index: 0}] |
@C |
2 |
[{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 1; type_argument_index: 0}] |
Table 4.7.20.2-F. type_path structures for Outer . @A MiddleStatic . @B Inner
Assuming:
class Outer {
static class MiddleStatic {
class Inner {}
}
}
| Annotation | path_length |
path |
|---|---|---|
@A |
0 |
[] |
@B |
1 |
[{type_path_kind: 1; type_argument_index: 0}] |
In the type Outer . MiddleStatic . Inner, type annotations on the simple name Outer are not admissible because the type name to its right, MiddleStatic, does not refer to an inner class of Outer. |
Table 4.7.20.2-G. type_path structures for Outer . MiddleStatic . @A InnerStatic
Assuming:
class Outer {
static class MiddleStatic {
static class InnerStatic {}
}
}
| Annotation | path_length |
path |
|---|---|---|
@A |
0 |
[] |
In the type Outer . MiddleStatic . InnerStatic, type annotations on the simple name Outer are not admissible because the type name to its right, MiddleStatic, does not refer to an inner class of Outer. Similarly, type annotations on the simple name MiddleStatic are not admissible because the type name to its right, InnerStatic, does not refer to an inner class of MiddleStatic. |
Table 4.7.20.2-H. type_path structures for Outer . Middle<@A Foo . @B Bar> . Inner<@D String @C []>
Assuming:
class Outer {
class Middle<T> {
class Inner<U> {}
}
}
| Annotation | path_length |
path |
|---|---|---|
@A |
2 |
[{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}] |
@B |
3 |
[{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 1; type_argument_index: 0}] |
@C |
3 |
[{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}] |
@D |
4 |
[{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 3; type_argument_index: 0}, {type_path_kind: 0; type_argument_index: 0}] |