Debian Codegraph @

src/com/google/errorprone/annotations/concurrent/LazyInit.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.concurrent;
17
18 import com.google.errorprone.annotations.IncompatibleModifiers;
19 import com.google.errorprone.annotations.Modifier;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import java.lang.annotation.Target;
24
25 /**
26 * Use this annotation on any static or field that will be initialized lazily, where races yield no
27 * semantic difference in the code (as, for example, is the case with {@link String#hashCode}). Note
28 * that lazily initializing a non-volatile field is hard to do correctly, and one should rarely use
29 * this. It should also only be done by developers who clearly understand the potential issues, and
30 * then, always using the pattern as presented in the {@code getData} method of this sample code
31 * below:
32 *
33 * <pre>{@code
34 * private final String source;
35 * @LazyInit private String data;
36 *
37 * public String getData() {
38 * String local = data;
39 * if (local == null) {
40 * data = local = expensiveCalculation(source);
41 * }
42 * return local;
43 * }
44 *
45 * private static String expensiveCalculation(String string) {
46 * return string.replaceAll(" ", "_");
47 * }
48 * }</pre>
49 *
50 * <p>The need for using the {@code local} variable is detailed in
51 * http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html (see, particularly, the
52 * part after "Now, let's break the code").
53 *
54 * <p>Also note that {@code LazyInit} must not be used on 64-bit primitives ({@code long}s and
55 * {@code double}s), because the Java Language Specification does not guarantee that writing to
56 * these is atomic. Furthermore, when used for non-primitives, the non-primitive must be either
57 * truly immutable or at least thread safe (in the Java memory model sense). And callers must
58 * accommodate the fact that different calls to something like the above getData() method may return
59 * different (though identically computed) objects, with different identityHashCode() values. Again,
60 * unless you really understand this <b>and</b> you really need the performance benefits of
61 * introducing the data race, do not use this construct.
62 */
63 @IncompatibleModifiers(modifier = {Modifier.FINAL})
64 @Retention(RetentionPolicy.RUNTIME)
65 @Target(ElementType.FIELD)
66 public @interface LazyInit {}