author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 43523 | 67007ed41226 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
import java.lang.invoke.MethodHandle; |
|
25 |
import java.lang.invoke.MethodHandleProxies; |
|
26 |
import java.lang.invoke.MethodHandles; |
|
27 |
import java.lang.invoke.MethodType; |
|
28 |
import java.lang.reflect.Constructor; |
|
29 |
import java.lang.reflect.InvocationHandler; |
|
30 |
import java.lang.reflect.InvocationTargetException; |
|
31 |
||
32 |
import org.testng.annotations.BeforeTest; |
|
33 |
import org.testng.annotations.Test; |
|
34 |
import static org.testng.Assert.*; |
|
35 |
||
36 |
/** |
|
37 |
* @test |
|
38 |
* @summary test MethodHandleProxies that adds qualified export of sun.invoke |
|
39 |
* from java.base to a dynamic module |
|
40 |
* @run testng ProxyForMethodHandle |
|
41 |
*/ |
|
42 |
public class ProxyForMethodHandle { |
|
43 |
/** |
|
44 |
* MethodHandleProxies will add qualified export of sun.invoke from java.base |
|
45 |
* to a dynamic module |
|
46 |
*/ |
|
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 | 49 |
MethodHandles.Lookup lookup = MethodHandles.lookup(); |
50 |
MethodType mt = MethodType.methodType(void.class); |
|
51 |
MethodHandle mh = lookup.findStatic(ProxyForMethodHandle.class, "runForRunnable", mt); |
|
52 |
Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh); |
|
53 |
proxy.run(); |
|
54 |
||
55 |
Class<?> proxyClass = proxy.getClass(); |
|
56 |
Module target = proxyClass.getModule(); |
|
57 |
assertDynamicModule(target, proxyClass.getClassLoader(), proxyClass); |
|
58 |
} |
|
59 |
||
60 |
static void runForRunnable() { |
|
61 |
System.out.println("runForRunnable"); |
|
62 |
} |
|
63 |
||
64 |
public static void assertDynamicModule(Module m, ClassLoader ld, Class<?> proxyClass) { |
|
65 |
if (!m.isNamed() || !m.getName().startsWith("jdk.proxy")) { |
|
66 |
throw new RuntimeException(m.getName() + " not dynamic module"); |
|
67 |
} |
|
68 |
||
69 |
if (ld != m.getClassLoader() || proxyClass.getClassLoader() != ld) { |
|
70 |
throw new RuntimeException("unexpected class loader"); |
|
71 |
} |
|
72 |
||
73 |
try { |
|
74 |
Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class); |
|
75 |
cons.newInstance(handler); |
|
76 |
throw new RuntimeException("Expected IllegalAccessException: " + proxyClass); |
|
77 |
} catch (IllegalAccessException e) { |
|
78 |
// expected |
|
79 |
} catch (NoSuchMethodException|InstantiationException|InvocationTargetException e) { |
|
80 |
throw new RuntimeException(e); |
|
81 |
} |
|
82 |
} |
|
83 |
private final static InvocationHandler handler = |
|
84 |
(proxy, m, params) -> { throw new RuntimeException(m.toString()); }; |
|
85 |
} |