Java Code Examples for java.lang.module.ModuleDescriptor#isOpen()

The following examples show how to use java.lang.module.ModuleDescriptor#isOpen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Module.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new named Module. The resulting Module will be defined to the
 * VM but will not read any other modules, will not have any exports setup
 * and will not be registered in the service catalog.
 */
Module(ModuleLayer layer,
       ClassLoader loader,
       ModuleDescriptor descriptor,
       URI uri)
{
    this.layer = layer;
    this.name = descriptor.name();
    this.loader = loader;
    this.descriptor = descriptor;

    // define module to VM

    boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic();
    Version version = descriptor.version().orElse(null);
    String vs = Objects.toString(version, null);
    String loc = Objects.toString(uri, null);
    String[] packages = descriptor.packages().toArray(new String[0]);
    defineModule0(this, isOpen, vs, loc, packages);
}
 
Example 2
Source File: Module.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new named Module. The resulting Module will be defined to the
 * VM but will not read any other modules, will not have any exports setup
 * and will not be registered in the service catalog.
 */
Module(ModuleLayer layer,
       ClassLoader loader,
       ModuleDescriptor descriptor,
       URI uri)
{
    this.layer = layer;
    this.name = descriptor.name();
    this.loader = loader;
    this.descriptor = descriptor;

    // define module to VM

    boolean isOpen = descriptor.isOpen();
    Version version = descriptor.version().orElse(null);
    String vs = Objects.toString(version, null);
    String loc = Objects.toString(uri, null);
    String[] packages = descriptor.packages().toArray(new String[0]);
    defineModule0(this, isOpen, vs, loc, packages);
}
 
Example 3
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkOpenModule() {
    ModuleDescriptor md = Foo.class.getModule().getDescriptor();
    System.out.println(md);

    if (!md.isOpen()) {
        throw new RuntimeException("m4 is a open module");
    }

    if (md.packages().size() != 1 || !md.packages().contains("p4")) {
        throw new RuntimeException("unexpected m4 packages: " + md.packages());
    }

    if (!md.opens().isEmpty()) {
        throw new RuntimeException("unexpected m4 opens: " + md.opens());
    }
}
 
Example 4
Source File: Loader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the given module opens the given package
 * unconditionally.
 *
 * @implNote This method currently iterates over each of the open
 * packages. This will be replaced once the ModuleDescriptor.Opens
 * API is updated.
 */
private boolean isOpen(ModuleReference mref, String pn) {
    ModuleDescriptor descriptor = mref.descriptor();
    if (descriptor.isOpen() || descriptor.isAutomatic())
        return true;
    for (ModuleDescriptor.Opens opens : descriptor.opens()) {
        String source = opens.source();
        if (!opens.isQualified() && source.equals(pn)) {
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: BuiltinClassLoader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the given module opens the given package
 * unconditionally.
 *
 * @implNote This method currently iterates over each of the open
 * packages. This will be replaced once the ModuleDescriptor.Opens
 * API is updated.
 */
private boolean isOpen(ModuleReference mref, String pn) {
    ModuleDescriptor descriptor = mref.descriptor();
    if (descriptor.isOpen() || descriptor.isAutomatic())
        return true;
    for (ModuleDescriptor.Opens opens : descriptor.opens()) {
        String source = opens.source();
        if (!opens.isQualified() && source.equals(pn)) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: LauncherHelper.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Prints a single line with the module name, version and modifiers
 */
private static void showModule(ModuleReference mref) {
    ModuleDescriptor md = mref.descriptor();
    ostream.print(md.toNameAndVersion());
    mref.location()
            .filter(uri -> !isJrt(uri))
            .ifPresent(uri -> ostream.format(" %s", uri));
    if (md.isOpen())
        ostream.print(" open");
    if (md.isAutomatic())
        ostream.print(" automatic");
    ostream.println();
}
 
Example 7
Source File: Loader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given module opens the given package
 * unconditionally.
 *
 * @implNote This method currently iterates over each of the open
 * packages. This will be replaced once the ModuleDescriptor.Opens
 * API is updated.
 */
private boolean isOpen(ModuleReference mref, String pn) {
    ModuleDescriptor descriptor = mref.descriptor();
    if (descriptor.isOpen() || descriptor.isAutomatic())
        return true;
    for (ModuleDescriptor.Opens opens : descriptor.opens()) {
        String source = opens.source();
        if (!opens.isQualified() && source.equals(pn)) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: BuiltinClassLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given module opens the given package
 * unconditionally.
 *
 * @implNote This method currently iterates over each of the open
 * packages. This will be replaced once the ModuleDescriptor.Opens
 * API is updated.
 */
private boolean isOpen(ModuleReference mref, String pn) {
    ModuleDescriptor descriptor = mref.descriptor();
    if (descriptor.isOpen() || descriptor.isAutomatic())
        return true;
    for (ModuleDescriptor.Opens opens : descriptor.opens()) {
        String source = opens.source();
        if (!opens.isQualified() && source.equals(pn)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: LauncherHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prints a single line with the module name, version and modifiers
 */
private static void showModule(ModuleReference mref) {
    ModuleDescriptor md = mref.descriptor();
    ostream.print(md.toNameAndVersion());
    mref.location()
            .filter(uri -> !isJrt(uri))
            .ifPresent(uri -> ostream.format(" %s", uri));
    if (md.isOpen())
        ostream.print(" open");
    if (md.isAutomatic())
        ostream.print(" automatic");
    ostream.println();
}
 
Example 10
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void describeModule(ModuleDescriptor md,
                            ModuleTarget target,
                            ModuleHashes hashes,
                            String uriString)
    throws IOException
{
    StringBuilder sb = new StringBuilder();

    sb.append(md.toNameAndVersion());

    if (!uriString.equals(""))
        sb.append(" ").append(uriString);
    if (md.isOpen())
        sb.append(" open");
    if (md.isAutomatic())
        sb.append(" automatic");
    sb.append("\n");

    // unqualified exports (sorted by package)
    md.exports().stream()
            .sorted(Comparator.comparing(Exports::source))
            .filter(e -> !e.isQualified())
            .forEach(e -> sb.append("exports ").append(e.source())
                            .append(toString(e.modifiers())).append("\n"));

    // dependences
    md.requires().stream().sorted()
            .forEach(r -> sb.append("requires ").append(r.name())
                            .append(toString(r.modifiers())).append("\n"));

    // service use and provides
    md.uses().stream().sorted()
            .forEach(s -> sb.append("uses ").append(s).append("\n"));

    md.provides().stream()
            .sorted(Comparator.comparing(Provides::service))
            .forEach(p -> sb.append("provides ").append(p.service())
                            .append(" with")
                            .append(toString(p.providers()))
                            .append("\n"));

    // qualified exports
    md.exports().stream()
            .sorted(Comparator.comparing(Exports::source))
            .filter(Exports::isQualified)
            .forEach(e -> sb.append("qualified exports ").append(e.source())
                            .append(" to").append(toString(e.targets()))
                            .append("\n"));

    // open packages
    md.opens().stream()
            .sorted(Comparator.comparing(Opens::source))
            .filter(o -> !o.isQualified())
            .forEach(o -> sb.append("opens ").append(o.source())
                             .append(toString(o.modifiers()))
                             .append("\n"));

    md.opens().stream()
            .sorted(Comparator.comparing(Opens::source))
            .filter(Opens::isQualified)
            .forEach(o -> sb.append("qualified opens ").append(o.source())
                             .append(toString(o.modifiers()))
                             .append(" to").append(toString(o.targets()))
                             .append("\n"));

    // non-exported/non-open packages
    Set<String> concealed = new TreeSet<>(md.packages());
    md.exports().stream().map(Exports::source).forEach(concealed::remove);
    md.opens().stream().map(Opens::source).forEach(concealed::remove);
    concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));

    md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));

    if (target != null) {
        String targetPlatform = target.targetPlatform();
        if (!targetPlatform.isEmpty())
            sb.append("platform ").append(targetPlatform).append("\n");
   }

   if (hashes != null) {
       hashes.names().stream().sorted().forEach(
               mod -> sb.append("hashes ").append(mod).append(" ")
                        .append(hashes.algorithm()).append(" ")
                        .append(toHex(hashes.hashFor(mod)))
                        .append("\n"));
    }

    output(sb.toString());
}
 
Example 11
Source File: JmodTask.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void describeModule(ModuleDescriptor md,
                            ModuleTarget target,
                            ModuleHashes hashes)
    throws IOException
{
    StringBuilder sb = new StringBuilder();

    sb.append(md.toNameAndVersion());

    if (md.isOpen())
        sb.append(" open");
    if (md.isAutomatic())
        sb.append(" automatic");
    sb.append("\n");

    // unqualified exports (sorted by package)
    md.exports().stream()
            .sorted(Comparator.comparing(Exports::source))
            .filter(e -> !e.isQualified())
            .forEach(e -> sb.append("exports ").append(e.source())
                            .append(toString(e.modifiers())).append("\n"));

    // dependences
    md.requires().stream().sorted()
            .forEach(r -> sb.append("requires ").append(r.name())
                            .append(toString(r.modifiers())).append("\n"));

    // service use and provides
    md.uses().stream().sorted()
            .forEach(s -> sb.append("uses ").append(s).append("\n"));

    md.provides().stream()
            .sorted(Comparator.comparing(Provides::service))
            .forEach(p -> sb.append("provides ").append(p.service())
                            .append(" with")
                            .append(toString(p.providers()))
                            .append("\n"));

    // qualified exports
    md.exports().stream()
            .sorted(Comparator.comparing(Exports::source))
            .filter(Exports::isQualified)
            .forEach(e -> sb.append("qualified exports ").append(e.source())
                            .append(" to").append(toString(e.targets()))
                            .append("\n"));

    // open packages
    md.opens().stream()
            .sorted(Comparator.comparing(Opens::source))
            .filter(o -> !o.isQualified())
            .forEach(o -> sb.append("opens ").append(o.source())
                             .append(toString(o.modifiers()))
                             .append("\n"));

    md.opens().stream()
            .sorted(Comparator.comparing(Opens::source))
            .filter(Opens::isQualified)
            .forEach(o -> sb.append("qualified opens ").append(o.source())
                             .append(toString(o.modifiers()))
                             .append(" to").append(toString(o.targets()))
                             .append("\n"));

    // non-exported/non-open packages
    Set<String> concealed = new TreeSet<>(md.packages());
    md.exports().stream().map(Exports::source).forEach(concealed::remove);
    md.opens().stream().map(Opens::source).forEach(concealed::remove);
    concealed.forEach(p -> sb.append("contains ").append(p).append("\n"));

    md.mainClass().ifPresent(v -> sb.append("main-class ").append(v).append("\n"));

    if (target != null) {
        String targetPlatform = target.targetPlatform();
        if (!targetPlatform.isEmpty())
            sb.append("platform ").append(targetPlatform).append("\n");
   }

   if (hashes != null) {
       hashes.names().stream().sorted().forEach(
               mod -> sb.append("hashes ").append(mod).append(" ")
                        .append(hashes.algorithm()).append(" ")
                        .append(toHex(hashes.hashFor(mod)))
                        .append("\n"));
    }

    out.println(sb.toString());
}
 
Example 12
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
public static ModuleDescriptor mergeModuleDescriptor(ModuleDescriptor sourceModule, ModuleDescriptor testModule) {
  var open = sourceModule.isOpen() || testModule.isOpen();

  var moduleModifiers = open? Set.of(Modifier.OPEN): Set.<Modifier>of();
  var builder = ModuleDescriptor.newModule(testModule.name(), moduleModifiers);

  var requires = merge(ModuleDescriptor::requires,
      Requires::name, Requires::modifiers, ModuleHelper::mergeRequiresModifiers, sourceModule, testModule);
  var exports = merge(ModuleDescriptor::exports,
      Exports::source, Exports::targets, ModuleHelper::mergeRestrictions, sourceModule, testModule);
  var packages = merge(ModuleDescriptor::packages,
      x -> x, x -> true, (_1, _2) -> true, sourceModule, testModule);
  var opens = merge(ModuleDescriptor::opens,
      Opens::source, Opens::targets, ModuleHelper::mergeRestrictions, sourceModule, testModule);
  var uses = merge(ModuleDescriptor::uses,
      x -> x, x -> true, (_1, _2) -> true, sourceModule, testModule);
  var provides = merge(ModuleDescriptor::provides,
      Provides::service, p -> new HashSet<>(p.providers()), ModuleHelper::mergeAll, sourceModule, testModule);

  requires.forEach((name, modifiers) -> builder.requires(modifiers, name));
  exports.forEach((source, target) -> {
    if (target.isEmpty()) {
      builder.exports(Set.of(), source);
    } else {
      builder.exports(source, target);
    }
  });
  packages.keySet().removeAll(exports.keySet());
  builder.packages(packages.keySet());
  opens.forEach((source, target) -> {
    if (target.isEmpty()) {
      builder.opens(Set.of(), source);
    } else {
      builder.opens(source, target);
    }
  });
  uses.keySet().forEach(builder::uses);
  provides.forEach((service, providers) -> builder.provides(service, providers.stream().collect(toUnmodifiableList())));

  return builder.build();
}