Repository navigation
Description
In bytecode, a TYPE_USE annotation uses a TargetType constant to store what an annotation applies to. It is useful to go through all locations and discuss their semantics, also to ensure we cover all source locations.
A typepath further refines where in the type the annotation applies. Here it is enough to distinguish top-level from non-top level uses.
For nested types, only the deepest nested type can be annotated. All enclosing types are implicitly NotNull and no explicit annotations on outer types are allowed. Note that type arguments to outer types might still be annotated. (This is a bit tricky to check, because depending on the type, an empty type path might be legal or not.)
The reference implementation should enforce correct usage of type annotations.
-
CLASS_TYPE_PARAMETER(0x00)
Annotation on the type parameter declaration. The lower bound is always NotNull, so forbid annotation. -
METHOD_TYPE_PARAMETER(0x01)
Annotation on the type parameter declaration. The lower bound is always NotNull, so forbid annotation. -
CLASS_EXTENDS(0x10)
Annotation on the class extends and implements clauses.
On the top-level annotations make no sense. In type argument positions they are allowed.
For example:class C extends @Nullable List<@Nullable String> { … }
The super-class/super-interface are always not-null and no annotations are allowed.
Type arguments in those types can use annotations. -
CLASS_TYPE_PARAMETER_BOUND(0x11)
Annotation on the type parameter bound. Can occur, both on the top-level and inside. -
METHOD_TYPE_PARAMETER_BOUND(0x12)
Annotation on the type parameter bound. Can occur, both on the top-level and inside. -
FIELD(0x13)
Can occur with arbitrary path. -
METHOD_RETURN(0x14)
Can occur with arbitrary path. -
METHOD_RECEIVER(0x15)
Method receiver types are always non-null. Forbid all annotations. Even for a generic class, I don't think the receiver ever needs an annotation. -
METHOD_FORMAL_PARAMETER(0x16)
Can occur with arbitrary path. -
THROWS(0x17)
Types in throws clauses are always non-null and no explicit annotations are allowed. No type arguments are allowed for exception types.
For examplevoid foo() throws @Nullable MyException { … }is forbidden. -
LOCAL_VARIABLE(0x40, true)
Can occur with arbitrary path. -
RESOURCE_VARIABLE(0x41, true)
Can occur with arbitrary path. -
EXCEPTION_PARAMETER(0x42, true)
Types in catch clauses are always non-null and no explicit annotations are allowed. No type arguments are allowed for exception types. -
INSTANCEOF(0x43, true)
See discussion about casts/instanceof in separate issue. The top-level annotation should depend on the cast expression. Type arguments could be annotated, but JVM won’t check. -
NEW(0x44, true)
An object creation is always non-null and no top-level annotations are allowed.
Type arguments can use annotations. -
CONSTRUCTOR_REFERENCE(0x45, true)
A constructor reference is always non-null and no top-level annotations are allowed. -
METHOD_REFERENCE(0x46, true)
A method reference is always non-null and no top-level annotations are allowed. -
CAST(0x47, true)
See discussion about casts/instanceof in separate issue. The top-level annotation should depend on the cast expression. Type arguments could be annotated, but JVM won’t check. -
CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x48, true)
Can occur with arbitrary path. -
METHOD_INVOCATION_TYPE_ARGUMENT(0x49, true)
Can occur with arbitrary path. -
CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT(0x4A, true)
Can occur with arbitrary path. -
METHOD_REFERENCE_TYPE_ARGUMENT(0x4B, true)
Can occur with arbitrary path. -
UNKNOWN(0xFF)
Should never occur.
Activity
cushon commentedon Jan 10, 2019
I think that isn't quite right, see: JDK-8215035.
wmdietlGC commentedon Jan 10, 2019
Thanks for pointing this out!
Do we ever need to annotate an "outer" type? That is, do we have a JLS/JVMS guarantee that the outer class field is always not-null?
Would we ever need to write:
So maybe we can say that nullness annotations are only allowed on the most-deeply-nested type and all outer classes are NotNull.
cushon commentedon Jan 10, 2019
I don't think so for
@Nullable. I agree with the general discussion of whichtarget_typelocations are relevant, I just wanted to clarify the distinction between an emptytype_pathand annotating the 'top-level' type. I think it reads OK if you just omit the part about 'empty path' and talk about 'top-level' types more abstractly.wmdietlGC commentedon Jan 10, 2019
I've changed the empty path sentence and added a paragraph about nested types. PTAL.
cushon commentedon Jan 10, 2019
LG. For "only the deepest nested type can be annotated", the idea is to disallow
Middle.@Nullable Outer.Inner, but not necessarilyOuter<@Nullable Foo>.Inner?wmdietlGC commentedon Jan 10, 2019
Yes, type arguments to outer classes are still necessary. So legal:
@Nullable OuterOuter.@Nullable InnerOuter2<@Nullable String>.Inner2Outer2<@Nullable String[]>.@Nullable Inner2but illegal:
@Nullable Outer.Inner@NotNull Outer2<String>.@Nullable Inneretc.
[-]TYPE_USE locations and their semantics[/-][+]TYPE_USE locations and their semantics for nullness[/+]kevinb9n commentedon Apr 19, 2019
The "For nested types..." part should I think clarify that it's about non-static nested types? This may be clear to those used to the terminology but wasn't to me. If a nested class is static, then the outer class name is used as a simple name qualifier only and I would think it certain that it makes no sense for annotations to be applied to it.
kevinb9n commentedon Apr 19, 2019
INSTANCEOF -- we should not permit anything that a user could think was being checked but wasn't.
kevinb9n commentedon Apr 19, 2019
Where does that long list say anything about arrays?
wmdietlGC commentedon Apr 19, 2019
One of the possible type paths is an array. See http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/file/8aab93346792/src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java#l43 which defines four options:
For reference, target types are here: http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/file/8aab93346792/src/share/classes/com/sun/tools/javac/code/TargetType.java#l45
So wherever a type-use annotation can appear can be specified by a combination of a target type and a type path.
dzharkov commentedon Jun 13, 2019
I wonder how defaulting annotations should work with TYPE_USE applicability:
(see the comment by @sdeleuze in our KEEP)
dzharkov commentedon Jun 13, 2019
NB: Some time ago when we were designing a similar artifact it was proposed to use a different enum instead of
ElementType(see the KEEP)The enum contains a special entry for
TYPE_ARGUMENTin addition toTYPE_USEkevinb9n commentedon Oct 10, 2019
@dzharkov
In general I think the topic of this issue is suitably specified in the design draft now.