CallSiteTest.java revision 6073:cea72c2bf071
1/* 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25/** 26 * @test 27 * @summary smoke tests for CallSite 28 * 29 * @build indify.Indify 30 * @compile CallSiteTest.java 31 * @run main/othervm 32 * indify.Indify 33 * --expand-properties --classpath ${test.classes} 34 * --java test.java.lang.invoke.CallSiteTest 35 */ 36 37package test.java.lang.invoke; 38 39import java.io.*; 40 41import java.lang.invoke.*; 42import static java.lang.invoke.MethodHandles.*; 43import static java.lang.invoke.MethodType.*; 44 45public class CallSiteTest { 46 private final static Class<?> CLASS = CallSiteTest.class; 47 48 private static CallSite mcs; 49 private static CallSite vcs; 50 private static MethodHandle mh_foo; 51 private static MethodHandle mh_bar; 52 53 static { 54 try { 55 mh_foo = lookup().findStatic(CLASS, "foo", methodType(int.class, int.class, int.class)); 56 mh_bar = lookup().findStatic(CLASS, "bar", methodType(int.class, int.class, int.class)); 57 mcs = new MutableCallSite(mh_foo); 58 vcs = new VolatileCallSite(mh_foo); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 64 public static void main(String... av) throws Throwable { 65 testMutableCallSite(); 66 testVolatileCallSite(); 67 } 68 69 private final static int N = Integer.MAX_VALUE / 100; 70 private final static int RESULT1 = 762786192; 71 private final static int RESULT2 = -21474836; 72 73 private static void assertEquals(int expected, int actual) { 74 if (expected != actual) 75 throw new AssertionError("expected: " + expected + ", actual: " + actual); 76 } 77 78 private static void testMutableCallSite() throws Throwable { 79 // warm-up 80 for (int i = 0; i < 20000; i++) { 81 mcs.setTarget(mh_foo); 82 } 83 // run 84 for (int n = 0; n < 2; n++) { 85 mcs.setTarget(mh_foo); 86 for (int i = 0; i < 5; i++) { 87 assertEquals(RESULT1, runMutableCallSite()); 88 } 89 mcs.setTarget(mh_bar); 90 for (int i = 0; i < 5; i++) { 91 assertEquals(RESULT2, runMutableCallSite()); 92 } 93 } 94 } 95 private static void testVolatileCallSite() throws Throwable { 96 // warm-up 97 for (int i = 0; i < 20000; i++) { 98 vcs.setTarget(mh_foo); 99 } 100 // run 101 for (int n = 0; n < 2; n++) { 102 vcs.setTarget(mh_foo); 103 for (int i = 0; i < 5; i++) { 104 assertEquals(RESULT1, runVolatileCallSite()); 105 } 106 vcs.setTarget(mh_bar); 107 for (int i = 0; i < 5; i++) { 108 assertEquals(RESULT2, runVolatileCallSite()); 109 } 110 } 111 } 112 113 private static int runMutableCallSite() throws Throwable { 114 int sum = 0; 115 for (int i = 0; i < N; i++) { 116 sum += (int) INDY_mcs().invokeExact(i, i+1); 117 } 118 return sum; 119 } 120 private static int runVolatileCallSite() throws Throwable { 121 int sum = 0; 122 for (int i = 0; i < N; i++) { 123 sum += (int) INDY_vcs().invokeExact(i, i+1); 124 } 125 return sum; 126 } 127 128 static int foo(int a, int b) { return a + b; } 129 static int bar(int a, int b) { return a - b; } 130 131 private static MethodType MT_bsm() { 132 shouldNotCallThis(); 133 return methodType(CallSite.class, Lookup.class, String.class, MethodType.class); 134 } 135 136 private static CallSite bsm_mcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException { 137 return mcs; 138 } 139 private static MethodHandle MH_bsm_mcs() throws ReflectiveOperationException { 140 shouldNotCallThis(); 141 return lookup().findStatic(lookup().lookupClass(), "bsm_mcs", MT_bsm()); 142 } 143 private static MethodHandle INDY_mcs() throws Throwable { 144 shouldNotCallThis(); 145 return ((CallSite) MH_bsm_mcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker(); 146 } 147 148 private static CallSite bsm_vcs(Lookup caller, String name, MethodType type) throws ReflectiveOperationException { 149 return vcs; 150 } 151 private static MethodHandle MH_bsm_vcs() throws ReflectiveOperationException { 152 shouldNotCallThis(); 153 return lookup().findStatic(lookup().lookupClass(), "bsm_vcs", MT_bsm()); 154 } 155 private static MethodHandle INDY_vcs() throws Throwable { 156 shouldNotCallThis(); 157 return ((CallSite) MH_bsm_vcs().invoke(lookup(), "foo", methodType(int.class, int.class, int.class))).dynamicInvoker(); 158 } 159 160 private static void shouldNotCallThis() { 161 // if this gets called, the transformation has not taken place 162 throw new AssertionError("this code should be statically transformed away by Indify"); 163 } 164} 165