8177530: Module system implementation refresh (4/2017)

Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Reviewed-by: jjg
This commit is contained in:
Alan Bateman 2017-04-07 08:08:26 +00:00
parent f6ed80af92
commit 481f056ca9
28 changed files with 112 additions and 67 deletions
langtools
make/tools/crules
src
java.compiler/share/classes/javax/tools
jdk.compiler/share/classes/com/sun/tools/javac
jdk.javadoc/share/classes/com/sun/tools
doclets/internal/toolkit/taglets
javadoc/main
test

@ -23,8 +23,6 @@
package crules;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -76,7 +74,7 @@ public class CodingRulesAnalyzerPlugin implements Plugin {
private void addExports(String moduleName, String... packageNames) {
for (String packageName : packageNames) {
try {
Layer layer = Layer.boot();
ModuleLayer layer = ModuleLayer.boot();
Optional<Module> m = layer.findModule(moduleName);
if (!m.isPresent())
throw new Error("module not found: " + moduleName);

@ -106,7 +106,7 @@ public class ToolProvider {
static {
Class<?> c = null;
try {
c = Class.forName("java.lang.reflect.Module");
c = Class.forName("java.lang.Module");
} catch (Throwable t) {
}
useLegacy = (c == null);

@ -1240,7 +1240,7 @@ public class Modules extends JCTree.Visitor {
case ALL_SYSTEM:
modules = new HashSet<>(syms.getAllModules())
.stream()
.filter(systemModulePred.and(observablePred).and(noIncubatorPred));
.filter(systemModulePred.and(observablePred));
break;
case ALL_MODULE_PATH:
modules = new HashSet<>(syms.getAllModules())
@ -1265,6 +1265,15 @@ public class Modules extends JCTree.Visitor {
result.add(syms.unnamedModule);
boolean hasAutomatic = result.stream().anyMatch(IS_AUTOMATIC);
if (hasAutomatic) {
syms.getAllModules()
.stream()
.filter(IS_AUTOMATIC)
.forEach(result::add);
}
String incubatingModules = result.stream()
.filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
.map(msym -> msym.name.toString())
@ -1282,6 +1291,9 @@ public class Modules extends JCTree.Visitor {
rootModules.forEach(m -> m.version = version);
}
}
//where:
private static final Predicate<ModuleSymbol> IS_AUTOMATIC =
m -> (m.flags_field & Flags.AUTOMATIC_MODULE) != 0;
public boolean isInModuleGraph(ModuleSymbol msym) {
return allModules == null || allModules.contains(msym);

@ -1282,8 +1282,7 @@ public class Locations {
}
// finally clean up the module name
mn = mn.replaceAll("(\\.|\\d)*$", "") // remove trailing version
.replaceAll("[^A-Za-z0-9]", ".") // replace non-alphanumeric
mn = mn.replaceAll("[^A-Za-z0-9]", ".") // replace non-alphanumeric
.replaceAll("(\\.)(\\1)+", ".") // collapse repeating dots
.replaceAll("^\\.", "") // drop leading dots
.replaceAll("\\.$", ""); // drop trailing dots

@ -183,7 +183,7 @@ public class JDK9Wrappers {
}
/**
* Wrapper class for java.lang.reflect.Module. To materialize a handle use the static factory
* Wrapper class for java.lang.Module. To materialize a handle use the static factory
* methods Module#getModule(Class<?>) or Module#getUnnamedModule(ClassLoader).
*/
public static class Module {
@ -236,9 +236,9 @@ public class JDK9Wrappers {
}
// -----------------------------------------------------------------------------------------
// on java.lang.reflect.Module
// on java.lang.Module
private static Method addExportsMethod = null;
// on java.lang.reflect.Module
// on java.lang.Module
private static Method addUsesMethod = null;
// on java.lang.Class
private static Method getModuleMethod;
@ -248,7 +248,7 @@ public class JDK9Wrappers {
private static void init() {
if (addExportsMethod == null) {
try {
Class<?> moduleClass = Class.forName("java.lang.reflect.Module", false, null);
Class<?> moduleClass = Class.forName("java.lang.Module", false, null);
addUsesMethod = moduleClass.getDeclaredMethod("addUses", new Class<?>[] { Class.class });
addExportsMethod = moduleClass.getDeclaredMethod("addExports",
new Class<?>[] { String.class, moduleClass });
@ -318,7 +318,7 @@ public class JDK9Wrappers {
}
/**
* Wrapper class for java.lang.module.Layer.
* Wrapper class for java.lang.ModuleLayer.
*/
public static final class Layer {
private final Object theRealLayer;
@ -372,7 +372,7 @@ public class JDK9Wrappers {
private static void init() {
if (layerClass == null) {
try {
layerClass = Class.forName("java.lang.reflect.Layer", false, null);
layerClass = Class.forName("java.lang.ModuleLayer", false, null);
bootMethod = layerClass.getDeclaredMethod("boot");
defineModulesWithOneLoaderMethod = layerClass.getDeclaredMethod("defineModulesWithOneLoader",
Configuration.getConfigurationClass(),

@ -292,7 +292,7 @@ public class TagletManager {
Method getModuleMethod = Class.class.getDeclaredMethod("getModule");
Object thisModule = getModuleMethod.invoke(getClass());
Class<?> moduleClass = Class.forName("java.lang.reflect.Module");
Class<?> moduleClass = Class.forName("java.lang.Module");
Method addExportsMethod = moduleClass.getDeclaredMethod("addExports", String.class, moduleClass);
Method getUnnamedModuleMethod = ClassLoader.class.getDeclaredMethod("getUnnamedModule");

@ -379,7 +379,7 @@ public class DocletInvoker {
Method getModuleMethod = Class.class.getDeclaredMethod("getModule");
Object thisModule = getModuleMethod.invoke(getClass());
Class<?> moduleClass = Class.forName("java.lang.reflect.Module");
Class<?> moduleClass = Class.forName("java.lang.Module");
Method addExportsMethod = moduleClass.getDeclaredMethod("addExports", String.class, moduleClass);
Method getUnnamedModuleMethod = ClassLoader.class.getDeclaredMethod("getUnnamedModule");

@ -14,8 +14,8 @@ keys=intermittent randomness
# Group definitions
groups=TEST.groups
# Tests using jtreg 4.2 b05 features
requiredVersion=4.2 b05
# Tests using jtreg 4.2 b07 features
requiredVersion=4.2 b07
# Use new module options
useNewOptions=true

@ -23,8 +23,6 @@
package taglets;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import com.sun.javadoc.*;

@ -23,8 +23,6 @@
package testtaglets;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import com.sun.javadoc.*;

@ -23,8 +23,6 @@
package testtaglets;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import com.sun.javadoc.*;

@ -23,8 +23,6 @@
package testtaglets;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import com.sun.javadoc.*;

@ -23,8 +23,6 @@
package taglets;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import com.sun.javadoc.*;

@ -33,8 +33,6 @@
*/
import java.io.*;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import javax.tools.*;
import com.sun.tools.classfile.*;
@ -265,7 +263,7 @@ public class CheckResourceKeys {
* Get the set of keys from the javadoc resource bundles.
*/
Set<String> getResourceKeys() {
Module jdk_javadoc = Layer.boot().findModule("jdk.javadoc").get();
Module jdk_javadoc = ModuleLayer.boot().findModule("jdk.javadoc").get();
String[] names = {
"jdk.javadoc.internal.doclets.formats.html.resources.standard",
"jdk.javadoc.internal.doclets.toolkit.resources.doclets",

@ -30,7 +30,6 @@ import java.io.StringWriter;
import java.lang.reflect.Method;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.lang.reflect.Layer;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
@ -211,11 +210,11 @@ public class KullaTesting {
public ClassLoader createAndRunFromModule(String moduleName, Path modPath) {
ModuleFinder finder = ModuleFinder.of(modPath);
Layer parent = Layer.boot();
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration()
.resolve(finder, ModuleFinder.of(), Set.of(moduleName));
ClassLoader scl = ClassLoader.getSystemClassLoader();
Layer layer = parent.defineModulesWithOneLoader(cf, scl);
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
ClassLoader loader = layer.findLoader(moduleName);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(loader);

@ -31,7 +31,6 @@
*/
import java.lang.reflect.Field;
import java.lang.reflect.Module;
import java.io.File;
import java.io.ByteArrayOutputStream;
import javax.tools.*;

@ -11,9 +11,9 @@
// Editing the imports and other leading text may affect the golden text in the tests field.
// Also beware of scripts that auto-expand tabs to spaces.
import java.io.*;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;

@ -39,8 +39,6 @@
*/
import java.io.*;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
@ -111,7 +109,7 @@ public class CheckExamples {
}
}
Module jdk_compiler = Layer.boot().findModule("jdk.compiler").get();
Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get();
ResourceBundle b =
ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler", jdk_compiler);
Set<String> resourceKeys = new TreeSet<String>(b.keySet());

@ -31,8 +31,6 @@
*/
import java.io.*;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import javax.tools.*;
import com.sun.tools.classfile.*;
@ -395,7 +393,7 @@ public class CheckResourceKeys {
* Get the set of keys from the javac resource bundles.
*/
Set<String> getResourceKeys() {
Module jdk_compiler = Layer.boot().findModule("jdk.compiler").get();
Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get();
Set<String> results = new TreeSet<String>();
for (String name : new String[]{"javac", "compiler"}) {
ResourceBundle b =

@ -22,7 +22,7 @@
*/
// key: compiler.misc.fatal.err.no.java.lang
// options: -source 8 -target 8 -Xbootclasspath:
// options: -source 8 -target 8 -Xbootclasspath: -classpath .
// run: backdoor
class NoJavaLang { }

@ -74,7 +74,7 @@ public class NoJavaLangTest {
// test with bootclasspath, for as long as its around
void testBootClassPath() {
String[] bcpOpts = { "-Xlint:-options", "-source", "8", "-bootclasspath", "." };
String[] bcpOpts = { "-Xlint:-options", "-source", "8", "-bootclasspath", ".", "-classpath", "." };
test(bcpOpts, compilerErrorMessage);
}

@ -21,8 +21,6 @@
* questions.
*/
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@ -102,7 +100,7 @@ public abstract class JavacTestingAbstractProcessor extends AbstractProcessor {
protected void addExports(String moduleName, String... packageNames) {
for (String packageName : packageNames) {
try {
Layer layer = Layer.boot();
ModuleLayer layer = ModuleLayer.boot();
Optional<Module> m = layer.findModule(moduleName);
if (!m.isPresent())
throw new Error("module not found: " + moduleName);

@ -418,13 +418,13 @@ public class AddLimitMods extends ModuleTestBase {
" public static void main(String... args) throws Exception {\n");
for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
testClassNamed.append(" System.err.println(\"visible:" + e.getKey() + ":\" + java.lang.reflect.Layer.boot().findModule(\"" + e.getKey() + "\").isPresent());\n");
testClassNamed.append(" System.err.println(\"visible:" + e.getKey() + ":\" + ModuleLayer.boot().findModule(\"" + e.getKey() + "\").isPresent());\n");
}
testClassNamed.append(" Class<?> cp = Class.forName(Test.class.getClassLoader().getUnnamedModule(), \"cp.CP\");\n");
testClassNamed.append(" cp.getDeclaredMethod(\"runMe\").invoke(null);\n");
testClassNamed.append(" Class<?> automatic = Class.forName(java.lang.reflect.Layer.boot().findModule(\"automatic\").get(), \"automatic.Automatic\");\n");
testClassNamed.append(" Class<?> automatic = Class.forName(ModuleLayer.boot().findModule(\"automatic\").get(), \"automatic.Automatic\");\n");
testClassNamed.append(" automatic.getDeclaredMethod(\"runMe\").invoke(null);\n");
testClassNamed.append(" }\n" +

@ -300,9 +300,8 @@ public class AutomaticModules extends ModuleTestBase {
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("Impl.java:1:47: compiler.err.package.not.visible: apiB, (compiler.misc.not.def.access.does.not.read: m1x, apiB, automaticB)",
"Impl.java:1:59: compiler.err.package.not.visible: m2x, (compiler.misc.not.def.access.does.not.read: m1x, m2x, m2x)",
"2 errors");
expected = Arrays.asList("Impl.java:1:59: compiler.err.package.not.visible: m2x, (compiler.misc.not.def.access.does.not.read: m1x, m2x, m2x)",
"1 error");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
@ -310,7 +309,7 @@ public class AutomaticModules extends ModuleTestBase {
}
@Test
public void testDropTrailingVersion(Path base) throws Exception {
public void testWithTrailingVersion(Path base) throws Exception {
Path legacySrc = base.resolve("legacy-src");
tb.writeJavaFiles(legacySrc,
"package api; public class Api {}");
@ -348,7 +347,7 @@ public class AutomaticModules extends ModuleTestBase {
Files.createDirectories(classes);
tb.writeJavaFiles(m,
"module m { requires test; }",
"module m { requires test1; }",
"package impl; public class Impl { public void e(api.Api api) { } }");
new JavacTask(tb)
@ -358,4 +357,70 @@ public class AutomaticModules extends ModuleTestBase {
.run()
.writeAll();
}
@Test
public void testMultipleAutomatic(Path base) throws Exception {
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
for (char c : new char[] {'A', 'B'}) {
Path automaticSrc = base.resolve("automaticSrc" + c);
tb.writeJavaFiles(automaticSrc, "package api" + c + "; public class Api {}");
Path automaticClasses = base.resolve("automaticClasses" + c);
tb.createDirectories(automaticClasses);
String automaticLog = new JavacTask(tb)
.outdir(automaticClasses)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!automaticLog.isEmpty())
throw new Exception("expected output not found: " + automaticLog);
Path automaticJar = modulePath.resolve("automatic" + c + "-1.0.jar");
new JarTask(tb, automaticJar)
.baseDir(automaticClasses)
.files("api" + c + "/Api.class")
.run();
}
Path src = base.resolve("src");
tb.writeJavaFiles(src.resolve("m1x"),
"package impl; public class Impl { apiA.Api a; apiB.Api b; }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> log = new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("Impl.java:1:35: compiler.err.package.not.visible: apiA, (compiler.misc.not.def.access.does.not.read.from.unnamed: apiA, automaticA)",
"Impl.java:1:47: compiler.err.package.not.visible: apiB, (compiler.misc.not.def.access.does.not.read.from.unnamed: apiB, automaticB)",
"2 errors");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--add-modules", "automaticA",
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
}
}

@ -122,14 +122,11 @@ public class IncubatingTest extends ModuleTestBase {
"-XDrawDiagnostics")
.outdir(testClasses)
.files(findJavaFiles(testSrc))
.run(Expect.FAIL)
.run(Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList(
"T.java:1:11: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.from.unnamed: api, jdk.i)",
"1 error"
);
expected = Arrays.asList("");
if (!expected.equals(log)) {
throw new AssertionError("Unexpected output: " + log);

@ -21,8 +21,6 @@
* questions.
*/
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;

@ -30,8 +30,6 @@
* jdk.compiler/com.sun.tools.javac.util
*/
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@ -42,7 +40,7 @@ import com.sun.tools.javac.util.Log.PrefixKind;
public class VerifyLintDescriptions {
public static void main(String... args) {
Layer boot = Layer.boot();
ModuleLayer boot = ModuleLayer.boot();
Module jdk_compiler = boot.findModule("jdk.compiler").get();
ResourceBundle b = ResourceBundle.getBundle("com.sun.tools.javac.resources.javac",
Locale.US,

@ -32,8 +32,6 @@
*/
import java.io.*;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
import javax.tools.*;
import com.sun.tools.classfile.*;
@ -229,7 +227,7 @@ public class CheckResourceKeys {
* Get the set of keys from the javadoc resource bundles.
*/
Set<String> getResourceKeys() {
Module jdk_javadoc = Layer.boot().findModule("jdk.javadoc").get();
Module jdk_javadoc = ModuleLayer.boot().findModule("jdk.javadoc").get();
String[] names = {
"com.sun.tools.doclets.formats.html.resources.standard",
"com.sun.tools.doclets.internal.toolkit.resources.doclets",