1 /*
   2  * Copyright (c) 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  * @modules java.base/jdk.internal.misc
  27  * @library /test/lib ..
  28  * @compile sealedP1/superClass.java sealedP1/c1.java sealedP2/c2.java sealedP3/c3.java
  29  * @build sun.hotspot.WhiteBox
  30  * @compile/module=java.base java/lang/ModuleHelper.java
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SealedModuleTest
  34  */
  35 
  36 public class SealedModuleTest {
  37 
  38     // Test sub-classing a sealed super class in a named module. In this test,
  39     // sealed class sealedP1/superClass permits sealedP1.c1, sealedP2.c2, and
  40     // sealedP3.c3.  All three of those classes extend sealedP1/superClass.
  41 
  42     public static void main(String args[]) throws Throwable {
  43         Object m1x, m2x;
  44 
  45         // Get the class loader for SealedModuleTest and assume it's also used
  46         // to load the other classes.
  47         ClassLoader this_cldr = AccessCheckRead.class.getClassLoader();
  48 
  49         // Define a module for packages sealedP1 and sealedP2.
  50         m1x = ModuleHelper.ModuleObject("module_one", this_cldr,
  51                                         new String[] { "sealedP1", "sealedP2" });
  52         ModuleHelper.DefineModule(m1x, false, "9.0", "m1x/here",
  53                                   new String[] { "sealedP1", "sealedP2" });
  54 
  55         // Define a module for package sealedP3 with the same class loader.
  56         m2x = ModuleHelper.ModuleObject("module_two", this_cldr, new String[] { "sealedP3" });
  57         ModuleHelper.DefineModule(m2x, false, "9.0", "m2x/there", new String[] { "sealedP3" });
  58 
  59         // Make package sealedP1 in m1x visible to everyone because it contains
  60         // the super class for c1, c2, and c3.
  61         ModuleHelper.AddModuleExportsToAll(m1x, "sealedP1");
  62         ModuleHelper.AddReadsModule(m2x, m1x);
  63 
  64         // Test subtype in the same named package and named module as its super
  65         // class.  This should succeed.
  66         // Class sealedP1.c1 extends class sealedP1.superClass.
  67         Class p1_c1_class = Class.forName("sealedP1.c1");
  68 
  69         // Test subtype in different named package but same named module as its
  70         // super class. This should succeed.
  71         // Class sealedP2.c2 extends class sealedP1.superClass.
  72         Class p2_c2_class = Class.forName("sealedP2.c2");
  73 
  74         // Test subtype in a different module than its super type.  This should
  75         // fail even though they have the same class loader.
  76         // Class sealedP3.c3 extends class sealedP1.superClass.
  77         try {
  78             Class p3_c3_class = Class.forName("sealedP3.c3");
  79             throw new RuntimeException("Expected VerifyError exception not thrown");
  80         } catch (VerifyError e) {
  81             if (!e.getMessage().contains("cannot inherit from final class")) {
  82                 throw new RuntimeException("Wrong VerifyError exception thrown: " + e.getMessage());
  83             }
  84         }
  85 
  86     }
  87 }