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