hotspot/test/runtime/modules/AccessCheck/Umod.java
changeset 44520 0553e129e0ec
parent 43665 4bb003cad9b9
equal deleted inserted replaced
44471:7e8e5ceecf99 44520:0553e129e0ec
    37  * @run main/othervm -Xbootclasspath/a:. Umod
    37  * @run main/othervm -Xbootclasspath/a:. Umod
    38  */
    38  */
    39 
    39 
    40 import static jdk.test.lib.Asserts.*;
    40 import static jdk.test.lib.Asserts.*;
    41 
    41 
    42 import java.lang.reflect.Layer;
       
    43 import java.lang.module.Configuration;
    42 import java.lang.module.Configuration;
    44 import java.lang.module.ModuleDescriptor;
    43 import java.lang.module.ModuleDescriptor;
    45 import java.lang.module.ModuleFinder;
    44 import java.lang.module.ModuleFinder;
    46 import java.lang.reflect.Module;
       
    47 import java.util.HashMap;
    45 import java.util.HashMap;
    48 import java.util.Map;
    46 import java.util.Map;
    49 import java.util.Set;
    47 import java.util.Set;
    50 import myloaders.MySameClassLoader;
    48 import myloaders.MySameClassLoader;
    51 
    49 
    66 //      is transitioned to a loose module, access
    64 //      is transitioned to a loose module, access
    67 //      to all unnamed modules is allowed.
    65 //      to all unnamed modules is allowed.
    68 //
    66 //
    69 public class Umod {
    67 public class Umod {
    70 
    68 
    71  // Create Layers over the boot layer to test different
    69  // Create layers over the boot layer to test different
    72  // accessing scenarios of a named module to an unnamed module.
    70  // accessing scenarios of a named module to an unnamed module.
    73 
    71 
    74  // Module m1x is a strict module and has not established
    72  // Module m1x is a strict module and has not established
    75  // readability to an unnamed module that p2.c2 is defined in.
    73  // readability to an unnamed module that p2.c2 is defined in.
    76  public void test_strictModuleLayer() throws Throwable {
    74  public void test_strictModuleLayer() throws Throwable {
    87 
    85 
    88      // Set up a ModuleFinder containing all modules for this layer.
    86      // Set up a ModuleFinder containing all modules for this layer.
    89      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
    87      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
    90 
    88 
    91      // Resolves "m1x"
    89      // Resolves "m1x"
    92      Configuration cf = Layer.boot()
    90      Configuration cf = ModuleLayer.boot()
    93              .configuration()
    91              .configuration()
    94              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    92              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    95 
    93 
    96      // map module m1x to class loader.
    94      // map module m1x to class loader.
    97      // class c2 will be loaded in an unnamed module/loader.
    95      // class c2 will be loaded in an unnamed module/loader.
    98      MySameClassLoader loader = new MySameClassLoader();
    96      MySameClassLoader loader = new MySameClassLoader();
    99      Map<String, ClassLoader> map = new HashMap<>();
    97      Map<String, ClassLoader> map = new HashMap<>();
   100      map.put("m1x", loader);
    98      map.put("m1x", loader);
   101 
    99 
   102      // Create Layer that contains m1x
   100      // Create layer that contains m1x
   103      Layer layer = Layer.boot().defineModules(cf, map::get);
   101      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
   104 
   102 
   105      assertTrue(layer.findLoader("m1x") == loader);
   103      assertTrue(layer.findLoader("m1x") == loader);
   106      assertTrue(layer.findLoader("java.base") == null);
   104      assertTrue(layer.findLoader("java.base") == null);
   107 
   105 
   108      // now use the same loader to load class p1.c1
   106      // now use the same loader to load class p1.c1
   133 
   131 
   134      // Set up a ModuleFinder containing all modules for this layer.
   132      // Set up a ModuleFinder containing all modules for this layer.
   135      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   133      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   136 
   134 
   137      // Resolves "m1x"
   135      // Resolves "m1x"
   138      Configuration cf = Layer.boot()
   136      Configuration cf = ModuleLayer.boot()
   139              .configuration()
   137              .configuration()
   140              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   138              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   141 
   139 
   142      MySameClassLoader loader = new MySameClassLoader();
   140      MySameClassLoader loader = new MySameClassLoader();
   143      // map module m1x to class loader.
   141      // map module m1x to class loader.
   144      // class c2 will be loaded in an unnamed module/loader.
   142      // class c2 will be loaded in an unnamed module/loader.
   145      Map<String, ClassLoader> map = new HashMap<>();
   143      Map<String, ClassLoader> map = new HashMap<>();
   146      map.put("m1x", loader);
   144      map.put("m1x", loader);
   147 
   145 
   148      // Create Layer that contains m1x
   146      // Create layer that contains m1x
   149      Layer layer = Layer.boot().defineModules(cf, map::get);
   147      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
   150 
   148 
   151      assertTrue(layer.findLoader("m1x") == loader);
   149      assertTrue(layer.findLoader("m1x") == loader);
   152      assertTrue(layer.findLoader("java.base") == null);
   150      assertTrue(layer.findLoader("java.base") == null);
   153 
   151 
   154      // now use the same loader to load class p1.c1ReadEdge
   152      // now use the same loader to load class p1.c1ReadEdge
   179 
   177 
   180      // Set up a ModuleFinder containing all modules for this layer.
   178      // Set up a ModuleFinder containing all modules for this layer.
   181      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   179      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   182 
   180 
   183      // Resolves "m1x"
   181      // Resolves "m1x"
   184      Configuration cf = Layer.boot()
   182      Configuration cf = ModuleLayer.boot()
   185              .configuration()
   183              .configuration()
   186              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   184              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   187 
   185 
   188      MySameClassLoader loader = new MySameClassLoader();
   186      MySameClassLoader loader = new MySameClassLoader();
   189      // map module m1x to class loader.
   187      // map module m1x to class loader.
   190      // class c2 will be loaded in an unnamed module/loader.
   188      // class c2 will be loaded in an unnamed module/loader.
   191      Map<String, ClassLoader> map = new HashMap<>();
   189      Map<String, ClassLoader> map = new HashMap<>();
   192      map.put("m1x", loader);
   190      map.put("m1x", loader);
   193 
   191 
   194      // Create Layer that contains m1x
   192      // Create layer that contains m1x
   195      Layer layer = Layer.boot().defineModules(cf, map::get);
   193      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
   196 
   194 
   197      assertTrue(layer.findLoader("m1x") == loader);
   195      assertTrue(layer.findLoader("m1x") == loader);
   198      assertTrue(layer.findLoader("java.base") == null);
   196      assertTrue(layer.findLoader("java.base") == null);
   199 
   197 
   200      // now use the same loader to load class p1.c1Loose
   198      // now use the same loader to load class p1.c1Loose