1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Test that if module m1x can read module m2x, AND package p2 in m2x is
  27  *          exported qualifiedly to m1x, then class p1.c1 in m1x can read p2.c2 in m2x.
  28  *          However, p1.c1 tries to access a private method within p2.c2, verify
  29  *          that the IAE message contains the correct loader and module names.
  30  * @modules java.base/jdk.internal.misc
  31  * @library /test/lib
  32  * @compile p1/c1.jasm
  33  * @compile p2/c2.jasm
  34  * @compile myloaders/MySameClassLoader.java
  35  * @run main/othervm -Xbootclasspath/a:. ExpQualToM1PrivateMethodIAE
  36  */
  37 
  38 import static jdk.test.lib.Asserts.*;
  39 
  40 import java.lang.module.Configuration;
  41 import java.lang.module.ModuleDescriptor;
  42 import java.lang.module.ModuleFinder;
  43 import java.util.HashMap;
  44 import java.util.Map;
  45 import java.util.Set;
  46 import myloaders.MySameClassLoader;
  47 
  48 public class ExpQualToM1PrivateMethodIAE {
  49 
  50     // Create a layer over the boot layer.
  51     // Define modules within this layer to test access between
  52     // publically defined classes within packages of those modules.
  53     public void createLayerOnBoot() throws Throwable {
  54 
  55         // Define module:     m1x
  56         // Can read:          java.base, m2x
  57         // Packages:          p1
  58         // Packages exported: p1 is exported unqualifiedly
  59         ModuleDescriptor descriptor_m1x =
  60                 ModuleDescriptor.newModule("m1x")
  61                         .requires("java.base")
  62                         .requires("m2x")
  63                         .exports("p1")
  64                         .build();
  65 
  66         // Define module:     m2x
  67         // Can read:          java.base
  68         // Packages:          p2
  69         // Packages exported: p2 is exported qualifiedly to m1x
  70         ModuleDescriptor descriptor_m2x =
  71                 ModuleDescriptor.newModule("m2x")
  72                         .requires("java.base")
  73                         .exports("p2", Set.of("m1x"))
  74                         .build();
  75 
  76         // Set up a ModuleFinder containing all modules for this layer.
  77         ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x);
  78 
  79         // Resolves "m1x"
  80         Configuration cf = ModuleLayer.boot()
  81                 .configuration()
  82                 .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
  83 
  84         // map each module to the same class loader for this test
  85         Map<String, ClassLoader> map = new HashMap<>();
  86         map.put("m1x", MySameClassLoader.loader1);
  87         map.put("m2x", MySameClassLoader.loader1);
  88 
  89         // Create layer that contains m1x & m2x
  90         ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
  91 
  92         assertTrue(layer.findLoader("m1x") == MySameClassLoader.loader1);
  93         assertTrue(layer.findLoader("m2x") == MySameClassLoader.loader1);
  94 
  95         // now use the same loader to load class p1.c1
  96         Class p1_c1_class = MySameClassLoader.loader1.loadClass("p1.c1");
  97         try {
  98             p1_c1_class.newInstance();
  99             throw new RuntimeException("Test Failed, an IAE should be thrown since p2/c2's method2 is private");
 100         } catch (IllegalAccessError e) {
 101             String message = e.getMessage();
 102             System.out.println(e.toString());
 103             // java.lang.IllegalAccessError:
 104             //   tried to access private method p2.c2.method2()V from class p1.c1 (p2.c2 is in module m2x of loader
 105             //   myloaders.MySameClassLoader @<id>; p1.c1 is in module m1x of loader myloaders.MySameClassLoader @<id>)
 106             if (!message.contains("class p1.c1 tried to access private method 'void p2.c2.method2()' " +
 107                                   "(p1.c1 is in module m1x of loader myloaders.MySameClassLoader @") ||
 108                 !message.contains("; p2.c2 is in module m2x of loader myloaders.MySameClassLoader @")) {
 109               throw new RuntimeException("Test Failed, an IAE was thrown with the wrong message: " + e.toString());
 110             }
 111         } catch (Throwable e) {
 112             throw new RuntimeException("Test Failed, an IAE should be thrown since p2/c2's method2 is private");
 113         }
 114     }
 115 
 116     public static void main(String args[]) throws Throwable {
 117       ExpQualToM1PrivateMethodIAE test = new ExpQualToM1PrivateMethodIAE();
 118       test.createLayerOnBoot();
 119     }
 120 }