Path: blob/master/test/jdk/java/lang/invoke/CallSiteTest.java
41149 views
/*1* Copyright (c) 2011, 2012, 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*/2223/**24* @test25* @summary smoke tests for CallSite26* @library /test/lib27*28* @build indify.Indify29* @compile CallSiteTest.java30* @run main/othervm/timeout=3600 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -Xbatch31* indify.Indify32* --expand-properties --classpath ${test.classes}33* --java test.java.lang.invoke.CallSiteTest34*/3536package test.java.lang.invoke;3738import java.io.*;3940import java.lang.invoke.*;41import static java.lang.invoke.MethodHandles.*;42import static java.lang.invoke.MethodType.*;43import static jdk.test.lib.Asserts.*;4445public class CallSiteTest {46private static final Class<?> CLASS = CallSiteTest.class;4748private static CallSite mcs;49private static CallSite vcs;50private static MethodHandle mh_foo;51private static MethodHandle mh_bar;5253static {54try {5556mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class));57mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class));58mcs = new MutableCallSite(mh_foo);59vcs = new VolatileCallSite(mh_foo);60} catch (Exception e) {61e.printStackTrace();62throw new Error(e);63}64}6566public static void main(String... av) throws Throwable {67testConstantCallSite();68testMutableCallSite();69testVolatileCallSite();70}7172private static final int N = Integer.MAX_VALUE / 100;73private static final int RESULT1 = 762786192;74private static final int RESULT2 = -21474836;7576static final CallSite MCS = new MutableCallSite(methodType(void.class));77static final MethodHandle MCS_INVOKER = MCS.dynamicInvoker();7879static void test(boolean shouldThrow) {80try {81MCS_INVOKER.invokeExact();82if (shouldThrow) {83throw new AssertionError("should throw");84}85} catch (IllegalStateException ise) {86if (!shouldThrow) {87throw new AssertionError("should not throw", ise);88}89} catch (Throwable e) {90throw new Error(e);91}92}9394static class MyCCS extends ConstantCallSite {95public MyCCS(MethodType targetType, MethodHandle createTargetHook) throws Throwable {96super(targetType, createTargetHook);97}98}99100private static MethodHandle testConstantCallSiteHandler(CallSite cs, CallSite[] holder) throws Throwable {101holder[0] = cs; // capture call site instance for subsequent checks102103MethodType csType = cs.type(); // should not throw on partially constructed instance104105// Truly dynamic invoker for constant call site106MethodHandle getTarget = lookup().findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class))107.bindTo(cs);108MethodHandle invoker = MethodHandles.exactInvoker(csType);109MethodHandle target = MethodHandles.foldArguments(invoker, getTarget);110111MCS.setTarget(target);112// warmup113for (int i = 0; i < 20_000; i++) {114test(true); // should throw IllegalStateException115}116117return MethodHandles.empty(csType); // initialize cs with an empty method handle118}119120private static void testConstantCallSite() throws Throwable {121CallSite[] holder = new CallSite[1];122MethodHandle handler = lookup().findStatic(CLASS, "testConstantCallSiteHandler", MethodType.methodType(MethodHandle.class, CallSite.class, CallSite[].class));123handler = MethodHandles.insertArguments(handler, 1, new Object[] { holder } );124125CallSite ccs = new MyCCS(MCS.type(), handler); // trigger call to handler126127if (ccs != holder[0]) {128throw new AssertionError("different call site instances");129}130for (int i = 0; i < 20_000; i++) {131test(false); // should not throw132}133}134135private static void testMutableCallSite() throws Throwable {136// warm-up137for (int i = 0; i < 20000; i++) {138mcs.setTarget(mh_foo);139}140// run141for (int n = 0; n < 2; n++) {142mcs.setTarget(mh_foo);143for (int i = 0; i < 5; i++) {144assertEQ(RESULT1, runMutableCallSite());145}146mcs.setTarget(mh_bar);147for (int i = 0; i < 5; i++) {148assertEQ(RESULT2, runMutableCallSite());149}150}151}152private static void testVolatileCallSite() throws Throwable {153// warm-up154for (int i = 0; i < 20000; i++) {155vcs.setTarget(mh_foo);156}157// run158for (int n = 0; n < 2; n++) {159vcs.setTarget(mh_foo);160for (int i = 0; i < 5; i++) {161assertEQ(RESULT1, runVolatileCallSite());162}163vcs.setTarget(mh_bar);164for (int i = 0; i < 5; i++) {165assertEQ(RESULT2, runVolatileCallSite());166}167}168}169170private static int runMutableCallSite() throws Throwable {171int sum = 0;172for (int i = 0; i < N; i++) {173sum += (int) INDY_mcs().invokeExact(i, i+1);174}175return sum;176}177private static int runVolatileCallSite() throws Throwable {178int sum = 0;179for (int i = 0; i < N; i++) {180sum += (int) INDY_vcs().invokeExact(i, i+1);181}182return sum;183}184185static int foo(int a, int b) { return a + b; }186static int bar(int a, int b) { return a - b; }187188private static MethodType MT_bsm() {189shouldNotCallThis();190return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);191}192193private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {194return mcs;195}196private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException {197shouldNotCallThis();198return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm());199}200private static MethodHandle INDY_mcs() throws Throwable {201shouldNotCallThis();202return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();203}204205private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {206return vcs;207}208private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException {209shouldNotCallThis();210return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm());211}212private static MethodHandle INDY_vcs() throws Throwable {213shouldNotCallThis();214return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();215}216217private static void shouldNotCallThis() {218// if this gets called, the transformation has not taken place219throw new AssertionError("this code should be statically transformed away by Indify");220}221}222223224