Erste Implementierung für JSR 308

Die JSR 308 (Annotations on Java Types) hat unter anderem das Ziel, Annotationen an weiteren Dingen festzumachen:

  • 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];

    This syntax permits independent annotations for each distinct level of array, and for the elements; see Section 3.4 for alternative syntaxes.

Die Beispiele stammen aus dem working document von Michael D. Ernst. Auf seiner Homepage lässt sich die Compilererweiterung herunterladen und alles über die JSR 308 nachlesen.

Ähnliche Beiträge

Ein Gedanke zu “Erste Implementierung für JSR 308

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert