@Documented @Inherited @Target(value={TYPE,FIELD,METHOD,PARAMETER,LOCAL_VARIABLE}) @Retention(value=CLASS) public @interface Constant
Indicates that the state of a class instance, a static field or a
method return value / parameter will not change ever. For parameters
tagged Constant, defensive copy is unnecessary.
@Constant(comment="Immutable") class Polygon extends Shape { private Point2D[] vertices; public Polygon(@Constant Point2D... vertices) { this.vertices = vertices; // No defensive copying required. } @Constant(comment="Unmodifiable View") List<Point2D> getVertices() { return ConstantTable.of(vertices); // Unmodifiable array wrapper. } }
The constant annotation is primarily for API documentation purpose but static analyzers could also be used to detect constant rules violations.
Polygon triangle = new Polygon(p1, p2, p3); // Ok, literals are always constant. Point2D[] vertices = new Point2D[] { p1, p2, p3 }; triangle = new Polygon(vertices); // vertices is now assumed constant. vertices[0] = null; // Rule violation, modification after vertices has been assumed constant!
public abstract boolean value
true).public abstract String comment
"").Copyright © 2017. All rights reserved.