|
1 /* |
|
2 * Copyright (c) 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 8194554 |
|
27 * @run testng/othervm test.java.lang.invoke.FilterArgumentsTest |
|
28 */ |
|
29 |
|
30 package test.java.lang.invoke; |
|
31 |
|
32 import java.lang.invoke.MethodHandle; |
|
33 import java.lang.invoke.MethodHandles; |
|
34 import java.util.ArrayList; |
|
35 import java.util.List; |
|
36 |
|
37 import static java.lang.invoke.MethodHandles.*; |
|
38 import static java.lang.invoke.MethodType.methodType; |
|
39 |
|
40 import org.testng.annotations.*; |
|
41 import static org.testng.Assert.*; |
|
42 |
|
43 public class FilterArgumentsTest { |
|
44 |
|
45 @Test |
|
46 public static void testFilterA_B_C() throws Throwable { |
|
47 FilterTest test = new FilterTest( |
|
48 filterArguments(MH_TEST, 0, MH_FILTER_A, MH_FILTER_B, MH_FILTER_C)); |
|
49 test.run(List.of("A", "B", "C")); |
|
50 } |
|
51 |
|
52 @Test |
|
53 public static void testFilterA_B() throws Throwable { |
|
54 FilterTest test = new FilterTest( |
|
55 filterArguments(MH_TEST, 0, MH_FILTER_A, MH_FILTER_B)); |
|
56 test.run(List.of("A", "B")); |
|
57 } |
|
58 |
|
59 @Test |
|
60 public static void testFilterB_C() throws Throwable { |
|
61 FilterTest test = new FilterTest( |
|
62 filterArguments(MH_TEST, 1, MH_FILTER_B, MH_FILTER_C)); |
|
63 test.run(List.of("B", "C")); |
|
64 } |
|
65 |
|
66 @Test |
|
67 public static void testFilterB() throws Throwable { |
|
68 FilterTest test = new FilterTest(filterArguments(MH_TEST, 1, MH_FILTER_B)); |
|
69 test.run(List.of("B")); |
|
70 } |
|
71 |
|
72 @Test |
|
73 public static void testFilterC() throws Throwable { |
|
74 FilterTest test = new FilterTest(filterArguments(MH_TEST, 2, MH_FILTER_C)); |
|
75 test.run(List.of("C")); |
|
76 } |
|
77 |
|
78 static class FilterTest { |
|
79 static List<String> filters = new ArrayList<>(); |
|
80 |
|
81 final MethodHandle mh; |
|
82 FilterTest(MethodHandle mh) { |
|
83 this.mh = mh; |
|
84 } |
|
85 |
|
86 void run(List<String> expected) throws Throwable { |
|
87 filters.clear(); |
|
88 assertEquals("x-0-z", (String)mh.invokeExact("x", 0, 'z')); |
|
89 assertEquals(expected, filters); |
|
90 } |
|
91 |
|
92 static String filterA(String s) { |
|
93 filters.add("A"); |
|
94 return s; |
|
95 } |
|
96 |
|
97 static int filterB(int value) { |
|
98 filters.add("B"); |
|
99 return value; |
|
100 } |
|
101 |
|
102 static char filterC(char c) { |
|
103 filters.add("C"); |
|
104 return c; |
|
105 } |
|
106 |
|
107 static String test(String s, int i, char c) { |
|
108 return s + "-" + i + "-" + c; |
|
109 } |
|
110 } |
|
111 |
|
112 static final MethodHandle MH_TEST; |
|
113 static final MethodHandle MH_FILTER_A; |
|
114 static final MethodHandle MH_FILTER_B; |
|
115 static final MethodHandle MH_FILTER_C; |
|
116 static final Lookup LOOKUP = MethodHandles.lookup(); |
|
117 |
|
118 static { |
|
119 try { |
|
120 MH_TEST = LOOKUP.findStatic(FilterTest.class, "test", |
|
121 methodType(String.class, String.class, int.class, char.class)); |
|
122 MH_FILTER_A = LOOKUP.findStatic(FilterTest.class, "filterA", |
|
123 methodType(String.class, String.class)); |
|
124 MH_FILTER_B = LOOKUP.findStatic(FilterTest.class, "filterB", |
|
125 methodType(int.class, int.class)); |
|
126 MH_FILTER_C = LOOKUP.findStatic(FilterTest.class, "filterC", |
|
127 methodType(char.class, char.class)); |
|
128 } catch (Exception e) { |
|
129 throw new RuntimeException(e); |
|
130 } |
|
131 } |
|
132 } |