CallSiteTest.java revision 5455
0N/A/*
0N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A *
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @summary smoke tests for CallSite
0N/A *
0N/A * @build indify.Indify
0N/A * @compile CallSiteTest.java
0N/A * @run main/othervm
0N/A * indify.Indify
0N/A * --expand-properties --classpath ${test.classes}
0N/A * --java test.java.lang.invoke.CallSiteTest
0N/A */
0N/A
0N/Apackage test.java.lang.invoke;
0N/A
0N/Aimport java.io.*;
0N/A
0N/Aimport java.lang.invoke.*;
0N/Aimport static java.lang.invoke.MethodHandles.*;
0N/Aimport static java.lang.invoke.MethodType.*;
0N/A
0N/Apublic class CallSiteTest {
0N/A private final static Class<?> CLASS = CallSiteTest.class;
0N/A
0N/A private static CallSite mcs;
0N/A private static CallSite vcs;
0N/A private static MethodHandle mh_foo;
0N/A private static MethodHandle mh_bar;
0N/A
0N/A static {
0N/A try {
0N/A mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class));
0N/A mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class));
0N/A mcs = new MutableCallSite(mh_foo);
0N/A vcs = new VolatileCallSite(mh_foo);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A public static void main(String... av) throws Throwable {
0N/A testMutableCallSite();
0N/A testVolatileCallSite();
0N/A }
0N/A
0N/A private final static int N = Integer.MAX_VALUE / 100;
0N/A private final static int RESULT1 = 762786192;
0N/A private final static int RESULT2 = -21474836;
0N/A
0N/A private static void assertEquals(int expected, int actual) {
0N/A if (expected != actual)
0N/A throw new AssertionError("expected: " + expected + ", actual: " + actual);
0N/A }
0N/A
0N/A private static void testMutableCallSite() throws Throwable {
0N/A // warm-up
0N/A for (int i = 0; i < 20000; i++) {
0N/A mcs.setTarget(mh_foo);
0N/A }
0N/A // run
0N/A for (int n = 0; n < 2; n++) {
0N/A mcs.setTarget(mh_foo);
0N/A for (int i = 0; i < 5; i++) {
0N/A assertEquals(RESULT1, runMutableCallSite());
0N/A }
0N/A mcs.setTarget(mh_bar);
0N/A for (int i = 0; i < 5; i++) {
0N/A assertEquals(RESULT2, runMutableCallSite());
0N/A }
0N/A }
0N/A }
0N/A private static void testVolatileCallSite() throws Throwable {
0N/A // warm-up
0N/A for (int i = 0; i < 20000; i++) {
0N/A vcs.setTarget(mh_foo);
0N/A }
0N/A // run
0N/A for (int n = 0; n < 2; n++) {
0N/A vcs.setTarget(mh_foo);
0N/A for (int i = 0; i < 5; i++) {
0N/A assertEquals(RESULT1, runVolatileCallSite());
0N/A }
0N/A vcs.setTarget(mh_bar);
0N/A for (int i = 0; i < 5; i++) {
0N/A assertEquals(RESULT2, runVolatileCallSite());
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static int runMutableCallSite() throws Throwable {
0N/A int sum = 0;
0N/A for (int i = 0; i < N; i++) {
0N/A sum += (int) INDY_mcs().invokeExact(i, i+1);
0N/A }
0N/A return sum;
0N/A }
0N/A private static int runVolatileCallSite() throws Throwable {
0N/A int sum = 0;
0N/A for (int i = 0; i < N; i++) {
0N/A sum += (int) INDY_vcs().invokeExact(i, i+1);
0N/A }
0N/A return sum;
0N/A }
0N/A
0N/A static int foo(int a, int b) { return a + b; }
0N/A static int bar(int a, int b) { return a - b; }
0N/A
0N/A private static MethodType MT_bsm() {
0N/A shouldNotCallThis();
0N/A return methodType(CallSite.class, Lookup.class, String.class, MethodType.class);
0N/A }
0N/A
0N/A private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
0N/A return mcs;
0N/A }
0N/A private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException {
0N/A shouldNotCallThis();
0N/A return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm());
0N/A }
0N/A private static MethodHandle INDY_mcs() throws Throwable {
0N/A shouldNotCallThis();
0N/A return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
0N/A }
0N/A
0N/A private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException {
0N/A return vcs;
0N/A }
0N/A private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException {
0N/A shouldNotCallThis();
0N/A return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm());
0N/A }
0N/A private static MethodHandle INDY_vcs() throws Throwable {
0N/A shouldNotCallThis();
0N/A return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker();
0N/A }
0N/A
0N/A private static void shouldNotCallThis() {
0N/A // if this gets called, the transformation has not taken place
0N/A throw new AssertionError("this code should be statically transformed away by Indify");
0N/A }
0N/A}
0N/A