author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 40964 | c4a7c6d1cfb3 |
child 44545 | 83b611b88ac8 |
permissions | -rw-r--r-- |
40684
2e37c119dc2a
8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents:
38966
diff
changeset
|
1 |
/* |
36511 | 2 |
* Copyright (c) 2014, 2016, 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 |
/* |
|
25 |
* @test |
|
26 |
* @summary Exercise Class#getModule |
|
27 |
* @modules java.base/jdk.internal.org.objectweb.asm |
|
38966
308f5cc7d5f6
8158855: Fix remaining module dependences in java/lang
shurailine
parents:
36511
diff
changeset
|
28 |
* java.base/jdk.internal.misc |
36511 | 29 |
* java.desktop |
30 |
* @run testng GetModuleTest |
|
31 |
*/ |
|
32 |
||
33 |
import java.awt.Component; |
|
34 |
import java.lang.reflect.Module; |
|
35 |
||
36 |
import jdk.internal.org.objectweb.asm.ClassWriter; |
|
37 |
import static jdk.internal.org.objectweb.asm.Opcodes.*; |
|
38966
308f5cc7d5f6
8158855: Fix remaining module dependences in java/lang
shurailine
parents:
36511
diff
changeset
|
38 |
import jdk.internal.misc.Unsafe; |
36511 | 39 |
|
40 |
import org.testng.annotations.DataProvider; |
|
41 |
import org.testng.annotations.Test; |
|
42 |
import static org.testng.Assert.*; |
|
43 |
||
44 |
public class GetModuleTest { |
|
45 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
40964
diff
changeset
|
46 |
private static final Unsafe U = Unsafe.getUnsafe(); |
36511 | 47 |
|
48 |
private static final Module TEST_MODULE = GetModuleTest.class.getModule(); |
|
49 |
||
50 |
||
51 |
@DataProvider(name = "testclasses") |
|
52 |
public Object[][] testClasses() { |
|
53 |
return new Object[][] { |
|
54 |
||
55 |
// unnamed module |
|
56 |
||
57 |
{ GetModuleTest.class, null }, |
|
58 |
{ GetModuleTest[].class, null }, |
|
59 |
{ GetModuleTest[][].class, null }, |
|
60 |
||
61 |
// should return named module |
|
62 |
||
63 |
{ int.class, "java.base" }, |
|
64 |
{ int[].class, "java.base" }, |
|
65 |
{ int[][].class, "java.base" }, |
|
66 |
{ void.class, "java.base" }, |
|
67 |
||
68 |
{ Object.class, "java.base" }, |
|
69 |
{ Object[].class, "java.base" }, |
|
70 |
{ Object[][].class, "java.base" }, |
|
71 |
{ Component.class, "java.desktop" }, |
|
72 |
{ Component[].class, "java.desktop" }, |
|
73 |
{ Component[][].class, "java.desktop" }, |
|
74 |
}; |
|
75 |
} |
|
76 |
||
77 |
@Test(dataProvider = "testclasses") |
|
78 |
public void testGetModule(Class<?> type, String expected) { |
|
79 |
Module m = type.getModule(); |
|
80 |
assertNotNull(m); |
|
81 |
if (expected == null) { |
|
82 |
assertTrue(m == TEST_MODULE); |
|
83 |
} else { |
|
84 |
assertEquals(m.getName(), expected); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
||
89 |
@DataProvider(name = "hostclasses") |
|
90 |
public Object[][] hostClasses() { |
|
91 |
return new Object[][] { |
|
92 |
||
93 |
{ GetModuleTest.class, null }, |
|
94 |
{ Object.class, null }, |
|
95 |
{ Component.class, null }, |
|
96 |
||
97 |
}; |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Exercise Class::getModule on VM anonymous classes |
|
102 |
*/ |
|
103 |
@Test(dataProvider = "hostclasses") |
|
104 |
public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) { |
|
105 |
||
106 |
// choose a class name in the same package as the host class |
|
40964
c4a7c6d1cfb3
8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents:
40684
diff
changeset
|
107 |
String prefix = hostClass.getPackageName(); |
36511 | 108 |
if (prefix.length() > 0) |
109 |
prefix = prefix.replace('.', '/') + "/"; |
|
110 |
String className = prefix + "Anon"; |
|
111 |
||
112 |
// create the class |
|
113 |
String superName = "java/lang/Object"; |
|
114 |
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS |
|
115 |
+ ClassWriter.COMPUTE_FRAMES); |
|
116 |
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, |
|
117 |
className, null, superName, null); |
|
118 |
byte[] classBytes = cw.toByteArray(); |
|
119 |
int cpPoolSize = constantPoolSize(classBytes); |
|
120 |
Class<?> anonClass |
|
121 |
= U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]); |
|
122 |
||
123 |
assertTrue(anonClass.getModule() == hostClass.getModule()); |
|
124 |
} |
|
125 |
||
126 |
private static int constantPoolSize(byte[] classFile) { |
|
127 |
return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF); |
|
128 |
} |
|
129 |
||
130 |
} |