JSR 308
"permits annotations to appear on any use of a type, not just on class/method/field/variable declarations"
for generic type arguments to parameterized classes:
Map<@NonNull String, @NonEmpty List<@Readonly Document>> files;for generic type arguments in a generic method or constructor invocation:
o.<@NonNull String>m("...");for type parameter bounds and wildcards:
class Folder<F extends @Existing File> { ... } Collection<? super @Existing File>for class inheritance:
class UnmodifiableList<T> implements @Readonly List<@Readonly T> { ... }for throws clauses:
void monitorTemperature() throws @Critical TemperatureException { ... }for typecasts:
myString = (@NonNull String) myObject;for type tests:
boolean isNonNull = myString instanceof @NonNull String;for object creation:
new @NonEmpty @Readonly List<String>(myNonEmptyStringSet)For generic constructors (JLS section 8.8.4), the annotation follows the explicit type arguments (JLS section 15.9):
new <String> @Interned MyObject()for method receivers:
public int size() @Readonly { ... }for class literals:
Class<@NonNull String> c = @NonNull String.class;for arrays:
Document[@Readonly][] docs4 = new Document[@Readonly 2][12]; Document[][@Readonly] docs5 = new Document[2][@Readonly 12];