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

The following examples show how to use java.lang.module.ModuleDescriptor#name() . 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: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkModuleDescriptor(ModuleDescriptor md, String... packages) throws IOException {
    String mainClass = md.name().replace('m', 'p') + ".Main";
    if (!md.mainClass().get().equals(mainClass)) {
        throw new RuntimeException(md.mainClass().toString());
    }

    if (expectModuleTarget) {
        // ModuleTarget attribute is retained
        if (! hasModuleTarget(md.name())) {
            throw new RuntimeException("ModuleTarget missing for " + md.name());
        }
    } else {
        // by default ModuleTarget attribute is dropped
        if (hasModuleTarget(md.name())) {
            throw new RuntimeException("ModuleTarget present for " + md.name());
        }
    }

    Set<String> pkgs = md.packages();
    if (!pkgs.equals(Set.of(packages))) {
        throw new RuntimeException(pkgs + " expected: " + Set.of(packages));
    }
}
 
Example 3
Source File: Loader.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@code Loader} in a loader pool that loads classes/resources
 * from one module.
 */
public Loader(ResolvedModule resolvedModule,
              LoaderPool pool,
              ClassLoader parent)
{
    super("Loader-" + resolvedModule.name(), parent);

    this.pool = pool;
    this.parent = parent;

    ModuleReference mref = resolvedModule.reference();
    ModuleDescriptor descriptor = mref.descriptor();
    String mn = descriptor.name();
    this.nameToModule = Map.of(mn, mref);

    Map<String, LoadedModule> localPackageToModule = new HashMap<>();
    LoadedModule lm = new LoadedModule(mref);
    descriptor.packages().forEach(pn -> localPackageToModule.put(pn, lm));
    this.localPackageToModule = localPackageToModule;

    this.acc = AccessController.getContext();
}
 
Example 4
Source File: ModuleBootstrap.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Checks for split packages between modules defined to the built-in class
 * loaders.
 */
private static void checkSplitPackages(Configuration cf,
                                       Function<String, ClassLoader> clf) {
    Map<String, String> packageToModule = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
        String name = descriptor.name();
        ClassLoader loader = clf.apply(name);
        if (loader == null || loader instanceof BuiltinClassLoader) {
            for (String p : descriptor.packages()) {
                String other = packageToModule.putIfAbsent(p, name);
                if (other != null) {
                    String msg = "Package " + p + " in both module "
                                 + name + " and module " + other;
                    throw new LayerInstantiationException(msg);
                }
            }
        }
    }
}
 
Example 5
Source File: ModuleLibrary.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void add(ModuleDescriptor... descriptors) {
    for (ModuleDescriptor descriptor: descriptors) {
        String name = descriptor.name();
        if (!namesToReference.containsKey(name)) {
            //modules.add(descriptor);

            URI uri = URI.create("module:/" + descriptor.name());

            ModuleReference mref = new ModuleReference(descriptor, uri) {
                @Override
                public ModuleReader open() {
                    throw new UnsupportedOperationException();
                }
            };

            namesToReference.put(name, mref);
        }
    }
}
 
Example 6
Source File: ModuleDotGraph.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void printNode(PrintWriter out, ModuleDescriptor md, Set<String> edges) {
    Set<String> requiresTransitive = md.requires().stream()
        .filter(d -> d.modifiers().contains(TRANSITIVE))
        .map(d -> d.name())
        .collect(toSet());

    String mn = md.name();
    edges.stream().forEach(dn -> {
        String attr;
        if (dn.equals("java.base")) {
            attr = "color=\"" + attributes.requiresMandatedColor() + "\"";
        } else {
            attr = (requiresTransitive.contains(dn) ? REEXPORTS : REQUIRES);
        }

        int w = attributes.weightOf(mn, dn);
        if (w > 1) {
            if (!attr.isEmpty())
                attr += ", ";

            attr += "weight=" + w;
        }
        out.format("  \"%s\" -> \"%s\" [%s];%n", mn, dn, attr);
    });
}
 
Example 7
Source File: ConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes a module-info.class. If {@code targetPlatform} is not null then
 * it includes the ModuleTarget class file attribute with the target platform.
 */
static Path writeModule(ModuleDescriptor descriptor, String targetPlatform)
    throws IOException
{
    ModuleTarget target;
    if (targetPlatform != null) {
        target = new ModuleTarget(targetPlatform);
    } else {
        target = null;
    }
    String name = descriptor.name();
    Path dir = Files.createTempDirectory(name);
    Path mi = dir.resolve("module-info.class");
    try (OutputStream out = Files.newOutputStream(mi)) {
        ModuleInfoWriter.write(descriptor, target, out);
    }
    return dir;
}
 
Example 8
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 9
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 10
Source File: Loader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a {@code Loader} in a loader pool that loads classes/resources
 * from one module.
 */
public Loader(ResolvedModule resolvedModule,
              LoaderPool pool,
              ClassLoader parent)
{
    super("Loader-" + resolvedModule.name(), parent);

    this.pool = pool;
    this.parent = parent;

    ModuleReference mref = resolvedModule.reference();
    ModuleDescriptor descriptor = mref.descriptor();
    String mn = descriptor.name();
    this.nameToModule = Map.of(mn, mref);

    Map<String, LoadedModule> localPackageToModule = new HashMap<>();
    LoadedModule lm = new LoadedModule(mref);
    descriptor.packages().forEach(pn -> localPackageToModule.put(pn, lm));
    this.localPackageToModule = localPackageToModule;

    this.acc = AccessController.getContext();
}
 
Example 11
Source File: ModuleBootstrap.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks for split packages between modules defined to the built-in class
 * loaders.
 */
private static void checkSplitPackages(Configuration cf,
                                       Function<String, ClassLoader> clf) {
    Map<String, String> packageToModule = new HashMap<>();
    for (ResolvedModule resolvedModule : cf.modules()) {
        ModuleDescriptor descriptor = resolvedModule.reference().descriptor();
        String name = descriptor.name();
        ClassLoader loader = clf.apply(name);
        if (loader == null || loader instanceof BuiltinClassLoader) {
            for (String p : descriptor.packages()) {
                String other = packageToModule.putIfAbsent(p, name);
                if (other != null) {
                    String msg = "Package " + p + " in both module "
                                 + name + " and module " + other;
                    throw new LayerInstantiationException(msg);
                }
            }
        }

    }
}
 
Example 12
Source File: ModuleHelper.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static ModuleReference moduleReference(ModuleDescriptor descriptor, URI uri, ModuleReader moduleReader) {
  return new ModuleReference(descriptor, uri) {
    @Override
    public ModuleReader open() {
      return moduleReader;
    }

    @Override
    public String toString() {
      return descriptor.name() + " at " + uri;
    }
  };
}
 
Example 13
Source File: LayerAndLoadersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String readModuleName(URL url) {
    try (InputStream in = url.openStream()) {
        ModuleDescriptor descriptor = ModuleDescriptor.read(in);
        return descriptor.name();
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
 
Example 14
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a byte array containing the given module-info.class plus any
 * extended attributes.
 *
 * If --module-version, --main-class, or other options were provided
 * then the corresponding class file attributes are added to the
 * module-info here.
 */
private byte[] extendedInfoBytes(ModuleDescriptor md,
                                 byte[] miBytes,
                                 Set<String> packages)
    throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream is = new ByteArrayInputStream(miBytes);
    ModuleInfoExtender extender = ModuleInfoExtender.newExtender(is);

    // Add (or replace) the Packages attribute
    extender.packages(packages);

    // --main-class
    if (ename != null)
        extender.mainClass(ename);

    // --module-version
    if (moduleVersion != null)
        extender.version(moduleVersion);

    // --hash-modules
    if (modulesToHash != null) {
        String mn = md.name();
        Hasher hasher = new Hasher(md, fname);
        ModuleHashes moduleHashes = hasher.computeHashes(mn);
        if (moduleHashes != null) {
            extender.hashes(moduleHashes);
        } else {
            warn("warning: no module is recorded in hash in " + mn);
        }
    }

    if (moduleResolution.value() != 0) {
        extender.moduleResolution(moduleResolution);
    }

    extender.write(baos);
    return baos.toByteArray();
}
 
Example 15
Source File: SystemModuleFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private ModuleReference toModuleReference(ModuleDescriptor md,
                                          ModuleTarget target,
                                          ModuleHashes recordedHashes,
                                          HashSupplier hasher,
                                          ModuleResolution mres) {
    String mn = md.name();
    URI uri = JNUA.create("jrt", "/".concat(mn));

    Supplier<ModuleReader> readerSupplier = new Supplier<>() {
        @Override
        public ModuleReader get() {
            return new ImageModuleReader(mn, uri);
        }
    };

    ModuleReference mref = new ModuleReferenceImpl(md,
                                                   uri,
                                                   readerSupplier,
                                                   null,
                                                   target,
                                                   recordedHashes,
                                                   hasher,
                                                   mres);

    // may need a reference to a patched module if --patch-module specified
    mref = ModuleBootstrap.patcher().patchIfNeeded(mref);

    return mref;
}
 
Example 16
Source File: Module.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a named module but without defining the module to the VM.
 *
 * @apiNote This constructor is for VM white-box testing.
 */
Module(ClassLoader loader, ModuleDescriptor descriptor) {
    this.layer = null;
    this.name = descriptor.name();
    this.loader = loader;
    this.descriptor = descriptor;
}
 
Example 17
Source File: SystemModuleFinders.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ModuleReference to the system module.
 */
static ModuleReference toModuleReference(ModuleDescriptor descriptor,
                                         ModuleTarget target,
                                         ModuleHashes recordedHashes,
                                         HashSupplier hasher,
                                         ModuleResolution mres) {
    String mn = descriptor.name();
    URI uri = JNUA.create("jrt", "/".concat(mn));

    Supplier<ModuleReader> readerSupplier = new Supplier<>() {
        @Override
        public ModuleReader get() {
            return new SystemModuleReader(mn, uri);
        }
    };

    ModuleReference mref = new ModuleReferenceImpl(descriptor,
                                                   uri,
                                                   readerSupplier,
                                                   null,
                                                   target,
                                                   recordedHashes,
                                                   hasher,
                                                   mres);

    // may need a reference to a patched module if --patch-module specified
    mref = ModuleBootstrap.patcher().patchIfNeeded(mref);

    return mref;
}
 
Example 18
Source File: Module.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a named module but without defining the module to the VM.
 *
 * @apiNote This constructor is for VM white-box testing.
 */
Module(ClassLoader loader, ModuleDescriptor descriptor) {
    this.layer = null;
    this.name = descriptor.name();
    this.loader = loader;
    this.descriptor = descriptor;
}
 
Example 19
Source File: Module.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public Builder(ModuleDescriptor md, boolean isSystem) {
    this.name = md.name();
    this.descriptor = md;
    this.isSystem = isSystem;
}
 
Example 20
Source File: ConfigImpl.java    From update4j with Apache License 2.0 4 votes vote down vote up
private static void checkBootConflicts(FileMetadata file, Path download) throws IOException {
    String filename = file.getPath().getFileName().toString();
    
    if (!FileUtils.isZipFile(download)) {
        Warning.nonZip(filename);
        throw new IllegalStateException(
                        "File '" + filename + "' is not a valid zip file.");
    }

    Set<Module> modules = ModuleLayer.boot().modules();
    Set<String> moduleNames = modules.stream().map(Module::getName).collect(Collectors.toSet());

    Set<String> sysMods = ModuleFinder.ofSystem()
                    .findAll()
                    .stream()
                    .map(mr -> mr.descriptor().name())
                    .collect(Collectors.toSet());
    
    ModuleDescriptor newMod = null;
    try {
        newMod = FileUtils.deriveModuleDescriptor(download, filename, sysMods.contains("jdk.zipfs"));
    } catch (IllegalArgumentException | InvalidModuleDescriptorException | FindException e) {
        Warning.illegalModule(filename);
        throw e;
    }

    if (moduleNames.contains(newMod.name())) {
        Warning.moduleConflict(newMod.name());
        throw new IllegalStateException(
                        "Module '" + newMod.name() + "' conflicts with a module in the boot modulepath");
    }

    Set<String> packages = modules.stream().flatMap(m -> m.getPackages().stream()).collect(Collectors.toSet());
    for (String p : newMod.packages()) {
        if (packages.contains(p)) {
            Warning.packageConflict(p);
            throw new IllegalStateException("Package '" + p + "' in module '" + newMod.name()
                            + "' conflicts with a package in the boot modulepath");

        }
    }

    for (Requires require : newMod.requires()) {

        // static requires are not mandatory
        if (require.modifiers().contains(Requires.Modifier.STATIC))
            continue;

        String reqName = require.name();
        if (StringUtils.isSystemModule(reqName)) {
            if (!sysMods.contains(reqName)) {
                Warning.missingSysMod(reqName);
                throw new IllegalStateException("System module '" + reqName
                                + "' is missing from JVM image, required by '" + newMod.name() + "'");
            }

        }
    }
}