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