jdk/test/java/lang/reflect/Proxy/ProxyForMethodHandle.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43523 67007ed41226
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.lang.invoke.MethodHandle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.invoke.MethodHandleProxies;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.lang.invoke.MethodHandles;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.lang.invoke.MethodType;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.lang.reflect.Constructor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.reflect.InvocationHandler;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @summary test MethodHandleProxies that adds qualified export of sun.invoke
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * from java.base to a dynamic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @run testng ProxyForMethodHandle
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
public class ProxyForMethodHandle {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
     * MethodHandleProxies will add qualified export of sun.invoke from java.base
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
     * to a dynamic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    @Test
43523
67007ed41226 8173412: @Test in java/lang/annotation and java/lang/reflect/Proxy tests not run
mchung
parents: 36511
diff changeset
    48
    public static void testRunnableMethodHandle() throws Exception {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        MethodHandles.Lookup lookup = MethodHandles.lookup();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        MethodType mt = MethodType.methodType(void.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        MethodHandle mh = lookup.findStatic(ProxyForMethodHandle.class, "runForRunnable", mt);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        proxy.run();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        Class<?> proxyClass = proxy.getClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        Module target = proxyClass.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        assertDynamicModule(target, proxyClass.getClassLoader(), proxyClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    static void runForRunnable() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        System.out.println("runForRunnable");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    public static void assertDynamicModule(Module m, ClassLoader ld, Class<?> proxyClass) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        if (!m.isNamed() || !m.getName().startsWith("jdk.proxy")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            throw new RuntimeException(m.getName() + " not dynamic module");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        if (ld != m.getClassLoader() || proxyClass.getClassLoader() != ld) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            throw new RuntimeException("unexpected class loader");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            cons.newInstance(handler);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            throw new RuntimeException("Expected IllegalAccessException: " + proxyClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        } catch (IllegalAccessException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            // expected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        } catch (NoSuchMethodException|InstantiationException|InvocationTargetException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            throw new RuntimeException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private final static InvocationHandler handler =
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            (proxy, m, params) -> { throw new RuntimeException(m.toString()); };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
}