GetModule.java revision 12290:8953c0318163
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
29import java.lang.reflect.Module;
30import java.lang.management.LockInfo;
31public class GetModule {
32
33    static {
34        System.loadLibrary("GetModule");
35    }
36
37    static native Object callGetModule(java.lang.Class clazz);
38
39    public static void main(String[] args) {
40        Module module;
41        Module javaBaseModule;
42
43        // Module for primitive type, should be "java.base"
44        java.lang.Integer primitive_int = 1;
45        try {
46            javaBaseModule = (Module)callGetModule(primitive_int.getClass());
47            if (!javaBaseModule.getName().equals("java.base")) {
48                throw new RuntimeException("Unexpected module name for primitive type: " +
49                                           javaBaseModule.getName());
50            }
51        } catch(Throwable e) {
52            throw new RuntimeException("Unexpected exception for Integer: " + e.toString());
53        }
54
55        // Module for array of primitives, should be "java.base"
56        int[] int_array = {1, 2, 3};
57        try {
58            javaBaseModule = (Module)callGetModule(int_array.getClass());
59            if (!javaBaseModule.getName().equals("java.base")) {
60                throw new RuntimeException("Unexpected module name for array of primitives: " +
61                                           javaBaseModule.getName());
62            }
63        } catch(Throwable e) {
64            throw new RuntimeException("Unexpected exception for [I: " + e.toString());
65        }
66
67        // Module for multi-dimensional array of primitives, should be "java.base"
68        int[][] multi_int_array = { {1, 2, 3}, {4, 5, 6} };
69        try {
70            javaBaseModule = (Module)callGetModule(multi_int_array.getClass());
71            if (!javaBaseModule.getName().equals("java.base")) {
72                throw new RuntimeException("Unexpected module name for multi-dimensional array of primitives: " +
73                                           javaBaseModule.getName());
74            }
75        } catch(Throwable e) {
76            throw new RuntimeException("Unexpected exception for multi-dimensional Integer array: " + e.toString());
77        }
78
79        // Module for java.lang.String, should be "java.base"
80        java.lang.String str = "abc";
81        try {
82            module = (Module)callGetModule(str.getClass());
83            if (!module.getName().equals("java.base")) {
84                throw new RuntimeException("Unexpected module name for class String: " +
85                                           module.getName());
86            }
87        } catch(Throwable e) {
88            throw new RuntimeException("Unexpected exception for String: " + e.toString());
89        }
90
91        // Module for array of java.lang.Strings, should be "java.base"
92        java.lang.String[] str_array = {"a", "b", "c"};
93        try {
94            javaBaseModule = (Module)callGetModule(str_array.getClass());
95            if (!javaBaseModule.getName().equals("java.base")) {
96                throw new RuntimeException("Unexpected module name for array of Strings: " +
97                                           javaBaseModule.getName());
98            }
99        } catch(Throwable e) {
100            throw new RuntimeException("Unexpected exception for String array: " + e.toString());
101        }
102
103        // Module for multi-dimensional array of java.lang.Strings, should be "java.base"
104        java.lang.String[][] multi_str_array = { {"a", "b", "c"}, {"d", "e", "f"} };
105        try {
106            javaBaseModule = (Module)callGetModule(multi_str_array.getClass());
107            if (!javaBaseModule.getName().equals("java.base")) {
108                throw new RuntimeException("Unexpected module name for multi-dimensional array of Strings: " +
109                                           javaBaseModule.getName());
110            }
111        } catch(Throwable e) {
112            throw new RuntimeException("Unexpected exception for multidimensional String array: " + e.toString());
113        }
114
115        // Module for java.lang.management.LockInfo
116        try {
117            LockInfo li = new LockInfo("java.lang.Class", 57);
118            module = (Module)callGetModule(li.getClass());
119            if (!module.getName().equals("java.management")) {
120                throw new RuntimeException("Unexpected module name for class LockInfo: " +
121                                           module.getName());
122            }
123        } catch(Throwable e) {
124            throw new RuntimeException("Unexpected exception for LockInfo: " + e.toString());
125        }
126
127        // Unnamed module.
128        try {
129            module = (Module)callGetModule(MyClassLoader.class);
130            if (module == null || module.getName() != null) {
131                throw new RuntimeException("Bad module for unnamed module");
132            }
133        } catch(Throwable e) {
134            throw new RuntimeException("Unexpected exception for unnamed module: " + e.toString());
135        }
136
137        try {
138            module = (Module)callGetModule(null);
139            throw new RuntimeException("Failed to get expected NullPointerException");
140        } catch(NullPointerException e) {
141            // Expected
142        }
143    }
144
145    static class MyClassLoader extends ClassLoader { }
146}
147