test/jdk/java/lang/invoke/ClassSpecializerTest.java
author redestad
Thu, 16 Nov 2017 00:58:50 +0100
changeset 47753 a2008587c13f
permissions -rw-r--r--
8184777: Factor out species generation logic from BoundMethodHandle Reviewed-by: vlivanov Contributed-by: john.r.rose@oracle.com, claes.redestad@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47753
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     1
/*
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     4
 *
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     8
 *
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    13
 * accompanied this code).
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    14
 *
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    18
 *
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    21
 * questions.
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    22
 */
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    23
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    24
/* @test
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    25
 * @summary Smoke-test class specializer, used to create BoundMethodHandle classes
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    26
 * @compile/module=java.base java/lang/invoke/ClassSpecializerHelper.java
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    27
 * @run testng/othervm/timeout=250 -ea -esa ClassSpecializerTest
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    28
 */
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    29
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    30
// Useful diagnostics to try:
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    31
//   -Djava.lang.invoke.MethodHandle.TRACE_RESOLVE=true
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    32
//   -Djava.lang.invoke.MethodHandle.DUMP_CLASS_FILES=true
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    33
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    34
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    35
import org.testng.annotations.*;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    36
import java.lang.invoke.*;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    37
import java.util.ArrayList;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    38
import java.util.Arrays;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    39
import java.util.List;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    40
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    41
import static java.lang.invoke.ClassSpecializerHelper.*;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    42
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    43
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    44
public class ClassSpecializerTest {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    45
    @Test
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    46
    public void testFindSpecies() throws Throwable {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    47
        System.out.println("testFindSpecies");
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    48
        System.out.println("test = " + SPEC_TEST);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    49
        ArrayList<Object> args = new ArrayList<>();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    50
        for (int key = 0; key <= Kind.MAX_KEY; key++) {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    51
            Kind k = SpecTest.kind(key);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    52
            System.out.println("k = " + k);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    53
            MethodHandle mh = k.factory();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    54
            System.out.println("k.f = " + mh);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    55
            args.clear();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    56
            for (Class<?> pt : mh.type().parameterList()) {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    57
                args.add(coughUpA(pt));
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    58
            }
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    59
            args.set(0, key * 1000 + 42);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    60
            Frob f = (Frob) mh.invokeWithArguments(args.toArray());
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    61
            assert(f.kind() == k);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    62
            System.out.println("k.f(...) = " + f.toString());
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    63
            List<Object> l = f.asList();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    64
            System.out.println("f.l = " + l);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    65
            args.subList(0,1).clear();  // drop label
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    66
            assert(args.equals(l));
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    67
        }
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    68
    }
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    69
    private static Object coughUpA(Class<?> pt) throws Throwable {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    70
        if (pt == String.class)  return "foo";
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    71
        if (pt.isArray()) return java.lang.reflect.Array.newInstance(pt.getComponentType(), 2);
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    72
        if (pt == Integer.class)  return 42;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    73
        if (pt == Double.class)  return 3.14;
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    74
        if (pt.isAssignableFrom(List.class))
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    75
            return Arrays.asList("hello", "world", "from", pt.getSimpleName());
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    76
        return MethodHandles.zero(pt).invoke();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    77
    }
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    78
    public static void main(String... av) throws Throwable {
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    79
        System.out.println("TEST: ClassSpecializerTest");
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    80
        new ClassSpecializerTest().testFindSpecies();
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    81
    }
a2008587c13f 8184777: Factor out species generation logic from BoundMethodHandle
redestad
parents:
diff changeset
    82
}