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