java.lang.module.ModuleDescriptor.Exports Java Examples

The following examples show how to use java.lang.module.ModuleDescriptor.Exports. 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: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void newExports(Set<Exports.Modifier> ms, String pn, Set<String> targets) {
    int modifiersSetIndex = dedupSetBuilder.indexOfExportsModifiers(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,
            "newExports", EXPORTS_MODIFIER_SET_STRING_SET_SIG, false);
    } else {
        mv.visitVarInsn(ALOAD, modifiersSetIndex);
        mv.visitLdcInsn(pn);
        mv.visitMethodInsn(INVOKESTATIC, MODULE_DESCRIPTOR_BUILDER,
            "newExports", EXPORTS_MODIFIER_SET_STRING_SIG, false);
    }
}
 
Example #4
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 #5
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 #6
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 #7
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testExportsToTargets() {
    Set<String> targets = new HashSet<>();
    targets.add("bar");
    targets.add("gus");
    Exports e
        = ModuleDescriptor.newModule("foo")
            .exports("p", targets)
            .build()
            .exports()
            .iterator()
            .next();
    assertEquals(e, e);
    assertTrue(e.modifiers().isEmpty());
    assertEquals(e.source(), "p");
    assertTrue(e.isQualified());
    assertTrue(e.targets().size() == 2);
    assertTrue(e.targets().contains("bar"));
    assertTrue(e.targets().contains("gus"));
}
 
Example #8
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsToAllWithModifier() {
    Exports e = exports(Set.of(Exports.Modifier.SYNTHETIC), "p");
    assertEquals(e, e);
    assertTrue(e.modifiers().size() == 1);
    assertTrue(e.modifiers().contains(Exports.Modifier.SYNTHETIC));
    assertEquals(e.source(), "p");
    assertFalse(e.isQualified());
    assertTrue(e.targets().isEmpty());
}
 
Example #9
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Exports exports(Set<Exports.Modifier> mods, String pn) {
    return ModuleDescriptor.newModule("foo")
        .exports(mods, pn)
        .build()
        .exports()
        .iterator()
        .next();
}
 
Example #10
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Exports exports(Set<Exports.Modifier> mods, String pn, String target) {
    return ModuleDescriptor.newModule("foo")
        .exports(mods, pn, Set.of(target))
        .build()
        .exports()
        .iterator()
        .next();
}
 
Example #11
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsToAll() {
    Exports e = exports("p");
    assertEquals(e, e);
    assertTrue(e.modifiers().isEmpty());
    assertEquals(e.source(), "p");
    assertFalse(e.isQualified());
    assertTrue(e.targets().isEmpty());
}
 
Example #12
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsToTarget() {
    Exports e = exports("p", "bar");
    assertEquals(e, e);
    assertTrue(e.modifiers().isEmpty());
    assertEquals(e.source(), "p");
    assertTrue(e.isQualified());
    assertTrue(e.targets().size() == 1);
    assertTrue(e.targets().contains("bar"));
}
 
Example #13
Source File: ModuleNamesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "legalModuleNames")
public void testLegalExports(String mn, String expected) throws Exception {
    ModuleDescriptor md = newBuilder("m")
            .requires("java.base")
            .exports("p", Set.of(mn))
            .build();
    ByteBuffer bb = toBuffer(md);
    ModuleDescriptor descriptor = ModuleDescriptor.read(bb);
    Optional<Exports> export = descriptor.exports().stream().findAny();
    assertTrue(export.isPresent());
    assertTrue(export.get().targets().contains(expected));
}
 
Example #14
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsToTargetWithModifier() {
    Exports e = exports(Set.of(Exports.Modifier.SYNTHETIC), "p", "bar");
    assertEquals(e, e);
    assertTrue(e.modifiers().size() == 1);
    assertTrue(e.modifiers().contains(Exports.Modifier.SYNTHETIC));
    assertEquals(e.source(), "p");
    assertTrue(e.isQualified());
    assertTrue(e.targets().size() == 1);
    assertTrue(e.targets().contains("bar"));
}
 
Example #15
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsCompare() {
    Exports e1 = exports("p");
    Exports e2 = exports("p");
    assertEquals(e1, e2);
    assertTrue(e1.hashCode() == e2.hashCode());
    assertTrue(e1.compareTo(e2) == 0);
    assertTrue(e2.compareTo(e1) == 0);
}
 
Example #16
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsCompareWithSameModifiers() {
    Exports e1 = exports(Set.of(Exports.Modifier.SYNTHETIC), "p");
    Exports e2 = exports(Set.of(Exports.Modifier.SYNTHETIC), "p");
    assertEquals(e1, e2);
    assertTrue(e1.hashCode() == e2.hashCode());
    assertTrue(e1.compareTo(e2) == 0);
    assertTrue(e2.compareTo(e1) == 0);
}
 
Example #17
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsCompareWithDifferentModifiers() {
    Exports e1 = exports(Set.of(Exports.Modifier.SYNTHETIC), "p");
    Exports e2 = exports("p");
    assertNotEquals(e1, e2);
    assertTrue(e1.compareTo(e2) == 1);
    assertTrue(e2.compareTo(e1) == -1);
}
 
Example #18
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsCompareWithSameTargets() {
    Exports e1 = exports("p", "x");
    Exports e2 = exports("p", "x");
    assertEquals(e1, e2);
    assertTrue(e1.hashCode() == e2.hashCode());
    assertTrue(e1.compareTo(e2) == 0);
    assertTrue(e2.compareTo(e1) == 0);
}
 
Example #19
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testExportsCompareWithDifferentTargets() {
    Exports e1 = exports("p", "y");
    Exports e2 = exports("p", "x");
    assertNotEquals(e1, e2);
    assertTrue(e1.compareTo(e2) == 1);
    assertTrue(e2.compareTo(e1) == -1);
}
 
Example #20
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testOpenModule() {
    ModuleDescriptor descriptor = ModuleDescriptor.newOpenModule("foo")
            .requires("bar")
            .exports("p")
            .provides("p.Service", List.of("q.ServiceImpl"))
            .build();

    // modifiers
    assertTrue(descriptor.modifiers().contains(ModuleDescriptor.Modifier.OPEN));
    assertTrue(descriptor.isOpen());

    // requires
    assertTrue(descriptor.requires().size() == 2);
    Set<String> names = descriptor.requires()
            .stream()
            .map(Requires::name)
            .collect(Collectors.toSet());
    assertEquals(names, Set.of("bar", "java.base"));

    // packages
    assertEquals(descriptor.packages(), Set.of("p", "q"));

    // exports
    assertTrue(descriptor.exports().size() == 1);
    names = descriptor.exports()
            .stream()
            .map(Exports::source)
            .collect(Collectors.toSet());
    assertEquals(names, Set.of("p"));

    // opens
    assertTrue(descriptor.opens().isEmpty());
}
 
Example #21
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static int modifierToInt(Exports.Modifier modifier) {
  switch(modifier) {
  case MANDATED:
    return ACC_MANDATED;
  case SYNTHETIC:
    return ACC_SYNTHETIC;
  default:
    throw new IllegalStateException("unknown modifier " + modifier);
  }
}
 
Example #22
Source File: Module.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize/setup a module's exports.
 *
 * @param m the module
 * @param nameToModule map of module name to Module (for qualified exports)
 */
private static void initExports(Module m, Map<String, Module> nameToModule) {
    Map<String, Set<Module>> exportedPackages = new HashMap<>();

    for (Exports exports : m.getDescriptor().exports()) {
        String source = exports.source();
        if (exports.isQualified()) {
            // qualified exports
            Set<Module> targets = new HashSet<>();
            for (String target : exports.targets()) {
                Module m2 = nameToModule.get(target);
                if (m2 != null) {
                    addExports0(m, source, m2);
                    targets.add(m2);
                }
            }
            if (!targets.isEmpty()) {
                exportedPackages.put(source, targets);
            }
        } else {
            // unqualified exports
            addExportsToAll0(m, source);
            exportedPackages.put(source, EVERYONE_SET);
        }
    }

    if (!exportedPackages.isEmpty())
        m.exportedPackages = exportedPackages;
}
 
Example #23
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 #24
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 #25
Source File: Builder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a {@link Exports} for a qualified export, with
 * the given (and possibly empty) set of modifiers,
 * to a set of target modules.
 */
public static Exports newExports(Set<Exports.Modifier> ms,
                                 String pn,
                                 Set<String> targets) {
    return JLMA.newExports(ms, pn, targets);
}
 
Example #26
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 #27
Source File: BasicModuleTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a {@code Predicate} to test if a package is exported.
 */
private Predicate<Exports> doesExport(String pn) {
    return e -> (e.source().equals(pn) && !e.isQualified());
}
 
Example #28
Source File: Builder.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link Exports} for a qualified export, with
 * the given (and possibly empty) set of modifiers,
 * to a set of target modules.
 */
public static Exports newExports(Set<Exports.Modifier> ms,
                                 String pn,
                                 Set<String> targets) {
    return JLMA.newExports(ms, pn, targets);
}
 
Example #29
Source File: ModuleInfoBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void printModuleInfo(PrintWriter writer, ModuleDescriptor md) {
    writer.format("%smodule %s {%n", open ? "open " : "", md.name());

    Map<String, Module> modules = configuration.getModules();

    // first print requires
    Set<Requires> reqs = md.requires().stream()
        .filter(req -> !req.name().equals("java.base") && req.modifiers().isEmpty())
        .collect(Collectors.toSet());
    reqs.stream()
        .sorted(Comparator.comparing(Requires::name))
        .forEach(req -> writer.format("    requires %s;%n",
                                      toString(req.modifiers(), req.name())));
    if (!reqs.isEmpty()) {
        writer.println();
    }

    // requires transitive
    reqs = md.requires().stream()
             .filter(req -> !req.name().equals("java.base") && !req.modifiers().isEmpty())
             .collect(Collectors.toSet());
    reqs.stream()
        .sorted(Comparator.comparing(Requires::name))
        .forEach(req -> writer.format("    requires %s;%n",
                                      toString(req.modifiers(), req.name())));
    if (!reqs.isEmpty()) {
        writer.println();
    }

    if (!open) {
        md.exports().stream()
          .peek(exp -> {
              if (exp.isQualified())
                  throw new InternalError(md.name() + " qualified exports: " + exp);
              })
          .sorted(Comparator.comparing(Exports::source))
          .forEach(exp -> writer.format("    exports %s;%n", exp.source()));

        if (!md.exports().isEmpty()) {
            writer.println();
        }
    }

    md.provides().stream()
      .sorted(Comparator.comparing(Provides::service))
      .map(p -> p.providers().stream()
                 .map(impl -> "        " + impl.replace('$', '.'))
                 .collect(joining(",\n",
                                  String.format("    provides %s with%n",
                                                p.service().replace('$', '.')),
                                  ";")))
                 .forEach(writer::println);

    if (!md.provides().isEmpty()) {
        writer.println();
    }
    writer.println("}");
}
 
Example #30
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 testExportsWithNullExports() {
    ModuleDescriptor.newModule("foo").exports((Exports) null);
}