8024599: JSR 292 direct method handles need to respect initialization rules for static members
authorjrose
Sat, 05 Oct 2013 05:30:39 -0700
changeset 20532 50fba462caa5
parent 20531 14c2a04f21e6
child 20533 bee974bc42ac
8024599: JSR 292 direct method handles need to respect initialization rules for static members Summary: Align MH semantic with bytecode behavior of constructor and static member accesses, regarding <clinit> invocation. Reviewed-by: twisti, darcy, abuckley, vlivanov
jdk/src/share/classes/java/lang/invoke/MethodHandles.java
jdk/test/java/lang/invoke/CallStaticInitOrder.java
--- a/jdk/src/share/classes/java/lang/invoke/MethodHandles.java	Sat Oct 05 05:30:39 2013 -0700
+++ b/jdk/src/share/classes/java/lang/invoke/MethodHandles.java	Sat Oct 05 05:30:39 2013 -0700
@@ -607,13 +607,14 @@
          * additional receiver argument inserted into the method handle type,
          * as there would be with {@link #findVirtual findVirtual} or {@link #findSpecial findSpecial}.)
          * The method and all its argument types must be accessible to the lookup class.
-         * If the method's class has not yet been initialized, that is done
-         * immediately, before the method handle is returned.
          * <p>
          * The returned method handle will have
          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
          * the method's variable arity modifier bit ({@code 0x0080}) is set.
-         * <b>Example:</b>
+         * <p>
+         * If the returned method handle is invoked, the method's class will
+         * be initialized, if it has not already been initialized.
+         * <p><b>Example:</b>
          * <p><blockquote><pre>{@code
 import static java.lang.invoke.MethodHandles.*;
 import static java.lang.invoke.MethodType.*;
@@ -739,8 +740,6 @@
          * The parameter types of the method handle will be those of the constructor,
          * while the return type will be a reference to the constructor's class.
          * The constructor and all its argument types must be accessible to the lookup class.
-         * If the constructor's class has not yet been initialized, that is done
-         * immediately, before the method handle is returned.
          * <p>
          * <em>(Note:  The requested type must have a return type of {@code void}.
          * This is consistent with the JVM's treatment of constructor type descriptors.)</em>
@@ -748,6 +747,9 @@
          * The returned method handle will have
          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
          * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If the returned method handle is invoked, the constructor's class will
+         * be initialized, if it has not already been initialized.
          * <p><b>Example:</b>
          * <p><blockquote><pre>{@code
 import static java.lang.invoke.MethodHandles.*;
@@ -918,6 +920,9 @@
          * value type.
          * The method handle will take no arguments.
          * Access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
          * @param refc the class or interface from which the method is accessed
          * @param name the field's name
          * @param type the field's type
@@ -940,6 +945,9 @@
          * The method handle will take a single
          * argument, of the field's value type, the value to be stored.
          * Access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
          * @param refc the class or interface from which the method is accessed
          * @param name the field's name
          * @param type the field's type
@@ -1024,6 +1032,10 @@
          * The returned method handle will have
          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
          * the method's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If <i>m</i> is static, and
+         * if the returned method handle is invoked, the method's class will
+         * be initialized, if it has not already been initialized.
          * @param m the reflected method
          * @return a method handle which can invoke the reflected method
          * @throws IllegalAccessException if access checking fails
@@ -1095,6 +1107,9 @@
          * The returned method handle will have
          * {@linkplain MethodHandle#asVarargsCollector variable arity} if and only if
          * the constructor's variable arity modifier bit ({@code 0x0080}) is set.
+         * <p>
+         * If the returned method handle is invoked, the constructor's class will
+         * be initialized, if it has not already been initialized.
          * @param c the reflected constructor
          * @return a method handle which can invoke the reflected constructor
          * @throws IllegalAccessException if access checking fails
@@ -1119,6 +1134,10 @@
          * the field.
          * If the field's {@code accessible} flag is not set,
          * access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the field is static, and
+         * if the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
          * @param f the reflected field
          * @return a method handle which can load values from the reflected field
          * @throws IllegalAccessException if access checking fails
@@ -1145,6 +1164,10 @@
          * the field, and the value to be stored.
          * If the field's {@code accessible} flag is not set,
          * access checking is performed immediately on behalf of the lookup class.
+         * <p>
+         * If the field is static, and
+         * if the returned method handle is invoked, the field's class will
+         * be initialized, if it has not already been initialized.
          * @param f the reflected field
          * @return a method handle which can store values into the reflected field
          * @throws IllegalAccessException if access checking fails
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/invoke/CallStaticInitOrder.java	Sat Oct 05 05:30:39 2013 -0700
@@ -0,0 +1,275 @@
+/*
+ * Copyright (c) 2013, 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.
+ *
+ * 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.
+ *
+ */
+
+/**
+ * @test
+ * @summary static initializer invocation order
+ *
+ * @build indify.Indify
+ * @compile CallStaticInitOrder.java
+ * @run main/othervm
+ *      indify.Indify
+ *      --expand-properties --classpath ${test.classes}
+ *      --java test.java.lang.invoke.CallStaticInitOrder
+ */
+
+package test.java.lang.invoke;
+
+import java.io.*;
+
+import java.lang.invoke.*;
+import static java.lang.invoke.MethodHandles.*;
+import static java.lang.invoke.MethodType.*;
+
+public class CallStaticInitOrder {
+    private static int TICK;
+    private static synchronized int tick(String event) {
+        int n = ++TICK;
+        System.out.println("event #"+n+" = "+event);
+        return n;
+    }
+
+    static int Init1Tick;
+    private static class Init1 {
+        static { Init1Tick = tick("foo -> Init1.<clinit>"); }
+        static int foo() { return Init1Tick; }
+    }
+
+    static int Init2Tick;
+    private static class Init2 {
+        static { Init2Tick = tick("bar -> Init2.<clinit>"); }
+        static int bar() { return Init2Tick; }
+    }
+
+    static int Init3Tick;
+    private static class Init3 {
+        static { Init3Tick = tick("baz -> Init3.<clinit>"); }
+        static int baz() { return Init3Tick; }
+    }
+
+    static int Init4Tick;
+    private static class Init4 {
+        static { Init4Tick = tick("bat -> Init4.<clinit>"); }
+        static int bat() { return Init4Tick; }
+    }
+
+    static int Init5Tick;
+    private static class Init5 {
+        static { Init5Tick = tick("read bang -> Init5.<clinit>"); }
+        static int bang = Init5Tick;
+    }
+
+    static int Init6Tick;
+    private static class Init6 {
+        static { Init6Tick = tick("write pong -> Init6.<clinit>"); }
+        static int pong;
+    }
+
+    private static final MutableCallSite CONSTANT_CS_baz;
+    private static MethodHandle MH_foo() throws ReflectiveOperationException {
+        return lookup().findStatic(Init1.class, "foo", methodType(int.class));
+    }
+    private static final MethodHandle CONSTANT_MH_bar;
+    private static MethodHandle MH_baz() throws ReflectiveOperationException {
+        return lookup().findStatic(Init3.class, "baz", methodType(int.class));
+    }
+    private static final MethodHandle CONSTANT_MH_bat;
+    private static final MethodHandle CONSTANT_MH_bangGetter;
+    private static final MethodHandle CONSTANT_MH_pongSetter;
+    static {
+        try {
+            int t1 = tick("CallStaticInitOrder.<clinit>");
+            {
+                CONSTANT_CS_baz = new MutableCallSite(methodType(int.class));
+                // MH_foo() := lookup().findStatic(Init1.class, "foo", methodType(int.class));
+                CONSTANT_MH_bar = lookup().findStatic(Init2.class, "bar", methodType(int.class));
+                // MH_baz() := lookup().findStatic(Init3.class, "baz", methodType(int.class));
+                CONSTANT_MH_bat = lookup().unreflect(Init4.class.getDeclaredMethod("bat"));
+                CONSTANT_MH_bangGetter = lookup().findStaticGetter(Init5.class, "bang", int.class);
+                MethodHandle pongSetter = lookup().findStaticSetter(Init6.class, "pong", int.class);
+                MethodHandle tickGetter = lookup().findStaticGetter(CallStaticInitOrder.class, "Init6Tick", int.class);
+                CONSTANT_MH_pongSetter = filterReturnValue(insertArguments(pongSetter, 0, -99), tickGetter);
+            }
+            int t2 = tick("CallStaticInitOrder.<clinit> done");
+            assertEquals(t1+1, t2);  // no ticks in between
+        } catch (Exception ex) {
+            throw new InternalError(ex.toString());
+        }
+    }
+
+    public static void main(String... av) throws Throwable {
+        testInit();
+        if (LAST_LOSER != null)  throw LAST_LOSER;
+    }
+
+    private static Throwable LAST_LOSER;
+
+    private static void assertEquals(int expected, int actual) {
+        if (expected != actual) {
+            Throwable loser = new AssertionError("expected: " + expected + ", actual: " + actual);
+            if (LAST_LOSER != null)
+                LAST_LOSER.printStackTrace(System.out);
+            LAST_LOSER = loser;
+        }
+    }
+
+    private static void testInit() throws Throwable {
+        System.out.println("runFoo = "+runFoo());
+        System.out.println("runBar = "+runBar());
+        try {
+            runBaz();
+        } catch (IllegalStateException ex) {
+            tick("runBaz throw/catch");
+        }
+        CONSTANT_CS_baz.setTarget(MH_baz());
+        System.out.println("runBaz = "+runBaz());
+        System.out.println("runBat = "+runBat());
+        System.out.println("runBang = "+runBang());
+        System.out.println("runPong = "+runPong());
+    }
+
+    private static int runFoo() throws Throwable {
+        assertEquals(Init1Tick, 0);  // Init1 not initialized yet
+        int t1 = tick("runFoo");
+        int t2 = (int) INDY_foo().invokeExact();
+        int t3 = tick("runFoo done");
+        assertEquals(Init1Tick, t2);  // when Init1 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_foo() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "foo", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static int runBar() throws Throwable {
+        assertEquals(Init2Tick, 0);  // Init2 not initialized yet
+        int t1 = tick("runBar");
+        int t2 = (int) INDY_bar().invokeExact();
+        int t3 = tick("runBar done");
+        assertEquals(Init2Tick, t2);  // when Init2 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_bar() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "bar", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static int runBaz() throws Throwable {
+        assertEquals(Init3Tick, 0);  // Init3 not initialized yet
+        int t1 = tick("runBaz");
+        int t2 = (int) INDY_baz().invokeExact();
+        int t3 = tick("runBaz done");
+        assertEquals(Init3Tick, t2);  // when Init3 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_baz() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "baz", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static int runBat() throws Throwable {
+        assertEquals(Init4Tick, 0);  // Init4 not initialized yet
+        int t1 = tick("runBat");
+        int t2 = (int) INDY_bat().invokeExact();
+        int t3 = tick("runBat done");
+        assertEquals(Init4Tick, t2);  // when Init4 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_bat() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "bat", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static int runBang() throws Throwable {
+        assertEquals(Init5Tick, 0);  // Init5 not initialized yet
+        int t1 = tick("runBang");
+        int t2 = (int) INDY_bang().invokeExact();
+        int t3 = tick("runBang done");
+        assertEquals(Init5Tick, t2);  // when Init5 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_bang() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "bang", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static int runPong() throws Throwable {
+        assertEquals(Init6Tick, 0);  // Init6 not initialized yet
+        int t1 = tick("runPong");
+        int t2 = (int) INDY_pong().invokeExact();
+        int t3 = tick("runPong done");
+        assertEquals(Init6Tick, t2);  // when Init6 was initialized
+        assertEquals(t1+2, t3);  // exactly two ticks in between
+        assertEquals(t1+1, t2);  // init happened inside
+        return t2;
+    }
+    private static MethodHandle INDY_pong() throws Throwable {
+        shouldNotCallThis();
+        return ((CallSite) MH_bsm().invoke(lookup(), "pong", methodType(int.class))).dynamicInvoker();
+    }
+
+    private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
+        System.out.println("bsm "+name+type);
+        CallSite res;
+        switch (name) {
+        case "foo":
+            res = new ConstantCallSite(MH_foo()); break;
+        case "bar":
+            res = new ConstantCallSite(CONSTANT_MH_bar); break;
+        case "baz":
+            res = CONSTANT_CS_baz; break;
+        case "bat":
+            res = new ConstantCallSite(CONSTANT_MH_bat); break;
+        case "bang":
+            res = new ConstantCallSite(CONSTANT_MH_bangGetter); break;
+        case "pong":
+            res = new ConstantCallSite(CONSTANT_MH_pongSetter); break;
+        default:
+            res = null;
+        }
+        if (res == null || !res.type().equals(type)) {
+            throw new AssertionError(String.valueOf(res));
+        }
+        return res;
+    }
+    private static MethodHandle MH_bsm() throws ReflectiveOperationException {
+        shouldNotCallThis();
+        return lookup().findStatic(lookup().lookupClass(), "bsm",
+                                   methodType(CallSite.class, Lookup.class, String.class, MethodType.class));
+    }
+    private static void shouldNotCallThis() {
+        // if this gets called, the transformation has not taken place
+        throw new AssertionError("this code should be statically transformed away by Indify");
+    }
+}