hotspot/test/runtime/modules/ModuleStress/ModuleSameCLMain.java
changeset 43665 4bb003cad9b9
parent 42307 cefc81dc1d52
child 44520 0553e129e0ec
equal deleted inserted replaced
43606:a5aa7536131c 43665:4bb003cad9b9
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    32 import java.util.HashMap;
    32 import java.util.HashMap;
    33 import java.util.Map;
    33 import java.util.Map;
    34 import java.util.Set;
    34 import java.util.Set;
    35 
    35 
    36 //
    36 //
    37 // ClassLoader1 --> defines m1 --> packages p1
    37 // ClassLoader1 --> defines m1x --> packages p1
    38 // ClassLoader1 --> defines m2 --> packages p2
    38 // ClassLoader1 --> defines m2x --> packages p2
    39 //
    39 //
    40 // m1 can read m2
    40 // m1x can read m2x
    41 // package p2 in m2 is exported to m1
    41 // package p2 in m2x is exported to m1x
    42 //
    42 //
    43 // class p1.c1 defined in m1 tries to access p2.c2 defined in m2
    43 // class p1.c1 defined in m1x tries to access p2.c2 defined in m2x
    44 // Access allowed since m1 can read m2 and package p2 is exported to m1.
    44 // Access allowed since m1x can read m2x and package p2 is exported to m1x.
    45 //
    45 //
    46 public class ModuleSameCLMain {
    46 public class ModuleSameCLMain {
    47 
    47 
    48     // Create a Layer over the boot layer.
    48     // Create a Layer over the boot layer.
    49     // Define modules within this layer to test access between
    49     // Define modules within this layer to test access between
    50     // publically defined classes within packages of those modules.
    50     // publically defined classes within packages of those modules.
    51     public void createLayerOnBoot() throws Throwable {
    51     public void createLayerOnBoot() throws Throwable {
    52 
    52 
    53         // Define module:     m1
    53         // Define module:     m1x
    54         // Can read:          java.base, m2
    54         // Can read:          java.base, m2x
    55         // Packages:          p1
    55         // Packages:          p1
    56         // Packages exported: p1 is exported to unqualifiedly
    56         // Packages exported: p1 is exported to unqualifiedly
    57         ModuleDescriptor descriptor_m1 =
    57         ModuleDescriptor descriptor_m1x =
    58                 ModuleDescriptor.module("m1")
    58                 ModuleDescriptor.newModule("m1x")
    59                         .requires("java.base")
    59                         .requires("java.base")
    60                         .requires("m2")
    60                         .requires("m2x")
    61                         .exports("p1")
    61                         .exports("p1")
    62                         .build();
    62                         .build();
    63 
    63 
    64         // Define module:     m2
    64         // Define module:     m2x
    65         // Can read:          java.base
    65         // Can read:          java.base
    66         // Packages:          p2
    66         // Packages:          p2
    67         // Packages exported: package p2 is exported to m1
    67         // Packages exported: package p2 is exported to m1x
    68         ModuleDescriptor descriptor_m2 =
    68         ModuleDescriptor descriptor_m2x =
    69                 ModuleDescriptor.module("m2")
    69                 ModuleDescriptor.newModule("m2x")
    70                         .requires("java.base")
    70                         .requires("java.base")
    71                         .exports("p2", Set.of("m1"))
    71                         .exports("p2", Set.of("m1x"))
    72                         .build();
    72                         .build();
    73 
    73 
    74         // Set up a ModuleFinder containing all modules for this layer.
    74         // Set up a ModuleFinder containing all modules for this layer.
    75         ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2);
    75         ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x);
    76 
    76 
    77         // Resolves "m1"
    77         // Resolves "m1x"
    78         Configuration cf = Layer.boot()
    78         Configuration cf = Layer.boot()
    79                 .configuration()
    79                 .configuration()
    80                 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));
    80                 .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
    81 
    81 
    82         // map each module to the same class loader for this test
    82         // map each module to the same class loader for this test
    83         Map<String, ClassLoader> map = new HashMap<>();
    83         Map<String, ClassLoader> map = new HashMap<>();
    84         Loader1 cl1 = new Loader1();
    84         Loader1 cl1 = new Loader1();
    85         map.put("m1", cl1);
    85         map.put("m1x", cl1);
    86         map.put("m2", cl1);
    86         map.put("m2x", cl1);
    87 
    87 
    88         // Create Layer that contains m1 & m2
    88         // Create Layer that contains m1x & m2x
    89         Layer layer = Layer.boot().defineModules(cf, map::get);
    89         Layer layer = Layer.boot().defineModules(cf, map::get);
    90         assertTrue(layer.findLoader("m1") == cl1);
    90         assertTrue(layer.findLoader("m1x") == cl1);
    91         assertTrue(layer.findLoader("m2") == cl1);
    91         assertTrue(layer.findLoader("m2x") == cl1);
    92         assertTrue(layer.findLoader("java.base") == null);
    92         assertTrue(layer.findLoader("java.base") == null);
    93 
    93 
    94         // now use the same loader to load class p1.c1
    94         // now use the same loader to load class p1.c1
    95         Class p1_c1_class = cl1.loadClass("p1.c1");
    95         Class p1_c1_class = cl1.loadClass("p1.c1");
    96         try {
    96         try {
    97             p1_c1_class.newInstance();
    97             p1_c1_class.newInstance();
    98         } catch (IllegalAccessError e) {
    98         } catch (IllegalAccessError e) {
    99             throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1");
    99             throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1x");
   100         }
   100         }
   101     }
   101     }
   102 
   102 
   103     public static void main(String args[]) throws Throwable {
   103     public static void main(String args[]) throws Throwable {
   104       ModuleSameCLMain test = new ModuleSameCLMain();
   104       ModuleSameCLMain test = new ModuleSameCLMain();