hotspot/test/runtime/modules/getModuleJNI/GetModule.java
author alanb
Fri, 07 Apr 2017 08:04:46 +0000
changeset 44520 0553e129e0ec
parent 42307 cefc81dc1d52
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: lfoltan, sspitsyn Contributed-by: alan.bateman@oracle.com, harold.seigel@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42307
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    23
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/* @test
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @summary test JNI_GetModule() API
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @run main/native GetModule
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    28
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.management.LockInfo;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    30
public class GetModule {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
    static {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
        System.loadLibrary("GetModule");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
    static native Object callGetModule(java.lang.Class clazz);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
    public static void main(String[] args) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
        Module module;
38094
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    40
        Module javaBaseModule;
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    41
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    42
        // 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
    43
        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
    44
        try {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    45
            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
    46
            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
    47
                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
    48
                                           javaBaseModule.getName());
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    49
            }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    50
        } catch(Throwable e) {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    51
            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
    52
        }
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        // Module for array of primitives, should be "java.base"
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        int[] int_array = {1, 2, 3};
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            javaBaseModule = (Module)callGetModule(int_array.getClass());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            if (!javaBaseModule.getName().equals("java.base")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
                throw new RuntimeException("Unexpected module name for array of primitives: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
                                           javaBaseModule.getName());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        } catch(Throwable e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            throw new RuntimeException("Unexpected exception for [I: " + e.toString());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
38094
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    66
        // 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
    67
        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
    68
        try {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    69
            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
    70
            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
    71
                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
    72
                                           javaBaseModule.getName());
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    73
            }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    74
        } catch(Throwable e) {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    75
            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
    76
        }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    77
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    78
        // Module for java.lang.String, should be "java.base"
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        java.lang.String str = "abc";
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            module = (Module)callGetModule(str.getClass());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            if (!module.getName().equals("java.base")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                throw new RuntimeException("Unexpected module name for class String: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                                           module.getName());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        } catch(Throwable e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            throw new RuntimeException("Unexpected exception for String: " + e.toString());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
38094
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    90
        // 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
    91
        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
    92
        try {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    93
            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
    94
            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
    95
                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
    96
                                           javaBaseModule.getName());
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    97
            }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    98
        } catch(Throwable e) {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
    99
            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
   100
        }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   101
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   102
        // 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
   103
        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
   104
        try {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   105
            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
   106
            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
   107
                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
   108
                                           javaBaseModule.getName());
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   109
            }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   110
        } catch(Throwable e) {
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   111
            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
   112
        }
46977cd73d86 8152844: JVM InstanceKlass Methods For Obtaining Package/Module Should Be Moved to Klass
rprotacio
parents: 36508
diff changeset
   113
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        // Module for java.lang.management.LockInfo
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            LockInfo li = new LockInfo("java.lang.Class", 57);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            module = (Module)callGetModule(li.getClass());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            if (!module.getName().equals("java.management")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                throw new RuntimeException("Unexpected module name for class LockInfo: " +
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                                           module.getName());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        } catch(Throwable e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            throw new RuntimeException("Unexpected exception for LockInfo: " + e.toString());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   125
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        // Unnamed module.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            module = (Module)callGetModule(MyClassLoader.class);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            if (module == null || module.getName() != null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                throw new RuntimeException("Bad module for unnamed module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        } catch(Throwable e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            throw new RuntimeException("Unexpected exception for unnamed module: " + e.toString());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   135
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            module = (Module)callGetModule(null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            throw new RuntimeException("Failed to get expected NullPointerException");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        } catch(NullPointerException e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            // Expected
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   143
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    static class MyClassLoader extends ClassLoader { }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   145
}