Path: blob/master/test/jdk/java/lang/invoke/ClassSpecializerTest.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Smoke-test class specializer, used to create BoundMethodHandle classes25* @compile/module=java.base java/lang/invoke/ClassSpecializerHelper.java26* @run testng/othervm/timeout=250 -ea -esa ClassSpecializerTest27*/2829// Useful diagnostics to try:30// -Djava.lang.invoke.MethodHandle.TRACE_RESOLVE=true31// -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true323334import org.testng.annotations.*;35import java.lang.invoke.*;36import java.util.ArrayList;37import java.util.Arrays;38import java.util.List;3940import static java.lang.invoke.ClassSpecializerHelper.*;414243public class ClassSpecializerTest {44@Test45public void testFindSpecies() throws Throwable {46System.out.println("testFindSpecies");47System.out.println("test = " + SPEC_TEST);48ArrayList<Object> args = new ArrayList<>();49for (int key = 0; key <= Kind.MAX_KEY; key++) {50Kind k = SpecTest.kind(key);51System.out.println("k = " + k);52MethodHandle mh = k.factory();53System.out.println("k.f = " + mh);54args.clear();55for (Class<?> pt : mh.type().parameterList()) {56args.add(coughUpA(pt));57}58args.set(0, key * 1000 + 42);59Frob f = (Frob) mh.invokeWithArguments(args.toArray());60assert(f.kind() == k);61System.out.println("k.f(...) = " + f.toString());62List<Object> l = f.asList();63System.out.println("f.l = " + l);64args.subList(0,1).clear(); // drop label65assert(args.equals(l));66}67}68private static Object coughUpA(Class<?> pt) throws Throwable {69if (pt == String.class) return "foo";70if (pt.isArray()) return java.lang.reflect.Array.newInstance(pt.getComponentType(), 2);71if (pt == Integer.class) return 42;72if (pt == Double.class) return 3.14;73if (pt.isAssignableFrom(List.class))74return Arrays.asList("hello", "world", "from", pt.getSimpleName());75return MethodHandles.zero(pt).invoke();76}77public static void main(String... av) throws Throwable {78System.out.println("TEST: ClassSpecializerTest");79new ClassSpecializerTest().testFindSpecies();80}81}828384