http://jcp.org/aboutJava/communityprocess/edr/jsr308/index2.html. Aus der Spezi:
JSR 308 extends Java to allow annotations on any use of a type, and on type parameter declarations.
Beispiele:
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, including wildcard bounds:
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;
It is not permitted to omit the Java type, as in myString = (@NonNull) myObject;.
for constructor invocation results (that is, for object creation):
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()
for type tests:
boolean isNonNull = myString instanceof @NonNull String;
It is not permitted to omit the Java type, as in myString instanceof @NonNull.
Damit einher geht eine krasse Änderung, das bei Objektmethoden einfach so ein this Übergeben werden kann.
It is permitted to explicitly declare the method receiver as the first formal parameter
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) { … }
}
and then it would be possible to annotate the receiver’s type:
class MyClass {
…
public String toString(@Readonly MyClass this) { … }
public boolean equals(@Readonly MyClass this, @Readonly Object other) { … }
}
Interessant finde ich noch:
To ease the transition from standard Java SE 7 code to code with type annotations, the reference implementation
recognizes the type annotations when surrounded by comment markers:
List</*@Readonly*/ Object> myList;
This permits use of both standard Java SE 7 tools and the new annotations even before Java SE 8 is released