java.lang.module.ModuleDescriptor.Opens Java Examples

The following examples show how to use java.lang.module.ModuleDescriptor.Opens. 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: JmodTask.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void validatePackages(ModuleDescriptor descriptor, Set<String> packages) {
    Set<String> nonExistPackages = new TreeSet<>();
    descriptor.exports().stream()
        .map(Exports::source)
        .filter(pn -> !packages.contains(pn))
        .forEach(nonExistPackages::add);

    descriptor.opens().stream()
        .map(Opens::source)
        .filter(pn -> !packages.contains(pn))
        .forEach(nonExistPackages::add);

    if (!nonExistPackages.isEmpty()) {
        throw new CommandException("err.missing.export.or.open.packages",
            descriptor.name(), nonExistPackages);
    }
}
 
Example #2
Source File: SecurityManager.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the non-exported packages of the specified module.
 */
private static Set<String> nonExportedPkgs(ModuleDescriptor md) {
    // start with all packages in the module
    Set<String> pkgs = new HashSet<>(md.packages());

    // remove the non-qualified exported packages
    md.exports().stream()
                .filter(p -> !p.isQualified())
                .map(Exports::source)
                .forEach(pkgs::remove);

    // remove the non-qualified open packages
    md.opens().stream()
              .filter(p -> !p.isQualified())
              .map(Opens::source)
              .forEach(pkgs::remove);

    return pkgs;
}
 
Example #3
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testOpensToTargets() {
    Set<String> targets = new HashSet<>();
    targets.add("bar");
    targets.add("gus");
    Opens o = ModuleDescriptor.newModule("foo")
            .opens("p", targets)
            .build()
            .opens()
            .iterator()
            .next();
    assertEquals(o, o);
    assertTrue(o.modifiers().isEmpty());
    assertEquals(o.source(), "p");
    assertTrue(o.isQualified());
    assertTrue(o.targets().size() == 2);
    assertTrue(o.targets().contains("bar"));
    assertTrue(o.targets().contains("gus"));
}
 
Example #4
Source File: SecurityManager.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the non-exported packages of the specified module.
 */
private static Set<String> nonExportedPkgs(ModuleDescriptor md) {
    // start with all packages in the module
    Set<String> pkgs = new HashSet<>(md.packages());

    // remove the non-qualified exported packages
    md.exports().stream()
                .filter(p -> !p.isQualified())
                .map(Exports::source)
                .forEach(pkgs::remove);

    // remove the non-qualified open packages
    md.opens().stream()
              .filter(p -> !p.isQualified())
              .map(Opens::source)
              .forEach(pkgs::remove);

    return pkgs;
}
 
Example #5
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void newOpens(Set<Opens.Modifier> ms, String pn, Set<String> targets) {
    int modifiersSetIndex = dedupSetBuilder.indexOfOpensModifiers(ms);
    if (!targets.isEmpty()) {
        int stringSetIndex = dedupSetBuilder.indexOfStringSet(targets);
        mv.visitVarInsn(ALOAD, modifiersSetIndex);
        mv.visitLdcInsn(pn);
        mv.visitVarInsn(ALOAD, stringSetIndex);
        mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
            "newOpens", OPENS_MODIFIER_SET_STRING_SET_SIG, false);
    } else {
        mv.visitVarInsn(ALOAD, modifiersSetIndex);
        mv.visitLdcInsn(pn);
        mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
            "newOpens", OPENS_MODIFIER_SET_STRING_SIG, false);
    }
}
 
Example #6
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void dedups(ModuleDescriptor md) {
    // exports
    for (Exports e : md.exports()) {
        dedupSetBuilder.stringSet(e.targets());
        dedupSetBuilder.exportsModifiers(e.modifiers());
    }

    // opens
    for (Opens opens : md.opens()) {
        dedupSetBuilder.stringSet(opens.targets());
        dedupSetBuilder.opensModifiers(opens.modifiers());
    }

    // requires
    for (Requires r : md.requires()) {
        dedupSetBuilder.requiresModifiers(r.modifiers());
    }

    // uses
    dedupSetBuilder.stringSet(md.uses());
}
 
Example #7
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validates if exported and open packages are present
 */
void validatePackages() {
    Set<String> nonExistPackages = new TreeSet<>();
    descriptor.exports().stream()
        .map(Exports::source)
        .filter(pn -> !packages.contains(pn))
        .forEach(nonExistPackages::add);

    descriptor.opens().stream()
        .map(Opens::source)
        .filter(pn -> !packages.contains(pn))
        .forEach(nonExistPackages::add);

    if (!nonExistPackages.isEmpty()) {
        throw new PluginException("Packages that are exported or open in "
            + descriptor.name() + " are not present: " + nonExistPackages);
    }
}
 
Example #8
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Opens opens(Set<Opens.Modifier> mods, String pn, String target) {
    return ModuleDescriptor.newModule("foo")
            .opens(mods, pn, Set.of(target))
            .build()
            .opens()
            .iterator()
            .next();
}
 
Example #9
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Opens opens(Set<Opens.Modifier> mods, String pn) {
    return ModuleDescriptor.newModule("foo")
            .opens(mods, pn)
            .build()
            .opens()
            .iterator()
            .next();
}
 
Example #10
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensToTarget() {
    Opens o = opens("p", "bar");
    assertEquals(o, o);
    assertTrue(o.modifiers().isEmpty());
    assertEquals(o.source(), "p");
    assertTrue(o.isQualified());
    assertTrue(o.targets().size() == 1);
    assertTrue(o.targets().contains("bar"));
}
 
Example #11
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensToAll() {
    Opens o = opens("p");
    assertEquals(o, o);
    assertTrue(o.modifiers().isEmpty());
    assertEquals(o.source(), "p");
    assertFalse(o.isQualified());
    assertTrue(o.targets().isEmpty());
}
 
Example #12
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a new {@code ModuleDescriptor} instance.
 */
ModuleDescriptor newModuleDescriptor(String name,
                                     Version version,
                                     Set<ModuleDescriptor.Modifier> ms,
                                     Set<Requires> requires,
                                     Set<Exports> exports,
                                     Set<Opens> opens,
                                     Set<String> uses,
                                     Set<Provides> provides,
                                     Set<String> packages,
                                     String mainClass,
                                     int hashCode);
 
Example #13
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensCompare() {
    Opens o1 = opens("p");
    Opens o2 = opens("p");
    assertEquals(o1, o2);
    assertTrue(o1.hashCode() == o2.hashCode());
    assertTrue(o1.compareTo(o2) == 0);
    assertTrue(o2.compareTo(o1) == 0);
}
 
Example #14
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensCompareWithSameModifiers() {
    Opens o1 = opens(Set.of(Opens.Modifier.SYNTHETIC), "p");
    Opens o2 = opens(Set.of(Opens.Modifier.SYNTHETIC), "p");
    assertEquals(o1, o2);
    assertTrue(o1.hashCode() == o2.hashCode());
    assertTrue(o1.compareTo(o2) == 0);
    assertTrue(o2.compareTo(o1) == 0);
}
 
Example #15
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensCompareWithDifferentModifiers() {
    Opens o1 = opens(Set.of(Opens.Modifier.SYNTHETIC), "p");
    Opens o2 = opens("p");
    assertNotEquals(o1, o2);
    assertTrue(o1.compareTo(o2) == 1);
    assertTrue(o2.compareTo(o1) == -1);
}
 
Example #16
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new {@code ModuleDescriptor} instance.
 */
ModuleDescriptor newModuleDescriptor(String name,
                                     Version version,
                                     Set<ModuleDescriptor.Modifier> ms,
                                     Set<Requires> requires,
                                     Set<Exports> exports,
                                     Set<Opens> opens,
                                     Set<String> uses,
                                     Set<Provides> provides,
                                     Set<String> packages,
                                     String mainClass,
                                     int hashCode);
 
Example #17
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensCompareWithSameTargets() {
    Opens o1 = opens("p", "x");
    Opens o2 = opens("p", "x");
    assertEquals(o1, o2);
    assertTrue(o1.hashCode() == o2.hashCode());
    assertTrue(o1.compareTo(o2) == 0);
    assertTrue(o2.compareTo(o1) == 0);
}
 
Example #18
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpensCompareWithDifferentTargets() {
    Opens o1 = opens("p", "y");
    Opens o2 = opens("p", "x");
    assertNotEquals(o1, o2);
    assertTrue(o1.compareTo(o2) == 1);
    assertTrue(o2.compareTo(o1) == -1);
}
 
Example #19
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static int modifierToInt(Opens.Modifier modifier) {
  switch(modifier) {
  case MANDATED:
    return ACC_MANDATED;
  case SYNTHETIC:
    return ACC_SYNTHETIC;
  default:
    throw new IllegalStateException("unknown modifier " + modifier);
  }
}
 
Example #20
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void testOpensOpens() {
    Opens o1 = opens("p");
    ModuleDescriptor descriptor = ModuleDescriptor.newModule("m").opens(o1).build();
    Opens o2 = descriptor.opens().iterator().next();
    assertEquals(o1, o2);
}
 
Example #21
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 #22
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
public void testOpensWithDuplicate1() {
    Opens o = opens("p");
    ModuleDescriptor.newModule("foo").opens(o).opens(o);
}
 
Example #23
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class )
public void testOpensWithNullExports() {
    ModuleDescriptor.newModule("foo").opens((Opens) null);
}
 
Example #24
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();
}
 
Example #25
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private Opens opens(String pn, String target) {
    return opens(Set.of(), pn, target);
}
 
Example #26
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private Opens opens(String pn) {
    return opens(Set.of(), pn);
}
 
Example #27
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void opensModifiers(Set<Opens.Modifier> mods) {
    opensModifiersSets.computeIfAbsent(mods, s ->
                    new EnumSetBuilder<>(s, OPENS_MODIFIER_CLASSNAME,
                            enumSetVar, localVarSupplier)
    ).increment();
}
 
Example #28
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 #29
Source File: Builder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns an {@link Opens} for a qualified opens, with
 * the given (and possibly empty) set of modifiers,
 * to a set of target modules.
 */
public static Opens newOpens(Set<Opens.Modifier> ms,
                             String pn,
                             Set<String> targets) {
    return JLMA.newOpens(ms, pn, targets);
}
 
Example #30
Source File: Builder.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an {@link Opens} for a qualified opens, with
 * the given (and possibly empty) set of modifiers,
 * to a set of target modules.
 */
public static Opens newOpens(Set<Opens.Modifier> ms,
                             String pn,
                             Set<String> targets) {
    return JLMA.newOpens(ms, pn, targets);
}