1 /* 2 * Copyright (c) 2017, 2018, 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 * @test 26 * @bug 8186046 8199875 27 * @summary Test basic invocation of bootstrap methods 28 * @library /lib/testlibrary/bytecode /java/lang/invoke/common 29 * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper 30 * @run testng CondyBSMInvocation 31 * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyBSMInvocation 32 */ 33 34 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 import test.java.lang.invoke.lib.InstructionHelper; 38 39 import java.lang.invoke.MethodHandle; 40 import java.lang.invoke.MethodHandles; 41 import java.lang.invoke.MethodType; 42 import java.lang.invoke.WrongMethodTypeException; 43 import java.util.Arrays; 44 import java.util.Collections; 45 import java.util.stream.IntStream; 46 import java.util.stream.Stream; 47 48 import static java.lang.invoke.MethodType.methodType; 49 50 public class CondyBSMInvocation { 51 static final MethodHandles.Lookup L = MethodHandles.lookup(); 52 53 54 @Test 55 public void testNonexistent() throws Throwable { 56 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 57 L, "name", Object.class, 58 "bsm", methodType(Object.class), 59 S -> {}); 60 61 try { 62 mh.invoke(); 63 Assert.fail("NoSuchMethodError expected to be thrown"); 64 } catch (NoSuchMethodError e) { 65 } 66 } 67 68 static MethodHandle[] bsms(String bsmName) { 69 return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()). 70 filter(m -> m.getName().equals(bsmName)). 71 map(m -> { 72 try { 73 return MethodHandles.lookup().unreflect(m); 74 } catch (IllegalAccessException e) { 75 throw new RuntimeException(); 76 } 77 }).toArray(MethodHandle[]::new); 78 } 79 80 public static Object shape_bsm() { 81 return "0"; 82 } 83 84 public static Object shape_bsm(Object a1) { 85 return "0"; 86 } 87 88 public static Object shape_bsm(Object... args) { 89 return "0"; 90 } 91 92 public static Object shape_bsm(Object a1, Object a2) { 93 return "0"; 94 } 95 96 public static Object shape_bsm(Object a1, Object... args) { 97 return "0"; 98 } 99 100 public static Object shape_bsm(Object a1, Object a2, Object a3) { 101 return "0"; 102 } 103 104 public static Object shape_bsm(MethodHandles.Lookup a1) { 105 return "0"; 106 } 107 108 @Test 109 public void testWrongShape() throws Throwable { 110 for (MethodHandle bsm : bsms("shape_bsm")) { 111 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 112 L, "name", Object.class, 113 "shape_bsm", bsm.type(), 114 S -> {} 115 ); 116 117 try { 118 Object r = mh.invoke(); 119 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm); 120 } catch (BootstrapMethodError e) { 121 } 122 } 123 } 124 125 126 public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) { 127 return "0"; 128 } 129 130 public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) { 131 return "0"; 132 } 133 134 @Test 135 public void testWrongSignature() throws Throwable { 136 for (MethodHandle bsm : bsms("sig_bsm")) { 137 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 138 L, "name", Object.class, 139 "sig_bsm", bsm.type(), 140 S -> {} 141 ); 142 143 try { 144 Object r = mh.invoke(); 145 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm); 146 } catch (BootstrapMethodError e) { 147 } 148 } 149 } 150 151 152 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) { 153 return "0"; 154 } 155 156 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 157 Object a1) { 158 assertAll(a1); 159 return "1"; 160 } 161 162 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 163 Object a1, Object a2) { 164 assertAll(a1, a2); 165 return "2"; 166 } 167 168 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 169 Object a1, Object a2, Object a3) { 170 assertAll(a1, a2, a3); 171 return "3"; 172 } 173 174 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 175 Object a1, Object a2, Object a3, Object a4) { 176 assertAll(a1, a2, a3, a4); 177 return "4"; 178 } 179 180 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 181 Object a1, Object a2, Object a3, Object a4, Object a5) { 182 assertAll(a1, a2, a3, a4, a5); 183 return "5"; 184 } 185 186 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 187 Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { 188 assertAll(a1, a2, a3, a4, a5, a6); 189 return "6"; 190 } 191 192 public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, 193 Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { 194 assertAll(a1, a2, a3, a4, a5, a6, a7); 195 return "7"; 196 } 197 198 public static Object bsm(MethodHandles.Lookup l, Object... args) { 199 Object[] staticArgs = Arrays.copyOfRange(args, 2, args.length); 200 assertAll(staticArgs); 201 return Integer.toString(staticArgs.length); 202 } 203 204 static void assertAll(Object... as) { 205 for (int i = 0; i < as.length; i++) { 206 Assert.assertEquals(as[i], i); 207 } 208 } 209 210 @Test 211 public void testArity() throws Throwable { 212 for (int i = 0; i < 8; i++) { 213 final int n = i; 214 MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class) 215 .appendParameterTypes(Collections.nCopies(n, Object.class)); 216 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 217 L, "name", Object.class, 218 "bsm", mt, 219 S -> IntStream.range(0, n).forEach(S::add) 220 ); 221 222 Object r = mh.invoke(); 223 Assert.assertEquals(r, Integer.toString(n)); 224 } 225 226 { 227 MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, Object[].class); 228 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 229 L, "name", Object.class, 230 "bsm", mt, 231 S -> IntStream.range(0, 9).forEach(S::add) 232 ); 233 234 Object r = mh.invoke(); 235 Assert.assertEquals(r, Integer.toString(9)); 236 237 } 238 } 239 240 @Test 241 public void testWrongNumberOfStaticArguments() throws Throwable { 242 for (int i = 1; i < 8; i++) { 243 final int n = i; 244 MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class) 245 .appendParameterTypes(Collections.nCopies(n, Object.class)); 246 MethodHandle mh = InstructionHelper.ldcDynamicConstant( 247 L, "name", Object.class, 248 "bsm", mt, 249 S -> IntStream.range(0, n - 1).forEach(S::add) 250 ); 251 252 try { 253 Object r = mh.invoke(); 254 Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n); 255 } catch (BootstrapMethodError e) { 256 Throwable t = e.getCause(); 257 Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass())); 258 } 259 } 260 } 261 }