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