/*
 * Copyright 2015 The Error Prone Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.errorprone.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Use this annotation on any static or field that will be initialized lazily, where races yield no
 * semantic difference in the code. The canonical example of this is String.hashCode():
 *
 * <pre>{@code
 * public int hashCode() {
 *   int h = hash;
 *   if (h == 0 && value.length > 0) {
 *     char val[] = value;
 *
 *     for (int i = 0; i < value.length; i++) {
 *       h = 31 * h + val[i];
 *     }
 *     hash = h;
 *   }
 *   return h;
 * }
 * }</pre>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LazyInit {}

@@ -960,336 +960,1518 @@
 code
-. The canonical example of this is String.hashCode():%0A *%0A * %3Cpre%3E%7B@code%0A * public int hashCode() %7B%0A *   int h = hash;%0A *   if (h == 0 && value.length %3E 0) %7B%0A *     char val%5B%5D = value
+ (as, for example, is the case with %7B@link String#hashCode%7D). Note%0A * that lazily initializing a non-volatile field is hard to do correctly, and one should rarely use%0A * this. It should also only be done by developers who clearly understand the potential issues, and%0A * then, always using the pattern as presented in the %7B@code getData%7D method of this sample code%0A * below:%0A *%0A * %3Cpre%3E%7B@code%0A * private final String source;%0A * %7B@literal @%7DLazyInit private String data;%0A *%0A * public String getData() %7B%0A *   String local = data;%0A *   if (local == null) %7B%0A *     local = data = expensiveCalculation(source);%0A *   %7D%0A *   return local;%0A *  %7D%0A *%0A * private static String expensiveCalculation(String string) %7B%0A *   return string.replaceAll(%22 %22, %22_%22)
 ;%0A *
+ %7D
 %0A * 
-    for (int i = 0; i %3C value.length; i++) %7B%0A *       h = 31 * h + val%5Bi%5D;%0A *     %7D%0A *     hash = h;%0A *   %7D%0A *   return h;%0A * %7D%0A * %7D%3C/pre%3E
+%7D%3C/pre%3E%0A *%0A * %3Cp%3EThe need for using the %7B@code local%7D variable is detailed in%0A * http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html (see, particularly, the%0A * part after %22Now, let's break the code%22).%0A *%0A * %3Cp%3EAlso note that %7B@code LazyInit%7D must not be used on 64-bit primitives (such as %7B@code long%7Ds%0A * and %7B@code double%7Ds), because the Java Language Specification does not guarantee that writing to%0A * these is made atomically. Furthermore, when using for non-primitives, the non-primitive must be%0A * either truly immutable or at least thread safe (in the Java memory model sense). Again, unless%0A * you really understand this %3Cb%3Eand%3C/b%3E you really need the performance benefits of avoiding the%0A * data race, do not use this construct.
 %0A */

Improve LazyInit's documentation