< prev index next >

test/runtime/modules/JVMAddModuleExportToAllUnnamed.java

Print this page




  38 
  39 public class JVMAddModuleExportToAllUnnamed {
  40 
  41     // Check that a class in a package in module_one cannot access a class
  42     // that is in the unnamed module if the accessing package is strict.
  43     public static void main(String args[]) throws Throwable {
  44         Object m1x;
  45 
  46         // Get the java.lang.Module object for module java.base.
  47         Class jlObject = Class.forName("java.lang.Object");
  48         Object jlObject_jlM = jlObject.getModule();
  49         assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null");
  50 
  51         // Get the class loader for JVMAddModuleExportToAllUnnamed and assume it's also used to
  52         // load class p1.c1.
  53         ClassLoader this_cldr = JVMAddModuleExportToAllUnnamed.class.getClassLoader();
  54 
  55         // Define a module for p1.
  56         m1x = ModuleHelper.ModuleObject("module_one", this_cldr, new String[] { "p1" });
  57         assertNotNull(m1x, "Module should not be null");
  58         ModuleHelper.DefineModule(m1x, "9.0", "m1x/here", new String[] { "p1" });
  59         ModuleHelper.AddReadsModule(m1x, jlObject_jlM);
  60 
  61         // Make package p1 in m1x visible to everyone.
  62         ModuleHelper.AddModuleExportsToAll(m1x, "p1");
  63 
  64         // p1.c1's ctor tries to call a method in p2.c2.  This should not work
  65         // because p2 is in the unnamed module and p1.c1 is strict.
  66         try {
  67             Class p1_c1_class = Class.forName("p1.c1");
  68             p1_c1_class.newInstance();
  69         } catch (IllegalAccessError e) {
  70             System.out.println(e.getMessage());
  71             if (!e.getMessage().contains("does not read unnamed module")) {
  72                 throw new RuntimeException("Wrong message: " + e.getMessage());
  73             }
  74         }
  75     }
  76 }


  38 
  39 public class JVMAddModuleExportToAllUnnamed {
  40 
  41     // Check that a class in a package in module_one cannot access a class
  42     // that is in the unnamed module if the accessing package is strict.
  43     public static void main(String args[]) throws Throwable {
  44         Object m1x;
  45 
  46         // Get the java.lang.Module object for module java.base.
  47         Class jlObject = Class.forName("java.lang.Object");
  48         Object jlObject_jlM = jlObject.getModule();
  49         assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null");
  50 
  51         // Get the class loader for JVMAddModuleExportToAllUnnamed and assume it's also used to
  52         // load class p1.c1.
  53         ClassLoader this_cldr = JVMAddModuleExportToAllUnnamed.class.getClassLoader();
  54 
  55         // Define a module for p1.
  56         m1x = ModuleHelper.ModuleObject("module_one", this_cldr, new String[] { "p1" });
  57         assertNotNull(m1x, "Module should not be null");
  58         ModuleHelper.DefineModule(m1x, false, "9.0", "m1x/here", new String[] { "p1" });
  59         ModuleHelper.AddReadsModule(m1x, jlObject_jlM);
  60 
  61         // Make package p1 in m1x visible to everyone.
  62         ModuleHelper.AddModuleExportsToAll(m1x, "p1");
  63 
  64         // p1.c1's ctor tries to call a method in p2.c2.  This should not work
  65         // because p2 is in the unnamed module and p1.c1 is strict.
  66         try {
  67             Class p1_c1_class = Class.forName("p1.c1");
  68             p1_c1_class.newInstance();
  69         } catch (IllegalAccessError e) {
  70             System.out.println(e.getMessage());
  71             if (!e.getMessage().contains("does not read unnamed module")) {
  72                 throw new RuntimeException("Wrong message: " + e.getMessage());
  73             }
  74         }
  75     }
  76 }
< prev index next >