Esempio n. 1
1
  // Create a Layer over the boot layer.
  // Define modules within this layer to test access between
  // publically defined classes within packages of those modules.
  public void createLayerOnBoot() throws Throwable {

    // Define module:     m1
    // Can read:          java.base, m2
    // Packages:          p1
    // Packages exported: p1 is exported unqualifiedly
    ModuleDescriptor descriptor_m1 =
        new ModuleDescriptor.Builder("m1")
            .requires("java.base")
            .requires("m2")
            .exports("p1")
            .build();

    // Define module:     m2
    // Can read:          java.base
    // Packages:          p2
    // Packages exported: p2 is exported unqualifiedly
    ModuleDescriptor descriptor_m2 =
        new ModuleDescriptor.Builder("m2").requires("java.base").exports("p2").build();

    // Set up a ModuleFinder containing all modules for this layer.
    ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2);

    // Resolves "m1"
    Configuration cf =
        Layer.boot().configuration().resolveRequires(finder, ModuleFinder.of(), Set.of("m1"));

    // map each module to the same class loader for this test
    Map<String, ClassLoader> map = new HashMap<>();
    map.put("m1", MySameClassLoader.loader1);
    map.put("m2", MySameClassLoader.loader1);

    // Create Layer that contains m1 & m2
    Layer layer = Layer.boot().defineModules(cf, map::get);

    assertTrue(layer.findLoader("m1") == MySameClassLoader.loader1);
    assertTrue(layer.findLoader("m2") == MySameClassLoader.loader1);

    // now use the same loader to load class p1.c1
    Class p1_c1_class = MySameClassLoader.loader1.loadClass("p1.c1");
    try {
      p1_c1_class.newInstance();
    } catch (IllegalAccessError e) {
      throw new RuntimeException(
          "Test Failed, an IAE should not be thrown since p2 is exported unqualifiedly.");
    }
  }
Esempio n. 2
0
  public static void main(String[] args) throws Exception {

    final FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
    final Path root = fs.getPath("/modules");

    final List<String> names =
        Files.walk(root)
            .filter(p -> p.getNameCount() > 2)
            .filter(p -> Layer.boot().findModule(p.getName(1).toString()).isPresent())
            .map(p -> p.subpath(2, p.getNameCount()))
            .map(p -> p.toString())
            .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
            .collect(Collectors.toList());

    Runnable r =
        new Runnable() {
          @Override
          public void run() {
            names.forEach(
                name -> {
                  String cn = name.substring(0, name.length() - 6).replace('/', '.');
                  try {
                    Class.forName(cn, false, ClassLoader.getSystemClassLoader());
                  } catch (Exception ex) {
                    System.err.println(Thread.currentThread() + " " + ex.getClass());
                  }
                });
          }
        };

    Thread[] threads = new Thread[NTHREADS];

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = new Thread(r);
      threads[i] = thread;
      thread.start();
    }

    Thread.sleep(1);

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = threads[i];

      if (thread.isAlive()) {
        thread.interrupt();
        break;
      }
    }

    for (int i = 0; i < NTHREADS; i++) {
      Thread thread = threads[i];
      thread.join();
    }
  }