Debian Codegraph @

src/com/google/errorprone/annotations/Immutable.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * Copyright 2015 The Error Prone Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.google.errorprone.annotations;
17
18 import static java.lang.annotation.ElementType.TYPE;
19 import static java.lang.annotation.RetentionPolicy.RUNTIME;
20
21 import java.lang.annotation.Documented;
22 import java.lang.annotation.Inherited;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.Target;
25
26 /**
27 * The class to which this annotation is applied is immutable.
28 *
29 * <p>An object is immutable if its state cannot be observed to change after construction. Immutable
30 * objects are inherently thread-safe.
31 *
32 * <p>A class is immutable if all instances of that class are immutable. The immutability of a class
33 * can only be fully guaranteed if the class is final, otherwise one must ensure all subclasses are
34 * also immutable.
35 *
36 * <p>A conservative definition of object immutability is:
37 *
38 * <ul>
39 * <li>All fields are final;
40 * <li>All reference fields are of immutable type, or null;
41 * <li>It is <em>properly constructed</em> (the {@code this} reference does not escape the
42 * constructor).
43 * </ul>
44 *
45 * <p>The requirement that all reference fields be immutable ensures <em>deep</em> immutability,
46 * meaning all contained state is also immutable. A weaker property, common with container classes,
47 * is <em>shallow</em> immutability, which allows some of the object's fields to point to mutable
48 * objects. One example of shallow immutability is guava's ImmutableList, which may contain mutable
49 * elements.
50 *
51 * <p>It is possible to implement immutable classes with some internal mutable state, as long as
52 * callers can never observe changes to that state. For example, some state may be lazily
53 * initialized to improve performance.
54 *
55 * <p>It is also technically possible to have an immutable object with non-final fields (see the
56 * implementation of {@link String#hashCode()} for an example), but doing this correctly requires
57 * subtle reasoning about safe data races and deep knowledge of the Java Memory Model.
58 *
59 * <p>Use of this annotation is validated by <a
60 * href="https://errorprone.info/bugpattern/Immutable">Error Prone's immutability analysis</a>,
61 * which ensures that all {@code @Immutable}-annotated classes are deeply immutable according to the
62 * conservative definition above. Non-final classes may be annotated with {@code @Immutable}, and
63 * any code compiled by Error Prone will be checked to ensure that no mutable subtypes of
64 * {@code @Immutable}-annotated classes exist.
65 *
66 * <p>For more information about immutability, see:
67 *
68 * <ul>
69 * <li>Java Concurrency in Practice §3.4
70 * <li>Effective Java 3rd Edition §17
71 * </ul>
72 */
73 @Documented
74 @Target(TYPE)
75 @Retention(RUNTIME)
76 @Inherited
77 public @interface Immutable {
78
79 /**
80 * When annotating a generic type as immutable, {@code containerOf} specifies which type
81 * parameters must be instantiated with immutable types for the container to be deeply immutable.
82 */
83 String[] containerOf() default {};
84 }