Skip to content
Permalink
Browse files

8233922: Service binding augments module graph with observable incuba…

…tor modules

Reviewed-by: mchung
  • Loading branch information
Alan Bateman
Alan Bateman committed Nov 19, 2019
1 parent ec51784 commit 745feb207ce60cc02ae79aa4e96fe9f93f0185aa
@@ -312,7 +312,7 @@ static Configuration resolveAndBind(ModuleFinder finder,
{
List<Configuration> parents = List.of(empty());
Resolver resolver = new Resolver(finder, parents, ModuleFinder.of(), traceOutput);
resolver.resolve(roots).bind();
resolver.resolve(roots).bind(/*bindIncubatorModules*/false);
return new Configuration(parents, resolver);
}

@@ -28,7 +28,6 @@
import java.io.PrintStream;
import java.lang.module.ModuleDescriptor.Provides;
import java.lang.module.ModuleDescriptor.Requires.Modifier;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
@@ -45,6 +44,7 @@

import jdk.internal.module.ModuleHashes;
import jdk.internal.module.ModuleReferenceImpl;
import jdk.internal.module.ModuleResolution;
import jdk.internal.module.ModuleTarget;

/**
@@ -215,15 +215,32 @@ Resolver resolve(Collection<String> roots) {
* service-use relation.
*/
Resolver bind() {
return bind(/*bindIncubatorModules*/true);
}

/**
* Augments the set of resolved modules with modules induced by the
* service-use relation.
*
* @param bindIncubatorModules true if incubator modules are candidates to
* add to the module graph
*/
Resolver bind(boolean bindIncubatorModules) {
// Scan the finders for all available service provider modules. As
// java.base uses services then the module finders will be scanned
// anyway.
Map<String, Set<ModuleReference>> availableProviders = new HashMap<>();
for (ModuleReference mref : findAll()) {
ModuleDescriptor descriptor = mref.descriptor();
if (!descriptor.provides().isEmpty()) {

boolean candidate;
if (!bindIncubatorModules && (mref instanceof ModuleReferenceImpl)) {
ModuleResolution mres = ((ModuleReferenceImpl) mref).moduleResolution();
candidate = (mres == null) || (mres.hasIncubatingWarning() == false);
} else {
candidate = true;
}
if (candidate && !descriptor.provides().isEmpty()) {
for (Provides provides : descriptor.provides()) {
String sn = provides.service();

@@ -352,7 +352,7 @@ public static ModuleLayer boot() throws Exception {

Configuration cf;
if (needResolution) {
cf = JLMA.resolveAndBind(finder, roots, traceOutput);
cf = Modules.newBootLayerConfiguration(finder, roots, traceOutput);
} else {
if (archivedModuleGraph != null) {
cf = archivedModuleGraph.configuration();
@@ -34,6 +34,7 @@
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.ModuleVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
import static jdk.internal.org.objectweb.asm.Opcodes.*;

@@ -78,7 +79,9 @@ private ModuleInfoWriter() { }
* Writes the given module descriptor to a module-info.class file,
* returning it in a byte array.
*/
private static byte[] toModuleInfo(ModuleDescriptor md, ModuleTarget target) {
private static byte[] toModuleInfo(ModuleDescriptor md,
ModuleResolution mres,
ModuleTarget target) {
ClassWriter cw = new ClassWriter(0);
cw.visit(Opcodes.V10, ACC_MODULE, "module-info", null, null, null);

@@ -147,6 +150,11 @@ private ModuleInfoWriter() { }

mv.visitEnd();

// write ModuleResolution attribute if specified
if (mres != null) {
cw.visitAttribute(new ModuleResolutionAttribute(mres.value()));
}

// write ModuleTarget attribute if there is a target platform
if (target != null && target.targetPlatform().length() > 0) {
cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
@@ -161,30 +169,55 @@ private ModuleInfoWriter() { }
* module-info.class.
*/
public static void write(ModuleDescriptor descriptor,
ModuleResolution mres,
ModuleTarget target,
OutputStream out)
throws IOException
{
byte[] bytes = toModuleInfo(descriptor, target);
byte[] bytes = toModuleInfo(descriptor, mres, target);
out.write(bytes);
}

/**
* Writes a module descriptor to the given output stream as a
* module-info.class.
*/
public static void write(ModuleDescriptor descriptor,
ModuleResolution mres,
OutputStream out)
throws IOException
{
write(descriptor, mres, null, out);
}

/**
* Writes a module descriptor to the given output stream as a
* module-info.class.
*/
public static void write(ModuleDescriptor descriptor,
ModuleTarget target,
OutputStream out)
throws IOException
{
write(descriptor, null, target, out);
}

/**
* Writes a module descriptor to the given output stream as a
* module-info.class.
*/
public static void write(ModuleDescriptor descriptor, OutputStream out)
throws IOException
{
write(descriptor, null, out);
write(descriptor, null, null, out);
}

/**
* Returns a {@code ByteBuffer} containing the given module descriptor
* in module-info.class format.
*/
public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
byte[] bytes = toModuleInfo(descriptor, null);
byte[] bytes = toModuleInfo(descriptor, null, null);
return ByteBuffer.wrap(bytes);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@

package jdk.internal.module;

import java.io.PrintStream;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
@@ -33,13 +34,15 @@
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import jdk.internal.access.JavaLangModuleAccess;
import jdk.internal.loader.BootLoader;
import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.loader.ClassLoaders;
@@ -61,6 +64,7 @@
private Modules() { }

private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
private static final JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess();

/**
* Creates a new Module. The module has the given ModuleDescriptor and
@@ -158,6 +162,20 @@ public static void addProvides(Module m, Class<?> service, Class<?> impl) {
}
}

/**
* Resolves a collection of root modules, with service binding and the empty
* Configuration as the parent to create a Configuration for the boot layer.
*
* This method is intended to be used to create the Configuration for the
* boot layer during startup or at a link-time.
*/
public static Configuration newBootLayerConfiguration(ModuleFinder finder,
Collection<String> roots,
PrintStream traceOutput)
{
return JLMA.resolveAndBind(finder, roots, traceOutput);
}

/**
* Called by the VM when code in the given Module has been transformed by
* an agent and so may have been instrumented to call into supporting
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -61,6 +61,7 @@
import jdk.internal.module.Checks;
import jdk.internal.module.DefaultRoots;
import jdk.internal.module.IllegalAccessMaps;
import jdk.internal.module.Modules;
import jdk.internal.module.ModuleHashes;
import jdk.internal.module.ModuleInfo.Attributes;
import jdk.internal.module.ModuleInfoExtender;
@@ -291,10 +292,10 @@ public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {

/**
* Resolves a collection of root modules, with service binding, to create
* configuration.
* a Configuration for the boot layer.
*/
private Configuration resolve(ModuleFinder finder, Set<String> roots) {
return Configuration.empty().resolveAndBind(finder, ModuleFinder.of(), roots);
return Modules.newBootLayerConfiguration(finder, roots, null);
}

/**
@@ -59,8 +59,8 @@ public static void main(String[] args) throws Exception {
String testSrc = System.getProperty("test.src");

// $JDK_HOME/bin/java TestModules.java
String source = Path.of(testSrc, "src", "TestRootModules.java").toString();
ProcessTools.executeTestJava(source)
String source = Path.of(testSrc, "TestRootModules.java").toString();
ProcessTools.executeTestJava("--add-exports", "java.base/jdk.internal.module=ALL-UNNAMED", source)
.outputTo(System.out)
.errorTo(System.err)
.shouldHaveExitValue(0);
@@ -89,15 +89,18 @@ public static void main(String[] args) throws Exception {
javaLauncher += ".exe";

// $CUSTOM_JDK/bin/java TestRootModules.java
source = Path.of(testSrc, "src", "TestRootModules.java").toString();
out.format("Command line: [%s %s]%n", javaLauncher, source);
ProcessTools.executeProcess(new ProcessBuilder(javaLauncher, source))
source = Path.of(testSrc, "TestRootModules.java").toString();
ProcessBuilder pb = new ProcessBuilder(javaLauncher,
"--add-exports", "java.base/jdk.internal.module=ALL-UNNAMED",
source);
out.format("Command line: [%s]%n", pb.command());
ProcessTools.executeProcess(pb)
.outputTo(System.out)
.errorTo(System.err)
.shouldHaveExitValue(0);

// $CUSTOM_JDK/bin/java TestJson.java
source = Path.of(testSrc, "src", "TestJson.java").toString();
source = Path.of(testSrc, "TestJson.java").toString();
out.format("Command line: [%s %s]%n", javaLauncher, source);
ProcessTools.executeProcess(new ProcessBuilder(javaLauncher, source))
.outputTo(System.out)
File renamed without changes.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -21,11 +21,12 @@
* questions.
*/


import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;

import jdk.internal.module.ModuleResolution;

/**
* Test the set of modules in the boot layer includes all modules that export
* an API. Also test that java.se is not resolved.
@@ -37,6 +38,7 @@ public static void main(String[] args) {
// For now, this test ignores the ModuleResolution attribute
ModuleLayer bootLayer = ModuleLayer.boot();
ModuleFinder.ofSystem().findAll().stream()
.filter(mref -> !ModuleResolution.doNotResolveByDefault(mref))
.map(ModuleReference::descriptor)
.filter(descriptor -> descriptor.exports()
.stream()

0 comments on commit 745feb2

Please sign in to comment.
You can’t perform that action at this time.