# HG changeset patch # Parent 5aa6f825b4ecddcf1fa773e9722a20f97d0f590a diff -r 5aa6f825b4ec make/data/symbols/system-modules-9 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/make/data/symbols/system-modules-9 Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,69 @@ +# +# Copyright (c) 2015, 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 +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the LICENSE file that accompanied this code. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# +java.activation +java.base +java.compiler +java.corba +java.datatransfer +java.desktop +java.instrument +java.logging +java.management +java.management.rmi +java.naming +java.prefs +java.rmi +java.scripting +java.se +java.security.jgss +java.security.sasl +java.se.ee +java.smartcardio +java.sql +java.sql.rowset +java.transaction +java.xml +java.xml.bind +java.xml.crypto +java.xml.ws +java.xml.ws.annotation +jdk.accessibility +jdk.attach +jdk.compiler +jdk.dynalink +jdk.httpserver +jdk.jartool +jdk.javadoc +jdk.jconsole +jdk.jdi +jdk.jshell +jdk.jsobject +jdk.management +jdk.net +jdk.scripting.nashorn +jdk.sctp +jdk.security.auth +jdk.security.jgss +jdk.xml.dom diff -r 5aa6f825b4ec make/gendata/Gendata-jdk.compiler.gmk --- a/make/gendata/Gendata-jdk.compiler.gmk Mon Apr 17 17:03:19 2017 -0700 +++ b/make/gendata/Gendata-jdk.compiler.gmk Fri Apr 21 12:12:43 2017 +0200 @@ -25,11 +25,13 @@ include JarArchive.gmk include JavaCompilation.gmk +include Modules.gmk include SetupJavaCompilers.gmk ################################################################################ -CT_DATA_DESCRIPTION ?= $(LANGTOOLS_TOPDIR)/make/data/symbols/symbols +CT_DATA_DIR ?= $(LANGTOOLS_TOPDIR)/make/data/symbols +CT_DATA_DESCRIPTION ?= $(CT_DATA_DIR)/symbols $(eval $(call SetupJavaCompilation, COMPILE_CREATE_SYMBOLS, \ SETUP := GENERATE_OLDBYTECODE, \ @@ -59,6 +61,9 @@ build-ctsym \ $(CT_DATA_DESCRIPTION) \ $(@D) + $(MKDIR) $(@D)/9 + $(CAT) $(CT_DATA_DIR)/system-modules-9 | $(GREP) -v '^#' >$(@D)/9/system-modules + $(ECHO) -n $(call FindImportedModules) | $(TR) ' ' '\n' >>$(@D)/9/system-modules $(TOUCH) $@ # Can't generate ct.sym directly into modules libs as the SetupJarArchive macro @@ -66,7 +71,7 @@ $(eval $(call SetupJarArchive, CREATE_CTSYM, \ DEPENDENCIES := $(SUPPORT_OUTPUTDIR)/symbols/ct.sym-files/_the.symbols, \ SRCS := $(SUPPORT_OUTPUTDIR)/symbols/ct.sym-files, \ - SUFFIXES := .sig, \ + SUFFIXES := .sig system-modules, \ JAR := $(SUPPORT_OUTPUTDIR)/symbols/ct.sym, \ )) diff -r 5aa6f825b4ec src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java Mon Apr 17 17:03:19 2017 -0700 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java Fri Apr 21 12:12:43 2017 +0200 @@ -142,6 +142,7 @@ private final ModuleFinder moduleFinder; private final Source source; private final boolean allowModules; + private final boolean allowAccessIntoSystem; public final boolean multiModuleMode; @@ -192,6 +193,7 @@ allowModules = source.allowModules(); Options options = Options.instance(context); + allowAccessIntoSystem = options.isUnset(Option.RELEASE); lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); legacyModuleOverride = options.get(Option.XMODULE); @@ -1182,7 +1184,7 @@ for (String limit : extraLimitMods) { limitMods.add(syms.enterModule(names.fromString(limit))); } - observable = computeTransitiveClosure(limitMods, null); + observable = computeTransitiveClosure(limitMods, rootModules, null); observable.addAll(rootModules); if (lintOptions) { for (ModuleSymbol msym : limitMods) { @@ -1262,7 +1264,7 @@ } } - Set result = computeTransitiveClosure(enabledRoot, observable); + Set result = computeTransitiveClosure(enabledRoot, rootModules, observable); result.add(syms.unnamedModule); @@ -1300,12 +1302,18 @@ return allModules == null || allModules.contains(msym); } - private Set computeTransitiveClosure(Set base, Set observable) { + private Set computeTransitiveClosure(Set base, + Set rootModules, + Set observable) { List primaryTodo = List.nil(); List secondaryTodo = List.nil(); for (ModuleSymbol ms : base) { - primaryTodo = primaryTodo.prepend(ms); + if (rootModules.contains(ms)) { + primaryTodo = primaryTodo.prepend(ms); + } else { + secondaryTodo = secondaryTodo.prepend(ms); + } } Set result = new LinkedHashSet<>(); @@ -1328,12 +1336,12 @@ if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0)) continue; current.complete(); - if (current.kind == ERR && isPrimaryTodo && warnedMissing.add(current)) { + if (current.kind == ERR && (isPrimaryTodo || base.contains(current)) && warnedMissing.add(current)) { log.error(Errors.ModuleNotFound(current)); } for (RequiresDirective rd : current.requires) { if (rd.module == syms.java_base) continue; - if ((rd.isTransitive() && isPrimaryTodo) || base.contains(current)) { + if ((rd.isTransitive() && isPrimaryTodo) || rootModules.contains(current)) { primaryTodo = primaryTodo.prepend(rd.module); } else { secondaryTodo = secondaryTodo.prepend(rd.module); @@ -1440,6 +1448,10 @@ } } + if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0 && + msym.patchLocation != null) { + log.error(Errors.PatchModuleWithRelease(msym)); + } } private Set retrieveRequiresTransitive(ModuleSymbol msym) { @@ -1565,6 +1577,12 @@ if (!isValidName(packageName)) continue; + + if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0) { + log.error(Errors.AddExportsWithRelease(msym)); + continue; + } + PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName)); p.modle = msym; // TODO: do we need this? @@ -1640,6 +1658,11 @@ continue; } + if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0) { + log.error(Errors.AddReadsWithRelease(msym)); + continue; + } + for (String targetName : targetNames.split("[ ,]+", -1)) { ModuleSymbol targetModule; if (targetName.equals("ALL-UNNAMED")) { diff -r 5aa6f825b4ec src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java Mon Apr 17 17:03:19 2017 -0700 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java Fri Apr 21 12:12:43 2017 +0200 @@ -300,7 +300,8 @@ Option.XBOOTCLASSPATH_PREPEND, Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS, Option.EXTDIRS, Option.DJAVA_EXT_DIRS, - Option.SOURCE, Option.TARGET); + Option.SOURCE, Option.TARGET, + Option.SYSTEM, Option.UPGRADE_MODULE_PATH); if (platformString != null) { PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString); @@ -331,7 +332,12 @@ try { StandardJavaFileManager sfm = (StandardJavaFileManager) fm; - sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP); + if (Source.instance(context).allowModules()) { + sfm.handleOption("--system", Arrays.asList("none").iterator()); + sfm.setLocationFromPaths(StandardLocation.UPGRADE_MODULE_PATH, platformCP); + } else { + sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP); + } } catch (IOException ex) { log.printLines(PrefixKind.JAVAC, "msg.io"); ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); diff -r 5aa6f825b4ec src/jdk.compiler/share/classes/com/sun/tools/javac/platform/JDKPlatformProvider.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/platform/JDKPlatformProvider.java Mon Apr 17 17:03:19 2017 -0700 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/platform/JDKPlatformProvider.java Fri Apr 21 12:12:43 2017 +0200 @@ -26,6 +26,8 @@ package com.sun.tools.javac.platform; import java.io.IOException; +import java.net.URI; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.FileSystem; import java.nio.file.FileSystems; @@ -41,6 +43,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Stream; import javax.annotation.processing.Processor; @@ -90,7 +93,6 @@ } catch (IOException | ProviderNotFoundException ex) { } } - SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.DEFAULT)); } private static String targetNumericVersion(Target target) { @@ -108,10 +110,6 @@ @Override public Collection getPlatformPath() { - if (Target.lookup(version) == Target.DEFAULT) { - return null; - } - List paths = new ArrayList<>(); Path file = findCtSym(); // file == ${jdk.home}/lib/ct.sym @@ -128,7 +126,21 @@ try (DirectoryStream dir = Files.newDirectoryStream(root)) { for (Path section : dir) { if (section.getFileName().toString().contains(version)) { - paths.add(section); + Path systemModules = section.resolve("system-modules"); + + if (Files.isRegularFile(systemModules)) { + Path modules = + FileSystems.getFileSystem(URI.create("jrt:/")) + .getPath("modules"); + try (Stream lines = + Files.lines(systemModules, Charset.forName("UTF-8"))) { + lines.map(line -> modules.resolve(line)) + .filter(mod -> Files.exists(mod)) + .forEach(mod -> paths.add(mod)); + } + } else { + paths.add(section); + } } } } catch (IOException ex) { diff -r 5aa6f825b4ec src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Apr 17 17:03:19 2017 -0700 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Apr 21 12:12:43 2017 +0200 @@ -3024,6 +3024,18 @@ compiler.err.addmods.all.module.path.invalid=\ --add-modules ALL-MODULE-PATH can only be used when compiling the unnamed module +# 0: symbol +compiler.err.add.exports.with.release=\ + exporting a package from system module {0} is not allowed with --release + +# 0: symbol +compiler.err.add.reads.with.release=\ + adding read edges for system module {0} is not allowed with --release + +# 0: symbol +compiler.err.patch.module.with.release=\ + patching system module {0} is not allowed in combination with --release + compiler.warn.addopens.ignored=\ --add-opens has no effect at compile time diff -r 5aa6f825b4ec test/tools/javac/diags/examples/AddExportsWithRelease.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/AddExportsWithRelease.java Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// key: compiler.err.add.exports.with.release +// options: --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --release 9 + +class AddExportsWithRelease { +} diff -r 5aa6f825b4ec test/tools/javac/diags/examples/AddReadsWithRelease.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/AddReadsWithRelease.java Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// key: compiler.err.add.reads.with.release +// options: --add-reads java.base=java.compiler --release 9 + +class AddReadsWithRelease { +} diff -r 5aa6f825b4ec test/tools/javac/diags/examples/PatchModuleWithRelease/PatchModuleWithRelease.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/PatchModuleWithRelease/PatchModuleWithRelease.java Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// key: compiler.err.patch.module.with.release +// options: --release 9 + +class PatchModuleWithRelease { +} diff -r 5aa6f825b4ec test/tools/javac/diags/examples/PatchModuleWithRelease/patchmodule/java.base/java/lang/Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/PatchModuleWithRelease/patchmodule/java.base/java/lang/Test.java Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.lang; + +class Test { +} diff -r 5aa6f825b4ec test/tools/javac/options/release/ReleaseOptionClashes.java --- a/test/tools/javac/options/release/ReleaseOptionClashes.java Mon Apr 17 17:03:19 2017 -0700 +++ b/test/tools/javac/options/release/ReleaseOptionClashes.java Fri Apr 21 12:12:43 2017 +0200 @@ -44,17 +44,19 @@ } void run() throws Exception { - doRunTest("-bootclasspath", "any"); - doRunTest("-Xbootclasspath:any"); - doRunTest("-Xbootclasspath/a:any"); - doRunTest("-Xbootclasspath/p:any"); - doRunTest("-endorseddirs", "any"); - doRunTest("-extdirs", "any"); - doRunTest("-source", "8"); - doRunTest("-target", "8"); + doRunTest("7", "-bootclasspath", "any"); + doRunTest("7", "-Xbootclasspath:any"); + doRunTest("7", "-Xbootclasspath/a:any"); + doRunTest("7", "-Xbootclasspath/p:any"); + doRunTest("7", "-endorseddirs", "any"); + doRunTest("7", "-extdirs", "any"); + doRunTest("7", "-source", "8"); + doRunTest("7", "-target", "8"); + doRunTest("9", "--system", "none"); + doRunTest("9", "--upgrade-module-path", "any"); } - void doRunTest(String... args) throws Exception { + void doRunTest(String release, String... args) throws Exception { System.out.println("Testing clashes for arguments: " + Arrays.asList(args)); Class log = Class.forName("com.sun.tools.javac.util.Log", true, cl); Field useRawMessages = log.getDeclaredField("useRawMessages"); @@ -62,7 +64,7 @@ useRawMessages.setBoolean(null, true); ByteArrayOutputStream out = new ByteArrayOutputStream(); List options = new ArrayList<>(); - options.addAll(Arrays.asList("--release", "7")); + options.addAll(Arrays.asList("--release", release)); options.addAll(Arrays.asList(args)); options.add(System.getProperty("test.src") + File.separator + "ReleaseOptionClashes.java"); compiler.run(null, null, out, options.toArray(new String[0])); diff -r 5aa6f825b4ec test/tools/javac/options/release/ReleaseOptionUnsupported.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/options/release/ReleaseOptionUnsupported.java Fri Apr 21 12:12:43 2017 +0200 @@ -0,0 +1,309 @@ +/* + * Copyright (c) 2017, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8178152 + * @summary Verify unsupported modules and module options handling. + * @library /tools/lib + * @modules jdk.compiler/com.sun.tools.javac.api + * jdk.compiler/com.sun.tools.javac.main + * jdk.compiler/com.sun.tools.javac.jvm + * jdk.jdeps/com.sun.tools.classfile + * jdk.jdeps/com.sun.tools.javap + * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.JavapTask toolbox.TestRunner + * @run main ReleaseOptionUnsupported + */ + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import com.sun.tools.javac.jvm.Target; +import toolbox.JavacTask; +import toolbox.Task; +import toolbox.Task.Expect; +import toolbox.TestRunner; +import toolbox.ToolBox; + +public class ReleaseOptionUnsupported extends TestRunner { + + private final ToolBox tb = new ToolBox(); + + public ReleaseOptionUnsupported() { + super(System.err); + } + + public static void main(String... args) throws Exception { + new ReleaseOptionUnsupported().runTests(); + } + + @Test + public void testUnsafe(Path base) throws IOException { + Path src = base.resolve("src"); + tb.writeJavaFiles(src, + "module m { requires jdk.unsupported; }", + "package test; public class Test { sun.misc.Unsafe unsafe; } "); + Path classes = base.resolve("classes"); + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics") + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List log; + List expected; + + log = new JavacTask(tb) + .options("-XDrawDiagnostics", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "module-info.java:1:24: compiler.err.module.not.found: jdk.unsupported", + "1 error" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } + } + + @Test + public void testUnsafeUnnamed(Path base) throws IOException { + Path src = base.resolve("src"); + tb.writeJavaFiles(src, + "package test; public class Test { sun.misc.Unsafe unsafe; } "); + Path classes = base.resolve("classes"); + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics") + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List log; + List expected; + + log = new JavacTask(tb) + .options("-XDrawDiagnostics", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "Test.java:1:43: compiler.err.doesnt.exist: sun.misc", + "1 error" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } + } + + @Test + public void testAddExports(Path base) throws IOException { + Path src = base.resolve("src"); + tb.writeJavaFiles(src, + "module m { }", + "package test; public class Test { jdk.internal.misc.Unsafe unsafe; } "); + Path classes = base.resolve("classes"); + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-exports", "java.base/jdk.internal.misc=m") + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List log; + List expected; + + log = new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-exports", "java.base/jdk.internal.misc=m", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "- compiler.err.add.exports.with.release: java.base", + "1 error" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } + + //OK to add exports a package of a non-system module: + tb.writeJavaFiles(src, + "package test; public class Test { } "); + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-exports", "m/test=ALL-UNNAMED", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + } + + @Test + public void testAddReads(Path base) throws IOException { + Path src = base.resolve("src"); + tb.writeJavaFiles(src, + "module m { }", + "package test; public class Test { } "); + Path classes = base.resolve("classes"); + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-reads", "java.base=m") + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List log; + List expected; + + log = new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-reads", "java.base=m", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "- compiler.err.add.reads.with.release: java.base", + "1 error" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } + + //OK to add reads a package of a non-system module: + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--add-reads", "m=java.base", + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + } + + @Test + public void testPatchModule(Path base) throws IOException { + Path src = base.resolve("src"); + tb.writeJavaFiles(src, + "module m { }", + "package test; public class Test { } "); + Path classes = base.resolve("classes"); + tb.createDirectories(classes); + Path patch = base.resolve("patch"); + tb.createDirectories(patch); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--patch-module", "java.base=" + patch) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List log; + List expected; + + log = new JavacTask(tb) + .options("-XDrawDiagnostics", + "--patch-module", "java.base=" + patch, + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "- compiler.err.patch.module.with.release: java.base", + "1 error" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } + + //OK to patch a non-system module: + tb.createDirectories(classes); + + new JavacTask(tb) + .options("-XDrawDiagnostics", + "--patch-module", "m=" + patch, + "--release", Target.DEFAULT.multiReleaseValue()) + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + } + + protected void runTests() throws Exception { + runTests(m -> new Object[] { Paths.get(m.getName()) }); + } +}