author | rprotacio |
Tue, 26 Apr 2016 09:08:12 -0400 | |
changeset 38094 | 46977cd73d86 |
parent 36508 | 5f9eee6b383b |
child 42307 | cefc81dc1d52 |
permissions | -rw-r--r-- |
36508 | 1 |
/* |
2 |
* Copyright (c) 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 |
/* @test |
|
25 |
* @summary test JNI_GetModule() API |
|
26 |
* @run main/native GetModule |
|
27 |
*/ |
|
28 |
||
29 |
import java.lang.reflect.Module; |
|
30 |
import java.lang.management.LockInfo; |
|
31 |
public class GetModule { |
|
32 |
||
33 |
static { |
|
34 |
System.loadLibrary("GetModule"); |
|
35 |
} |
|
36 |
||
37 |
static native Object callGetModule(java.lang.Class clazz); |
|
38 |
static native void callAddModuleReads(java.lang.reflect.Module from_module, |
|
39 |
java.lang.reflect.Module source_module); |
|
40 |
static native boolean callCanReadModule(java.lang.reflect.Module asking_module, |
|
41 |
java.lang.reflect.Module source_module); |
|
42 |
||
43 |
public static void main(String[] args) { |
|
44 |
Module module; |
|
38094
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
45 |
Module javaBaseModule; |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
46 |
|
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
47 |
// Module for primitive type, should be "java.base" |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
48 |
java.lang.Integer primitive_int = 1; |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
49 |
try { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
50 |
javaBaseModule = (Module)callGetModule(primitive_int.getClass()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
51 |
if (!javaBaseModule.getName().equals("java.base")) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
52 |
throw new RuntimeException("Unexpected module name for primitive type: " + |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
53 |
javaBaseModule.getName()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
54 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
55 |
} catch(Throwable e) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
56 |
throw new RuntimeException("Unexpected exception for Integer: " + e.toString()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
57 |
} |
36508 | 58 |
|
59 |
// Module for array of primitives, should be "java.base" |
|
60 |
int[] int_array = {1, 2, 3}; |
|
61 |
try { |
|
62 |
javaBaseModule = (Module)callGetModule(int_array.getClass()); |
|
63 |
if (!javaBaseModule.getName().equals("java.base")) { |
|
64 |
throw new RuntimeException("Unexpected module name for array of primitives: " + |
|
65 |
javaBaseModule.getName()); |
|
66 |
} |
|
67 |
} catch(Throwable e) { |
|
68 |
throw new RuntimeException("Unexpected exception for [I: " + e.toString()); |
|
69 |
} |
|
70 |
||
38094
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
71 |
// Module for multi-dimensional array of primitives, should be "java.base" |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
72 |
int[][] multi_int_array = { {1, 2, 3}, {4, 5, 6} }; |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
73 |
try { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
74 |
javaBaseModule = (Module)callGetModule(multi_int_array.getClass()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
75 |
if (!javaBaseModule.getName().equals("java.base")) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
76 |
throw new RuntimeException("Unexpected module name for multi-dimensional array of primitives: " + |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
77 |
javaBaseModule.getName()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
78 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
79 |
} catch(Throwable e) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
80 |
throw new RuntimeException("Unexpected exception for multi-dimensional Integer array: " + e.toString()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
81 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
82 |
|
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
83 |
// Module for java.lang.String, should be "java.base" |
36508 | 84 |
java.lang.String str = "abc"; |
85 |
try { |
|
86 |
module = (Module)callGetModule(str.getClass()); |
|
87 |
if (!module.getName().equals("java.base")) { |
|
88 |
throw new RuntimeException("Unexpected module name for class String: " + |
|
89 |
module.getName()); |
|
90 |
} |
|
91 |
} catch(Throwable e) { |
|
92 |
throw new RuntimeException("Unexpected exception for String: " + e.toString()); |
|
93 |
} |
|
94 |
||
38094
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
95 |
// Module for array of java.lang.Strings, should be "java.base" |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
96 |
java.lang.String[] str_array = {"a", "b", "c"}; |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
97 |
try { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
98 |
javaBaseModule = (Module)callGetModule(str_array.getClass()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
99 |
if (!javaBaseModule.getName().equals("java.base")) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
100 |
throw new RuntimeException("Unexpected module name for array of Strings: " + |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
101 |
javaBaseModule.getName()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
102 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
103 |
} catch(Throwable e) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
104 |
throw new RuntimeException("Unexpected exception for String array: " + e.toString()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
105 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
106 |
|
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
107 |
// Module for multi-dimensional array of java.lang.Strings, should be "java.base" |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
108 |
java.lang.String[][] multi_str_array = { {"a", "b", "c"}, {"d", "e", "f"} }; |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
109 |
try { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
110 |
javaBaseModule = (Module)callGetModule(multi_str_array.getClass()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
111 |
if (!javaBaseModule.getName().equals("java.base")) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
112 |
throw new RuntimeException("Unexpected module name for multi-dimensional array of Strings: " + |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
113 |
javaBaseModule.getName()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
114 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
115 |
} catch(Throwable e) { |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
116 |
throw new RuntimeException("Unexpected exception for multidimensional String array: " + e.toString()); |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
117 |
} |
46977cd73d86
8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents:
36508
diff
changeset
|
118 |
|
36508 | 119 |
// Module for java.lang.management.LockInfo |
120 |
try { |
|
121 |
LockInfo li = new LockInfo("java.lang.Class", 57); |
|
122 |
module = (Module)callGetModule(li.getClass()); |
|
123 |
if (!module.getName().equals("java.management")) { |
|
124 |
throw new RuntimeException("Unexpected module name for class LockInfo: " + |
|
125 |
module.getName()); |
|
126 |
} |
|
127 |
} catch(Throwable e) { |
|
128 |
throw new RuntimeException("Unexpected exception for LockInfo: " + e.toString()); |
|
129 |
} |
|
130 |
||
131 |
// Unnamed module. |
|
132 |
try { |
|
133 |
module = (Module)callGetModule(MyClassLoader.class); |
|
134 |
if (module == null || module.getName() != null) { |
|
135 |
throw new RuntimeException("Bad module for unnamed module"); |
|
136 |
} |
|
137 |
} catch(Throwable e) { |
|
138 |
throw new RuntimeException("Unexpected exception for unnamed module: " + e.toString()); |
|
139 |
} |
|
140 |
||
141 |
try { |
|
142 |
module = (Module)callGetModule(null); |
|
143 |
throw new RuntimeException("Failed to get expected NullPointerException"); |
|
144 |
} catch(NullPointerException e) { |
|
145 |
// Expected |
|
146 |
} |
|
147 |
||
148 |
||
149 |
// Tests for JNI_AddModuleReads() // |
|
150 |
||
151 |
Module javaScriptingModule = javax.script.Bindings.class.getModule(); |
|
152 |
if (javaScriptingModule == null) { |
|
153 |
throw new RuntimeException("Failed to get java.scripting module"); |
|
154 |
} |
|
155 |
Module javaLoggingModule = java.util.logging.Level.class.getModule(); |
|
156 |
if (javaLoggingModule == null) { |
|
157 |
throw new RuntimeException("Failed to get java.logging module"); |
|
158 |
} |
|
159 |
||
160 |
if (callCanReadModule(javaLoggingModule, javaScriptingModule)) { |
|
161 |
throw new RuntimeException( |
|
162 |
"Expected FALSE because javaLoggingModule cannot read javaScriptingModule"); |
|
163 |
} |
|
164 |
||
165 |
callAddModuleReads(javaLoggingModule, javaScriptingModule); |
|
166 |
callAddModuleReads(javaScriptingModule, GetModule.class.getModule()); // unnamed module |
|
167 |
||
168 |
try { |
|
169 |
callAddModuleReads(null, javaLoggingModule); |
|
170 |
throw new RuntimeException( |
|
171 |
"Expected NullPointerException for bad from_module not thrown"); |
|
172 |
} catch(NullPointerException e) { |
|
173 |
// expected |
|
174 |
} |
|
175 |
||
176 |
try { |
|
177 |
callAddModuleReads(javaLoggingModule, null); |
|
178 |
throw new RuntimeException( |
|
179 |
"Expected NullPointerException for bad source_module not thrown"); |
|
180 |
} catch(NullPointerException e) { |
|
181 |
// expected |
|
182 |
} |
|
183 |
||
184 |
||
185 |
// Tests for JNI_CanReadModule() // |
|
186 |
||
187 |
if (!callCanReadModule(javaLoggingModule, javaScriptingModule)) { |
|
188 |
throw new RuntimeException( |
|
189 |
"Expected TRUE because javaLoggingModule can read javaScriptingModule"); |
|
190 |
} |
|
191 |
||
192 |
if (callCanReadModule(javaBaseModule, javaScriptingModule)) { |
|
193 |
throw new RuntimeException( |
|
194 |
"Expected FALSE because javaBaseModule cannnot read javaScriptingModule"); |
|
195 |
} |
|
196 |
||
197 |
try { |
|
198 |
callCanReadModule(javaLoggingModule, null); |
|
199 |
throw new RuntimeException( |
|
200 |
"Expected NullPointerException for bad sourceModule not thrown"); |
|
201 |
} catch(NullPointerException e) { |
|
202 |
// expected |
|
203 |
} |
|
204 |
||
205 |
try { |
|
206 |
callCanReadModule(null, javaScriptingModule); |
|
207 |
throw new RuntimeException( |
|
208 |
"Expected NullPointerException for bad asking_module not thrown"); |
|
209 |
} catch(NullPointerException e) { |
|
210 |
// expected |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
static class MyClassLoader extends ClassLoader { } |
|
215 |
} |