1 /*
   2  * Copyright (c) 2017, 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 /* @test
  25  * @summary Smoke-test class specializer, used to create BoundMethodHandle classes
  26  * @compile/module=java.base java/lang/invoke/ClassSpecializerHelper.java
  27  * @run testng/othervm/timeout=250 -ea -esa ClassSpecializerTest
  28  */
  29 
  30 // Useful diagnostics to try:
  31 //   -Djava.lang.invoke.MethodHandle.TRACE_RESOLVE=true
  32 //   -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true
  33 
  34 
  35 import org.testng.annotations.*;
  36 import java.lang.invoke.*;
  37 import java.util.ArrayList;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 
  41 import static java.lang.invoke.ClassSpecializerHelper.*;
  42 
  43 
  44 public class ClassSpecializerTest {
  45     @Test
  46     public void testFindSpecies() throws Throwable {
  47         System.out.println("testFindSpecies");
  48         System.out.println("test = " + SPEC_TEST);
  49         ArrayList<Object> args = new ArrayList<>();
  50         for (int key = 0; key <= Kind.MAX_KEY; key++) {
  51             Kind k = SpecTest.kind(key);
  52             System.out.println("k = " + k);
  53             MethodHandle mh = k.factory();
  54             System.out.println("k.f = " + mh);
  55             args.clear();
  56             for (Class<?> pt : mh.type().parameterList()) {
  57                 args.add(coughUpA(pt));
  58             }
  59             args.set(0, key * 1000 + 42);
  60             Frob f = (Frob) mh.invokeWithArguments(args.toArray());
  61             assert(f.kind() == k);
  62             System.out.println("k.f(...) = " + f.toString());
  63             List<Object> l = f.asList();
  64             System.out.println("f.l = " + l);
  65             args.subList(0,1).clear();  // drop label
  66             assert(args.equals(l));
  67         }
  68     }
  69     private static Object coughUpA(Class<?> pt) throws Throwable {
  70         if (pt == String.class)  return "foo";
  71         if (pt.isArray()) return java.lang.reflect.Array.newInstance(pt.getComponentType(), 2);
  72         if (pt == Integer.class)  return 42;
  73         if (pt == Double.class)  return 3.14;
  74         if (pt.isAssignableFrom(List.class))
  75             return Arrays.asList("hello", "world", "from", pt.getSimpleName());
  76         return MethodHandles.zero(pt).invoke();
  77     }
  78     public static void main(String... av) throws Throwable {
  79         System.out.println("TEST: ClassSpecializerTest");
  80         new ClassSpecializerTest().testFindSpecies();
  81     }
  82 }