8001107: @Stable annotation for constant folding of lazily evaluated variables
authorvlivanov
Mon, 23 Sep 2013 19:51:40 +0400
changeset 20494 cf77ae5ff678
parent 20493 6dff5c0efc2f
child 20495 6586c2f78d57
8001107: @Stable annotation for constant folding of lazily evaluated variables Reviewed-by: twisti, kvn, rbackman Contributed-by: john.r.rose@oracle.com, vladimir.x.ivanov@oracle.com
jdk/src/share/classes/java/lang/invoke/LambdaForm.java
jdk/src/share/classes/java/lang/invoke/MethodType.java
jdk/src/share/classes/java/lang/invoke/MethodTypeForm.java
jdk/src/share/classes/java/lang/invoke/Stable.java
--- a/jdk/src/share/classes/java/lang/invoke/LambdaForm.java	Tue Oct 01 10:37:05 2013 -0700
+++ b/jdk/src/share/classes/java/lang/invoke/LambdaForm.java	Mon Sep 23 19:51:40 2013 +0400
@@ -120,7 +120,7 @@
 class LambdaForm {
     final int arity;
     final int result;
-    final Name[] names;
+    @Stable final Name[] names;
     final String debugName;
     MemberName vmentry;   // low-level behavior, or null if not yet prepared
     private boolean isCompiled;
@@ -971,8 +971,8 @@
 
     static class NamedFunction {
         final MemberName member;
-        MethodHandle resolvedHandle;
-        MethodHandle invoker;
+        @Stable MethodHandle resolvedHandle;
+        @Stable MethodHandle invoker;
 
         NamedFunction(MethodHandle resolvedHandle) {
             this(resolvedHandle.internalMemberName(), resolvedHandle);
@@ -1267,7 +1267,7 @@
         final char type;
         private short index;
         final NamedFunction function;
-        final Object[] arguments;
+        @Stable final Object[] arguments;
 
         private Name(int index, char type, NamedFunction function, Object[] arguments) {
             this.index = (short)index;
--- a/jdk/src/share/classes/java/lang/invoke/MethodType.java	Tue Oct 01 10:37:05 2013 -0700
+++ b/jdk/src/share/classes/java/lang/invoke/MethodType.java	Mon Sep 23 19:51:40 2013 +0400
@@ -94,9 +94,9 @@
     private final Class<?>[] ptypes;
 
     // The remaining fields are caches of various sorts:
-    private MethodTypeForm form; // erased form, plus cached data about primitives
-    private MethodType wrapAlt;  // alternative wrapped/unwrapped version
-    private Invokers invokers;   // cache of handy higher-order adapters
+    private @Stable MethodTypeForm form; // erased form, plus cached data about primitives
+    private @Stable MethodType wrapAlt;  // alternative wrapped/unwrapped version
+    private @Stable Invokers invokers;   // cache of handy higher-order adapters
 
     /**
      * Check the given parameters for validity and store them into the final fields.
--- a/jdk/src/share/classes/java/lang/invoke/MethodTypeForm.java	Tue Oct 01 10:37:05 2013 -0700
+++ b/jdk/src/share/classes/java/lang/invoke/MethodTypeForm.java	Mon Sep 23 19:51:40 2013 +0400
@@ -51,12 +51,13 @@
     final MethodType basicType;         // the canonical erasure, with primitives simplified
 
     // Cached adapter information:
-    /*lazy*/ MethodHandle genericInvoker; // JVM hook for inexact invoke
-    /*lazy*/ MethodHandle basicInvoker;   // cached instance of MH.invokeBasic
-    /*lazy*/ MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction
+    @Stable String typeString;           // argument type signature characters
+    @Stable MethodHandle genericInvoker; // JVM hook for inexact invoke
+    @Stable MethodHandle basicInvoker;   // cached instance of MH.invokeBasic
+    @Stable MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction
 
     // Cached lambda form information, for basic types only:
-    final LambdaForm[] lambdaForms;
+    final @Stable LambdaForm[] lambdaForms;
     // Indexes into lambdaForms:
     static final int
             LF_INVVIRTUAL     =  0,  // DMH invokeVirtual
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/src/share/classes/java/lang/invoke/Stable.java	Mon Sep 23 19:51:40 2013 +0400
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package java.lang.invoke;
+
+import java.lang.annotation.*;
+
+/**
+ * A field may be annotated as stable if all of its component variables
+ * changes value at most once.
+ * A field's value counts as its component value.
+ * If the field is typed as an array, then all the non-null components
+ * of the array, of depth up to the rank of the field's array type,
+ * also count as component values.
+ * By extension, any variable (either array or field) which has annotated
+ * as stable is called a stable variable, and its non-null or non-zero
+ * value is called a stable value.
+ * <p>
+ * Since all fields begin with a default value of null for references
+ * (resp., zero for primitives), it follows that this annotation indicates
+ * that the first non-null (resp., non-zero) value stored in the field
+ * will never be changed.
+ * <p>
+ * If the field is not of an array type, there are no array elements,
+ * then the value indicated as stable is simply the value of the field.
+ * If the dynamic type of the field value is an array but the static type
+ * is not, the components of the array are <em>not</em> regarded as stable.
+ * <p>
+ * If the field is an array type, then both the field value and
+ * all the components of the field value (if the field value is non-null)
+ * are indicated to be stable.
+ * If the field type is an array type with rank {@code N &gt; 1},
+ * then each component of the field value (if the field value is non-null),
+ * is regarded as a stable array of rank {@code N-1}.
+ * <p>
+ * Fields which are declared {@code final} may also be annotated as stable.
+ * Since final fields already behave as stable values, such an annotation
+ * indicates no additional information, unless the type of the field is
+ * an array type.
+ * <p>
+ * It is (currently) undefined what happens if a field annotated as stable
+ * is given a third value.  In practice, if the JVM relies on this annotation
+ * to promote a field reference to a constant, it may be that the Java memory
+ * model would appear to be broken, if such a constant (the second value of the field)
+ * is used as the value of the field even after the field value has changed.
+ */
+/* package-private */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+@interface Stable {
+}