hotspot/test/runtime/modules/AccessCheck/UmodUPkg.java
changeset 44520 0553e129e0ec
parent 43665 4bb003cad9b9
equal deleted inserted replaced
44471:7e8e5ceecf99 44520:0553e129e0ec
    38 import static jdk.test.lib.Asserts.*;
    38 import static jdk.test.lib.Asserts.*;
    39 
    39 
    40 import java.lang.module.Configuration;
    40 import java.lang.module.Configuration;
    41 import java.lang.module.ModuleDescriptor;
    41 import java.lang.module.ModuleDescriptor;
    42 import java.lang.module.ModuleFinder;
    42 import java.lang.module.ModuleFinder;
    43 import java.lang.reflect.Layer;
       
    44 import java.lang.reflect.Module;
       
    45 import java.util.HashMap;
    43 import java.util.HashMap;
    46 import java.util.Map;
    44 import java.util.Map;
    47 import java.util.Set;
    45 import java.util.Set;
    48 import myloaders.MySameClassLoader;
    46 import myloaders.MySameClassLoader;
    49 
    47 
    61 //      to the particular unnamed module it is trying to access.
    59 //      to the particular unnamed module it is trying to access.
    62 //      Access is allowed.
    60 //      Access is allowed.
    63 //
    61 //
    64 public class UmodUPkg {
    62 public class UmodUPkg {
    65 
    63 
    66  // Create Layers over the boot layer to test different
    64  // Create layers over the boot layer to test different
    67  // accessing scenarios of a named module to an unnamed module.
    65  // accessing scenarios of a named module to an unnamed module.
    68 
    66 
    69  // Module m1x is a strict module and has not established
    67  // Module m1x is a strict module and has not established
    70  // readability to an unnamed module that c4 is defined in.
    68  // readability to an unnamed module that c4 is defined in.
    71  public void test_strictModuleLayer() throws Throwable {
    69  public void test_strictModuleLayer() throws Throwable {
    82 
    80 
    83      // Set up a ModuleFinder containing all modules for this layer.
    81      // Set up a ModuleFinder containing all modules for this layer.
    84      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
    82      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
    85 
    83 
    86      // Resolves "m1x"
    84      // Resolves "m1x"
    87      Configuration cf = Layer.boot()
    85      Configuration cf = ModuleLayer.boot()
    88              .configuration()
    86              .configuration()
    89              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    87              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    90 
    88 
    91      // map module m1x to class loader.
    89      // map module m1x to class loader.
    92      // class c4 will be loaded in an unnamed module/loader.
    90      // class c4 will be loaded in an unnamed module/loader.
    93      MySameClassLoader loader = new MySameClassLoader();
    91      MySameClassLoader loader = new MySameClassLoader();
    94      Map<String, ClassLoader> map = new HashMap<>();
    92      Map<String, ClassLoader> map = new HashMap<>();
    95      map.put("m1x", loader);
    93      map.put("m1x", loader);
    96 
    94 
    97      // Create Layer that contains m1x
    95      // Create layer that contains m1x
    98      Layer layer = Layer.boot().defineModules(cf, map::get);
    96      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
    99 
    97 
   100      assertTrue(layer.findLoader("m1x") == loader);
    98      assertTrue(layer.findLoader("m1x") == loader);
   101      assertTrue(layer.findLoader("java.base") == null);
    99      assertTrue(layer.findLoader("java.base") == null);
   102 
   100 
   103      // now use the same loader to load class p3.c3
   101      // now use the same loader to load class p3.c3
   128 
   126 
   129      // Set up a ModuleFinder containing all modules for this layer.
   127      // Set up a ModuleFinder containing all modules for this layer.
   130      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   128      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
   131 
   129 
   132      // Resolves "m1x"
   130      // Resolves "m1x"
   133      Configuration cf = Layer.boot()
   131      Configuration cf = ModuleLayer.boot()
   134              .configuration()
   132              .configuration()
   135              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   133              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
   136 
   134 
   137      MySameClassLoader loader = new MySameClassLoader();
   135      MySameClassLoader loader = new MySameClassLoader();
   138      // map module m1x to class loader.
   136      // map module m1x to class loader.
   139      // class c4 will be loaded in an unnamed module/loader.
   137      // class c4 will be loaded in an unnamed module/loader.
   140      Map<String, ClassLoader> map = new HashMap<>();
   138      Map<String, ClassLoader> map = new HashMap<>();
   141      map.put("m1x", loader);
   139      map.put("m1x", loader);
   142 
   140 
   143      // Create Layer that contains m1x
   141      // Create layer that contains m1x
   144      Layer layer = Layer.boot().defineModules(cf, map::get);
   142      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
   145 
   143 
   146      assertTrue(layer.findLoader("m1x") == loader);
   144      assertTrue(layer.findLoader("m1x") == loader);
   147      assertTrue(layer.findLoader("java.base") == null);
   145      assertTrue(layer.findLoader("java.base") == null);
   148 
   146 
   149      // now use the same loader to load class p3.c3ReadEdge
   147      // now use the same loader to load class p3.c3ReadEdge