Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/CallStaticInitOrder.java
47209 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/**25* @test26* @summary static initializer invocation order27*28* @build indify.Indify29* @compile CallStaticInitOrder.java30* @run main/othervm31* indify.Indify32* --expand-properties --classpath ${test.classes}33* --java test.java.lang.invoke.CallStaticInitOrder34*/3536package test.java.lang.invoke;3738import java.io.*;3940import java.lang.invoke.*;41import static java.lang.invoke.MethodHandles.*;42import static java.lang.invoke.MethodType.*;4344public class CallStaticInitOrder {45private static int TICK;46private static synchronized int tick(String event) {47int n = ++TICK;48System.out.println("event #"+n+" = "+event);49return n;50}5152static int Init1Tick;53private static class Init1 {54static { Init1Tick = tick("foo -> Init1.<clinit>"); }55static int foo() { return Init1Tick; }56}5758static int Init2Tick;59private static class Init2 {60static { Init2Tick = tick("bar -> Init2.<clinit>"); }61static int bar() { return Init2Tick; }62}6364static int Init3Tick;65private static class Init3 {66static { Init3Tick = tick("baz -> Init3.<clinit>"); }67static int baz() { return Init3Tick; }68}6970static int Init4Tick;71private static class Init4 {72static { Init4Tick = tick("bat -> Init4.<clinit>"); }73static int bat() { return Init4Tick; }74}7576static int Init5Tick;77private static class Init5 {78static { Init5Tick = tick("read bang -> Init5.<clinit>"); }79static int bang = Init5Tick;80}8182static int Init6Tick;83private static class Init6 {84static { Init6Tick = tick("write pong -> Init6.<clinit>"); }85static int pong;86}8788private static final MutableCallSite CONSTANT_CS_baz;89private static MethodHandle MH_foo() throws ReflectiveOperationException {90return lookup().findStatic(Init1.class, "foo", methodType(int.class));91}92private static final MethodHandle CONSTANT_MH_bar;93private static MethodHandle MH_baz() throws ReflectiveOperationException {94return lookup().findStatic(Init3.class, "baz", methodType(int.class));95}96private static final MethodHandle CONSTANT_MH_bat;97private static final MethodHandle CONSTANT_MH_bangGetter;98private static final MethodHandle CONSTANT_MH_pongSetter;99static {100try {101int t1 = tick("CallStaticInitOrder.<clinit>");102{103CONSTANT_CS_baz = new MutableCallSite(methodType(int.class));104// MH_foo() := lookup().findStatic(Init1.class, "foo", methodType(int.class));105CONSTANT_MH_bar = lookup().findStatic(Init2.class, "bar", methodType(int.class));106// MH_baz() := lookup().findStatic(Init3.class, "baz", methodType(int.class));107CONSTANT_MH_bat = lookup().unreflect(Init4.class.getDeclaredMethod("bat"));108CONSTANT_MH_bangGetter = lookup().findStaticGetter(Init5.class, "bang", int.class);109MethodHandle pongSetter = lookup().findStaticSetter(Init6.class, "pong", int.class);110MethodHandle tickGetter = lookup().findStaticGetter(CallStaticInitOrder.class, "Init6Tick", int.class);111CONSTANT_MH_pongSetter = filterReturnValue(insertArguments(pongSetter, 0, -99), tickGetter);112}113int t2 = tick("CallStaticInitOrder.<clinit> done");114assertEquals(t1+1, t2); // no ticks in between115} catch (Exception ex) {116throw new InternalError(ex.toString());117}118}119120public static void main(String... av) throws Throwable {121testInit();122if (LAST_LOSER != null) throw LAST_LOSER;123}124125private static Throwable LAST_LOSER;126127private static void assertEquals(int expected, int actual) {128if (expected != actual) {129Throwable loser = new AssertionError("expected: " + expected + ", actual: " + actual);130if (LAST_LOSER != null)131LAST_LOSER.printStackTrace(System.out);132LAST_LOSER = loser;133}134}135136private static void testInit() throws Throwable {137System.out.println("runFoo = "+runFoo());138System.out.println("runBar = "+runBar());139try {140runBaz();141} catch (IllegalStateException ex) {142tick("runBaz throw/catch");143}144CONSTANT_CS_baz.setTarget(MH_baz());145System.out.println("runBaz = "+runBaz());146System.out.println("runBat = "+runBat());147System.out.println("runBang = "+runBang());148System.out.println("runPong = "+runPong());149}150151private static int runFoo() throws Throwable {152assertEquals(Init1Tick, 0); // Init1 not initialized yet153int t1 = tick("runFoo");154int t2 = (int) INDY_foo().invokeExact();155int t3 = tick("runFoo done");156assertEquals(Init1Tick, t2); // when Init1 was initialized157assertEquals(t1+2, t3); // exactly two ticks in between158assertEquals(t1+1, t2); // init happened inside159return t2;160}161private static MethodHandle INDY_foo() throws Throwable {162shouldNotCallThis();163return ((CallSite) MH_bsm().invoke(lookup(), "foo", methodType(int.class))).dynamicInvoker();164}165166private static int runBar() throws Throwable {167assertEquals(Init2Tick, 0); // Init2 not initialized yet168int t1 = tick("runBar");169int t2 = (int) INDY_bar().invokeExact();170int t3 = tick("runBar done");171assertEquals(Init2Tick, t2); // when Init2 was initialized172assertEquals(t1+2, t3); // exactly two ticks in between173assertEquals(t1+1, t2); // init happened inside174return t2;175}176private static MethodHandle INDY_bar() throws Throwable {177shouldNotCallThis();178return ((CallSite) MH_bsm().invoke(lookup(), "bar", methodType(int.class))).dynamicInvoker();179}180181private static int runBaz() throws Throwable {182assertEquals(Init3Tick, 0); // Init3 not initialized yet183int t1 = tick("runBaz");184int t2 = (int) INDY_baz().invokeExact();185int t3 = tick("runBaz done");186assertEquals(Init3Tick, t2); // when Init3 was initialized187assertEquals(t1+2, t3); // exactly two ticks in between188assertEquals(t1+1, t2); // init happened inside189return t2;190}191private static MethodHandle INDY_baz() throws Throwable {192shouldNotCallThis();193return ((CallSite) MH_bsm().invoke(lookup(), "baz", methodType(int.class))).dynamicInvoker();194}195196private static int runBat() throws Throwable {197assertEquals(Init4Tick, 0); // Init4 not initialized yet198int t1 = tick("runBat");199int t2 = (int) INDY_bat().invokeExact();200int t3 = tick("runBat done");201assertEquals(Init4Tick, t2); // when Init4 was initialized202assertEquals(t1+2, t3); // exactly two ticks in between203assertEquals(t1+1, t2); // init happened inside204return t2;205}206private static MethodHandle INDY_bat() throws Throwable {207shouldNotCallThis();208return ((CallSite) MH_bsm().invoke(lookup(), "bat", methodType(int.class))).dynamicInvoker();209}210211private static int runBang() throws Throwable {212assertEquals(Init5Tick, 0); // Init5 not initialized yet213int t1 = tick("runBang");214int t2 = (int) INDY_bang().invokeExact();215int t3 = tick("runBang done");216assertEquals(Init5Tick, t2); // when Init5 was initialized217assertEquals(t1+2, t3); // exactly two ticks in between218assertEquals(t1+1, t2); // init happened inside219return t2;220}221private static MethodHandle INDY_bang() throws Throwable {222shouldNotCallThis();223return ((CallSite) MH_bsm().invoke(lookup(), "bang", methodType(int.class))).dynamicInvoker();224}225226private static int runPong() throws Throwable {227assertEquals(Init6Tick, 0); // Init6 not initialized yet228int t1 = tick("runPong");229int t2 = (int) INDY_pong().invokeExact();230int t3 = tick("runPong done");231assertEquals(Init6Tick, t2); // when Init6 was initialized232assertEquals(t1+2, t3); // exactly two ticks in between233assertEquals(t1+1, t2); // init happened inside234return t2;235}236private static MethodHandle INDY_pong() throws Throwable {237shouldNotCallThis();238return ((CallSite) MH_bsm().invoke(lookup(), "pong", methodType(int.class))).dynamicInvoker();239}240241private static CallSite bsm(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {242System.out.println("bsm "+name+type);243CallSite res;244switch (name) {245case "foo":246res = new ConstantCallSite(MH_foo()); break;247case "bar":248res = new ConstantCallSite(CONSTANT_MH_bar); break;249case "baz":250res = CONSTANT_CS_baz; break;251case "bat":252res = new ConstantCallSite(CONSTANT_MH_bat); break;253case "bang":254res = new ConstantCallSite(CONSTANT_MH_bangGetter); break;255case "pong":256res = new ConstantCallSite(CONSTANT_MH_pongSetter); break;257default:258res = null;259}260if (res == null || !res.type().equals(type)) {261throw new AssertionError(String.valueOf(res));262}263return res;264}265private static MethodHandle MH_bsm() throws ReflectiveOperationException {266shouldNotCallThis();267return lookup().findStatic(lookup().lookupClass(), "bsm",268methodType(CallSite.class, Lookup.class, String.class, MethodType.class));269}270private static void shouldNotCallThis() {271// if this gets called, the transformation has not taken place272throw new AssertionError("this code should be statically transformed away by Indify");273}274}275276277