hotspot/test/runtime/modules/AccessCheck/DiffCL_Umod.java
author alanb
Fri, 07 Apr 2017 08:04:46 +0000
changeset 44520 0553e129e0ec
parent 43665 4bb003cad9b9
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
/*
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/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.  Oracle designates this
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    25
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    26
/*
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @test
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    28
 * @summary class p1.c1 defined in m1x tries to access p2.c2 defined in unnamed module.
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38306
diff changeset
    29
 * @library /test/lib
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 36508
diff changeset
    30
 * @modules java.base/jdk.internal.misc
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @modules java.base/jdk.internal.module
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @compile myloaders/MyDiffClassLoader.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @compile p2/c2.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 * @compile p1/c1.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 * @compile p1/c1ReadEdgeDiffLoader.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 * @compile p1/c1Loose.java
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * @run main/othervm -Xbootclasspath/a:. DiffCL_Umod
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import static jdk.test.lib.Asserts.*;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.lang.module.Configuration;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.lang.module.ModuleDescriptor;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.lang.module.ModuleFinder;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.HashMap;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Map;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Set;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import myloaders.MyDiffClassLoader;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
//
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    51
// ClassLoader1 --> defines m1x --> packages p1
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    52
//                  package p1 in m1x is exported unqualifiedly
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
//
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    54
// class p1.c1 defined in m1x tries to access p2.c2 defined in
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
// in unnamed module.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
//
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
// Three access attempts occur in this test:
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
//   1. The first access is not allowed because a strict module
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
//      cannot read an unnamed module.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
//   2. In this scenario a strict module establishes readability
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
//      to the particular unnamed module it is trying to access.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
//      Access is allowed.
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    63
//   3. Module m1x in the test_looseModuleLayer() method
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
//      is transitioned to a loose module, access
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
//      to all unnamed modules is allowed.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
//
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
public class DiffCL_Umod {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    69
 // Create layers over the boot layer to test different
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 // accessing scenarios of a named module to an unnamed module.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    72
 // Module m1x is a strict module and has not established
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 // readability to an unnamed module that p2.c2 is defined in.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
 public void test_strictModuleLayer() throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    76
     // Define module:     m1x
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     // Can read:          java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     // Packages:          p1
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     // Packages exported: p1 is exported unqualifiedly
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    80
     ModuleDescriptor descriptor_m1x =
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    81
             ModuleDescriptor.newModule("m1x")
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                     .requires("java.base")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                     .exports("p1")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                     .build();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     // Set up a ModuleFinder containing all modules for this layer.
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    87
     ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    89
     // Resolves "m1x"
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    90
     Configuration cf = ModuleLayer.boot()
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
             .configuration()
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    92
             .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    94
     MyDiffClassLoader.loader1 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    95
     MyDiffClassLoader.loader2 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    96
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
    97
     // map module m1x to class loader.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     // class c2 will be loaded in an unnamed module/loader2
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     // to achieve differing class loaders.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     Map<String, ClassLoader> map = new HashMap<>();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   101
     map.put("m1x", MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   102
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   103
     // Create layer that contains m1x
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   104
     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   105
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   106
     assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     assertTrue(layer.findLoader("java.base") == null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   108
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     // now use the same loader to load class p1.c1
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   111
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     // Attempt access
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   114
         p1_c1_class.newInstance();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   115
         throw new RuntimeException("Test Failed, strict module m1x should not be able " +
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                                    "to access public type p2.c2 defined in unnamed module");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     } catch (IllegalAccessError e) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   119
}
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   120
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   121
 // Module m1x is a strict module and has established
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   122
 // readability to an unnamed module that p2.c2 is defined in.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   123
 public void test_strictModuleUnnamedReadableLayer() throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   124
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   125
     // Define module:     m1x
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     // Can read:          java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     // Packages:          p1
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   128
     // Packages exported: p1 is exported unqualifiedly
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   129
     ModuleDescriptor descriptor_m1x =
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   130
             ModuleDescriptor.newModule("m1x")
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                     .requires("java.base")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                     .exports("p1")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                     .build();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   134
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     // Set up a ModuleFinder containing all modules for this layer.
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   136
     ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   137
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   138
     // Resolves "m1x"
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   139
     Configuration cf = ModuleLayer.boot()
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   140
             .configuration()
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   141
             .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   142
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   143
     MyDiffClassLoader.loader1 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   144
     MyDiffClassLoader.loader2 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   145
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   146
     // map module m1x to class loader.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   147
     // class c2 will be loaded in an unnamed module/loader2
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     // to achieve differing class loaders.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   149
     Map<String, ClassLoader> map = new HashMap<>();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   150
     map.put("m1x", MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   151
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   152
     // Create layer that contains m1x
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   153
     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   154
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   155
     assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     assertTrue(layer.findLoader("java.base") == null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   157
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   158
     // now use the same loader to load class p1.c1ReadEdgeDiffLoader
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1ReadEdgeDiffLoader");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   160
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   161
     try {
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   162
        // Read edge between m1x and the unnamed module that loads p2.c2 is established in
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        // c1ReadEdgeDiffLoader's ctor before attempting access.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        p1_c1_class.newInstance();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     } catch (IllegalAccessError e) {
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   166
         throw new RuntimeException("Test Failed, module m1x has established readability to p2/c2 loader's " +
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                                    "unnamed module, access should be allowed: " + e.getMessage());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   168
     }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   169
 }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   170
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   171
 // Module m1x is a loose module and thus can read all unnamed modules.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   172
 public void test_looseModuleLayer() throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   173
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   174
     // Define module:     m1x
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   175
     // Can read:          java.base
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   176
     // Packages:          p1
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   177
     // Packages exported: p1 is exported unqualifiedly
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   178
     ModuleDescriptor descriptor_m1x =
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   179
             ModuleDescriptor.newModule("m1x")
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                     .requires("java.base")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                     .exports("p1")
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                     .build();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   183
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   184
     // Set up a ModuleFinder containing all modules for this layer.
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   185
     ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   186
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   187
     // Resolves "m1x"
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   188
     Configuration cf = ModuleLayer.boot()
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   189
             .configuration()
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   190
             .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   191
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   192
     MyDiffClassLoader.loader1 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   193
     MyDiffClassLoader.loader2 = new MyDiffClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   194
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   195
     // map module m1x to class loader.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   196
     // class c2 will be loaded in an unnamed module/loader2
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   197
     // to achieve differing class loaders.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   198
     Map<String, ClassLoader> map = new HashMap<>();
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   199
     map.put("m1x", MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   200
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   201
     // Create layer that contains m1x
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   202
     ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   203
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   204
     assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     assertTrue(layer.findLoader("java.base") == null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   206
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     // now use the same loader to load class p1.c1Loose
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1Loose");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   209
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   210
     // change m1x to read all unnamed modules
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   211
     Module m1x = layer.findModule("m1x").get();
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   212
     jdk.internal.module.Modules.addReadsAllUnnamed(m1x);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   213
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   215
         p1_c1_class.newInstance();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     } catch (IllegalAccessError e) {
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42307
diff changeset
   217
         throw new RuntimeException("Test Failed, loose module m1x should be able to access " +
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                                    "public type p2.c2 defined in unnamed module: " + e.getMessage());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   219
     }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   220
 }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   221
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   222
 public static void main(String args[]) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   223
   DiffCL_Umod test = new DiffCL_Umod();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   224
   test.test_strictModuleLayer();                // access denied
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   225
   test.test_strictModuleUnnamedReadableLayer(); // access allowed
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   226
   test.test_looseModuleLayer();                 // access allowed
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   227
 }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   228
}