Java 8 Type Annotations
Join the DZone community and get the full member experience.
Join For FreeType Annotations are annotations that can be placed anywhere you use a type. This includes the new operator, type casts, implements clauses and throws clauses. Type Annotations allow improved analysis of Java code and can ensure even stronger type checking.
In source code this means we get two new ElementTypes for annotations:
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) public @interface Test { }
Please note that the annotations from the following examples will not work out of the box when Java 8 is released. Java 8 only provides the ability to define these types of annotations. It is then up to framework and tool developers to actually make use of it. So this is a collection of annotations frameworks could give us in the future. Most of the examples are taken from the Type Annotations specification and various Java 8 presentations.
Simple type definitions with type annotations look like this:
@NotNull String str1 = ... @Email String str2 = ... @NotNull @NotBlank String str3 = ...
Map.@NonNull Entry = ...
new @Interned MyObject() new @NonEmpty @Readonly List<String>(myNonEmptyStringSet)
myObject.new @Readonly NestedClass()
myString = (@NonNull String) myObject; query = (@Untainted String) str;
class UnmodifiableList<T> implements @Readonly List<T> { ... }
List<@Email String> emails = ... List<@ReadOnly @Localized Message> messages = ... Graph<@Directional Node> directedGraph = ...
Map<@NonNull String, @NonEmpty List<@Readonly Document>> documents;
public <E extends @ReadOnly Composable<E> & @Localized MessageSource> void foo(...) { ... }
class Folder<F extends @Existing File> { ... } Collection<? super @Existing File> c = ... List<@Immutable ? extends Comparable<T>> unchangeable = ...
myObject.<@NotBlank String>myMethod(...);
1 | new <String> @Interned MyObject() |
void monitorTemperature() throws @Critical TemperatureException { ... } void authenticate() throws @Fatal @Logged AccessDeniedException { ... }
boolean isNonNull = myString instanceof @NonNull String; boolean isNonBlankEmail = myString instanceof @NotBlank @Email String;
@Vernal Date::getDay List<@English String>::size Arrays::<@NonNegative Integer>sort
Conclusion
Type annotations are an interesting addition to the Java type system. They can be applied to any use of a type and enable a more detailed code analysis. If you want to use Type annotations right now you should have a look at the Checker Framework.
Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments