1 /*
 2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.internal.vm.annotation;
27 
28 import java.lang.annotation.*;
29 
30 /**
31  * A field may be annotated as stable if all of its component variables
32  * changes value at most once.
33  * A field's value counts as its component value.
34  * If the field is typed as an array, then all the non-null components
35  * of the array, of depth up to the rank of the field's array type,
36  * also count as component values.
37  * By extension, any variable (either array or field) which has annotated
38  * as stable is called a stable variable, and its non-null or non-zero
39  * value is called a stable value.
40  * <p>
41  * Since all fields begin with a default value of null for references
42  * (resp., zero for primitives), it follows that this annotation indicates
43  * that the first non-null (resp., non-zero) value stored in the field
44  * will never be changed.
45  * <p>
46  * If the field is not of an array type, there are no array elements,
47  * then the value indicated as stable is simply the value of the field.
48  * If the dynamic type of the field value is an array but the static type
49  * is not, the components of the array are <em>not</em> regarded as stable.
50  * <p>
51  * If the field is an array type, then both the field value and
52  * all the components of the field value (if the field value is non-null)
53  * are indicated to be stable.
54  * If the field type is an array type with rank {@code N > 1},
55  * then each component of the field value (if the field value is non-null),
56  * is regarded as a stable array of rank {@code N-1}.
57  * <p>
58  * Fields which are declared {@code final} may also be annotated as stable.
59  * Since final fields already behave as stable values, such an annotation
60  * conveys no additional information regarding change of the field's value, but
61  * still conveys information regarding change of additional components values if
62  * the type of the field is an array type (as described above).
63  * <p>
64  * The HotSpot VM relies on this annotation to promote a non-null (resp.,
65  * non-zero) component value to a constant, thereby enabling superior
66  * optimizations of code depending on such a value (such as constant folding).
67  * More specifically, the HotSpot VM will process non-null stable fields (final
68  * or otherwise) in a similar manner to static final fields with respect to
69  * promoting the field's value to a constant.  Thus, placing aside the
70  * differences for null/non-null values and arrays, a final stable field is
71  * treated as if it is really final from both the Java language and the HotSpot
72  * VM.
73  * <p>
74  * It is (currently) undefined what happens if a field annotated as stable
75  * is given a third value (by explicitly updating a stable field, a component of
76  * a stable array, or a final stable field via reflection or other means).
77  * Since the HotSpot VM promotes a non-null component value to constant, it may
78  * be that the Java memory model would appear to be broken, if such a constant
79  * (the second value of the field) is used as the value of the field even after
80  * the field value has changed (to a third value).
81  *
82  * @implNote
83  * This annotation only takes effect for fields of classes loaded by the boot
84  * loader.  Annotations on fields of classes loaded outside of the boot loader
85  * are ignored.
86  */
87 @Target(ElementType.FIELD)
88 @Retention(RetentionPolicy.RUNTIME)
89 public @interface Stable {
90 }