MethodReferenceTestMethodHandle.java revision 3014:a3dd196e5341
1/*
2 * Copyright (c) 2013, 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 8028739
27 * @summary javac generates incorrect descriptor for MethodHandle::invoke
28 * @run testng MethodReferenceTestMethodHandle
29 */
30
31import java.lang.invoke.*;
32import java.util.*;
33
34import org.testng.annotations.Test;
35import static org.testng.Assert.assertEquals;
36
37@Test
38public class MethodReferenceTestMethodHandle {
39
40  MethodHandles.Lookup lookup = MethodHandles.lookup();
41
42  interface ReplaceItf {
43      Object apply(String a, char b, char c) throws Throwable;
44  }
45
46  interface FormatItf {
47      Object apply(String a, Object... args) throws Throwable;
48  }
49
50  interface AddItf {
51      void apply(List st, int idx, Object v) throws Throwable;
52  }
53
54  public void testVirtual() throws Throwable {
55
56      MethodType mt = MethodType.methodType(String.class, char.class, char.class);
57      MethodHandle ms = lookup.findVirtual(String.class, "replace", mt);
58
59      // --- String.replace(String, char, char) ---
60
61      assertEquals("oome otring to oearch", ms.invoke("some string to search", 's', 'o'));
62
63      ReplaceItf f1 = (a, b, c) -> ms.invoke(a,b,c);
64      assertEquals("oome otring to oearch", f1.apply("some string to search", 's', 'o'));
65
66      ReplaceItf f2 = ms::invoke;
67      assertEquals("oome otring to oearch", f2.apply("some string to search", 's', 'o'));
68      assertEquals("oome otring to oearch", f2.apply("some string to search", new Character('s'), 'o'));
69      assertEquals("oome otring to oearch", ((ReplaceItf) ms::invoke).apply("some string to search", 's', 'o'));
70  }
71
72  public void testStatic() throws Throwable {
73      MethodType fmt = MethodType.methodType(String.class, String.class, (new Object[1]).getClass());
74      MethodHandle fms = lookup.findStatic(String.class, "format", fmt);
75
76      // --- String.format(String, Object...) ---
77
78      assertEquals("Testing One 2 3", fms.invoke("Testing %s %d %x", "One", new Integer(2), 3));
79
80      FormatItf ff2 = fms::invoke;
81      assertEquals("Testing One 2 3", ff2.apply("Testing %s %d %x", "One", new Integer(2), 3));
82      assertEquals("Testing One 2 3", ((FormatItf) fms::invoke).apply("Testing %s %d %x", "One", new Integer(2), 3));
83      assertEquals("Testing One 2 3 four", ff2.apply("Testing %s %d %x %s", "One", new Integer(2), 3, "four"));
84  }
85
86  public void testVoid() throws Throwable {
87      MethodType pmt = MethodType.methodType(void.class, int.class, Object.class);
88      MethodHandle pms = lookup.findVirtual(List.class, "add", pmt);
89      List<String> list = new ArrayList<>();
90
91      // --- List.add(int,String) ---
92
93      pms.invoke(list, 0, "Hi");
94
95      AddItf pf2 = pms::invoke;
96      pf2.apply(list, 1, "there");
97      AddItf pf3 = pms::invokeExact;
98      pf3.apply(list, 2, "you");
99      assertEquals("Hi", list.get(0));
100      assertEquals("there", list.get(1));
101      assertEquals("you", list.get(2));
102   }
103}
104