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 /java/lang/invoke/common
 29  * @build test.java.lang.invoke.lib.InstructionHelper
 30  * @enablePreview
 31  * @run testng CondyBSMInvocation
 32  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyBSMInvocation
 33  */
 34 
 35 
 36 import org.testng.Assert;
 37 import org.testng.annotations.Test;
 38 import test.java.lang.invoke.lib.InstructionHelper;
 39 
 40 import java.lang.constant.ConstantDesc;
 41 import java.lang.invoke.MethodHandle;
 42 import java.lang.invoke.MethodHandles;
 43 import java.lang.invoke.MethodType;
 44 import java.lang.invoke.WrongMethodTypeException;
 45 import java.util.Arrays;
 46 import java.util.Collections;
 47 import java.util.stream.IntStream;
 48 import java.util.stream.Stream;
 49 
 50 import static java.lang.invoke.MethodType.methodType;
 51 
 52 public class CondyBSMInvocation {
 53     static final MethodHandles.Lookup L = MethodHandles.lookup();
 54 
 55 
 56     @Test
 57     public void testNonexistent() throws Throwable {
 58         MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 59                 L, "name", Object.class,
 60                 "bsm", methodType(Object.class)
 61         );
 62 
 63         try {
 64             mh.invoke();
 65             Assert.fail("NoSuchMethodError expected to be thrown");
 66         } catch (NoSuchMethodError e) {
 67         }
 68     }
 69 
 70     static MethodHandle[] bsms(String bsmName) {
 71         return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()).
 72                 filter(m -> m.getName().equals(bsmName)).
 73                 map(m -> {
 74                     try {
 75                         return MethodHandles.lookup().unreflect(m);
 76                     } catch (IllegalAccessException e) {
 77                         throw new RuntimeException();
 78                     }
 79                 }).toArray(MethodHandle[]::new);
 80     }
 81 
 82     public static Object shape_bsm() {
 83         return "0";
 84     }
 85 
 86     public static Object shape_bsm(Object a1) {
 87         return "0";
 88     }
 89 
 90     public static Object shape_bsm(Object... args) {
 91         return "0";
 92     }
 93 
 94     public static Object shape_bsm(Object a1, Object a2) {
 95         return "0";
 96     }
 97 
 98     public static Object shape_bsm(Object a1, Object... args) {
 99         return "0";
100     }
101 
102     public static Object shape_bsm(Object a1, Object a2, Object a3) {
103         return "0";
104     }
105 
106     public static Object shape_bsm(MethodHandles.Lookup a1) {
107         return "0";
108     }
109 
110     @Test
111     public void testWrongShape() throws Throwable {
112         for (MethodHandle bsm : bsms("shape_bsm")) {
113             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
114                     L, "name", Object.class,
115                     "shape_bsm", bsm.type()
116             );
117 
118             try {
119                 Object r = mh.invoke();
120                 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
121             } catch (BootstrapMethodError e) {
122             }
123         }
124     }
125 
126 
127     public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) {
128         return "0";
129     }
130 
131     public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) {
132         return "0";
133     }
134 
135     @Test
136     public void testWrongSignature() throws Throwable {
137         for (MethodHandle bsm : bsms("sig_bsm")) {
138             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
139                     L, "name", Object.class,
140                     "sig_bsm", bsm.type()
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                     IntStream.range(0, n).boxed().toArray(ConstantDesc[]::new)
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                     IntStream.range(0, 9).boxed().toArray(ConstantDesc[]::new)
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                     IntStream.range(0, n - 1).boxed().toArray(ConstantDesc[]::new)
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 }