mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-16 10:53:31 +00:00
8171177: Compiler should issue a warning for incubating modules that are resolved
Javac needs to follow ModuleResolution.DO_NOT_RESOLVE_BY_DEFAULT and ModuleResolution.WARN_INCUBATING Reviewed-by: jjg
This commit is contained in:
parent
ec3981561d
commit
b6e7bcbc6a
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 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
|
||||
@ -1045,7 +1045,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
|
||||
DO_NOT_RESOLVE_BY_DEFAULT(0x0001),
|
||||
WARN_DEPRECATED(0x0002),
|
||||
WARN_DEPRECATED_REMOVAL(0x0004),
|
||||
WARN_INCUBATOR(0x0008);
|
||||
WARN_INCUBATING(0x0008);
|
||||
|
||||
public static int value(Set<ModuleResolutionFlags> s) {
|
||||
int v = 0;
|
||||
|
||||
@ -41,6 +41,7 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
@ -114,6 +115,7 @@ import static com.sun.tools.javac.code.Flags.UNATTRIBUTED;
|
||||
import static com.sun.tools.javac.code.Kinds.Kind.ERR;
|
||||
import static com.sun.tools.javac.code.Kinds.Kind.MDL;
|
||||
import static com.sun.tools.javac.code.Kinds.Kind.MTH;
|
||||
import com.sun.tools.javac.code.Symbol.ModuleResolutionFlags;
|
||||
import static com.sun.tools.javac.code.TypeTag.CLASS;
|
||||
|
||||
/**
|
||||
@ -1090,6 +1092,10 @@ public class Modules extends JCTree.Visitor {
|
||||
Predicate<ModuleSymbol> observablePred = sym ->
|
||||
(observable == null) ? (moduleFinder.findModule(sym).kind != ERR) : observable.contains(sym);
|
||||
Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0;
|
||||
Predicate<ModuleSymbol> noIncubatorPred = sym -> {
|
||||
sym.complete();
|
||||
return !sym.resolutionFlags.contains(ModuleResolutionFlags.DO_NOT_RESOLVE_BY_DEFAULT);
|
||||
};
|
||||
Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>();
|
||||
|
||||
if (rootModules.contains(syms.unnamedModule)) {
|
||||
@ -1108,7 +1114,7 @@ public class Modules extends JCTree.Visitor {
|
||||
}
|
||||
|
||||
for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) {
|
||||
if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym)) {
|
||||
if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym) && noIncubatorPred.test(sym)) {
|
||||
enabledRoot.add(sym);
|
||||
}
|
||||
}
|
||||
@ -1128,14 +1134,14 @@ public class Modules extends JCTree.Visitor {
|
||||
Stream<ModuleSymbol> modules;
|
||||
switch (added) {
|
||||
case ALL_SYSTEM:
|
||||
modules = syms.getAllModules()
|
||||
.stream()
|
||||
.filter(systemModulePred.and(observablePred));
|
||||
modules = new HashSet<>(syms.getAllModules())
|
||||
.stream()
|
||||
.filter(systemModulePred.and(observablePred).and(noIncubatorPred));
|
||||
break;
|
||||
case ALL_MODULE_PATH:
|
||||
modules = syms.getAllModules()
|
||||
.stream()
|
||||
.filter(systemModulePred.negate().and(observablePred));
|
||||
modules = new HashSet<>(syms.getAllModules())
|
||||
.stream()
|
||||
.filter(systemModulePred.negate().and(observablePred));
|
||||
break;
|
||||
default:
|
||||
if (!isValidName(added))
|
||||
@ -1155,6 +1161,15 @@ public class Modules extends JCTree.Visitor {
|
||||
|
||||
result.add(syms.unnamedModule);
|
||||
|
||||
String incubatingModules = result.stream()
|
||||
.filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
|
||||
.map(msym -> msym.name.toString())
|
||||
.collect(Collectors.joining(","));
|
||||
|
||||
if (!incubatingModules.isEmpty()) {
|
||||
log.warning(Warnings.IncubatingModules(incubatingModules));
|
||||
}
|
||||
|
||||
allModules = result;
|
||||
|
||||
//add module versions from options, if any:
|
||||
|
||||
@ -1535,6 +1535,10 @@ compiler.warn.finally.cannot.complete=\
|
||||
compiler.warn.poor.choice.for.module.name=\
|
||||
module name {0} should avoid terminal digits
|
||||
|
||||
# 0: string
|
||||
compiler.warn.incubating.modules=\
|
||||
using incubating module(s): {0}
|
||||
|
||||
# 0: symbol, 1: symbol
|
||||
compiler.warn.has.been.deprecated=\
|
||||
{0} in {1} has been deprecated
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, 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
|
||||
@ -50,7 +50,7 @@ public class ModuleResolution_attribute extends Attribute {
|
||||
public ModuleResolution_attribute(ConstantPool constant_pool,
|
||||
int resolution_flags)
|
||||
throws ConstantPoolException {
|
||||
this(constant_pool.getUTF8Index(Attribute.ModulePackages),
|
||||
this(constant_pool.getUTF8Index(Attribute.ModuleResolution),
|
||||
resolution_flags);
|
||||
}
|
||||
|
||||
|
||||
@ -103,6 +103,7 @@ compiler.warn.annotation.method.not.found.reason # ClassReader
|
||||
compiler.warn.big.major.version # ClassReader
|
||||
compiler.warn.future.attr # ClassReader
|
||||
compiler.warn.illegal.char.for.encoding
|
||||
compiler.warn.incubating.modules # requires adjusted classfile
|
||||
compiler.warn.invalid.archive.file
|
||||
compiler.warn.override.bridge
|
||||
compiler.warn.position.overflow # CRTable: caused by files with long lines >= 1024 chars
|
||||
|
||||
291
langtools/test/tools/javac/modules/IncubatingTest.java
Normal file
291
langtools/test/tools/javac/modules/IncubatingTest.java
Normal file
@ -0,0 +1,291 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 8171177
|
||||
* @summary Verify that ModuleResolution attribute flags are honored.
|
||||
* @library /tools/lib
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
* jdk.compiler/com.sun.tools.javac.main
|
||||
* jdk.jdeps/com.sun.tools.classfile
|
||||
* jdk.jdeps/com.sun.tools.javap
|
||||
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.JavapTask ModuleTestBase
|
||||
* @run main IncubatingTest
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sun.tools.classfile.Attribute;
|
||||
import com.sun.tools.classfile.Attributes;
|
||||
import com.sun.tools.classfile.ClassFile;
|
||||
import com.sun.tools.classfile.ClassWriter;
|
||||
import com.sun.tools.classfile.ConstantPool;
|
||||
import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
|
||||
import com.sun.tools.classfile.ConstantPool.CPInfo;
|
||||
import com.sun.tools.classfile.ModuleResolution_attribute;
|
||||
import toolbox.JavacTask;
|
||||
import toolbox.Task;
|
||||
import toolbox.Task.Expect;
|
||||
|
||||
public class IncubatingTest extends ModuleTestBase {
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
new IncubatingTest().runTests();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoNotResolve(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
tb.writeJavaFiles(src,
|
||||
"module jdk.i { exports api; }",
|
||||
"package api; public class Api { }");
|
||||
Path classes = base.resolve("classes");
|
||||
Files.deleteIfExists(classes);
|
||||
Path iClasses = classes.resolve("jdk.i");
|
||||
tb.createDirectories(iClasses);
|
||||
|
||||
new JavacTask(tb)
|
||||
.outdir(iClasses)
|
||||
.files(findJavaFiles(src))
|
||||
.run()
|
||||
.writeAll();
|
||||
|
||||
copyJavaBase(classes);
|
||||
|
||||
Path jdkIModuleInfo = iClasses.resolve("module-info.class");
|
||||
addModuleResolutionAttribute(jdkIModuleInfo, ModuleResolution_attribute.DO_NOT_RESOLVE_BY_DEFAULT);
|
||||
|
||||
Path testSrc = base.resolve("test-src");
|
||||
tb.writeJavaFiles(testSrc,
|
||||
"class T { api.Api api; }");
|
||||
Path testClasses = base.resolve("test-classes");
|
||||
tb.createDirectories(testClasses);
|
||||
|
||||
List<String> log;
|
||||
List<String> expected;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("--system", "none",
|
||||
"--upgrade-module-path", classes.toString(),
|
||||
"-XDrawDiagnostics")
|
||||
.outdir(testClasses)
|
||||
.files(findJavaFiles(testSrc))
|
||||
.run(Expect.FAIL)
|
||||
.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"
|
||||
);
|
||||
|
||||
if (!expected.equals(log)) {
|
||||
throw new AssertionError("Unexpected output: " + log);
|
||||
}
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("--system", "none",
|
||||
"--upgrade-module-path", classes.toString(),
|
||||
"--add-modules", "ALL-SYSTEM",
|
||||
"-XDrawDiagnostics")
|
||||
.outdir(testClasses)
|
||||
.files(findJavaFiles(testSrc))
|
||||
.run(Expect.FAIL)
|
||||
.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"
|
||||
);
|
||||
|
||||
if (!expected.equals(log)) {
|
||||
throw new AssertionError("Unexpected output: " + log);
|
||||
}
|
||||
|
||||
new JavacTask(tb)
|
||||
.options("--system", "none",
|
||||
"--upgrade-module-path", classes.toString(),
|
||||
"--add-modules", "jdk.i")
|
||||
.outdir(testClasses)
|
||||
.files(findJavaFiles(testSrc))
|
||||
.run()
|
||||
.writeAll();
|
||||
|
||||
Path testModuleSrc = base.resolve("test-module-src");
|
||||
tb.writeJavaFiles(testModuleSrc,
|
||||
"module test { requires jdk.i; }", //explicit requires of an incubating module
|
||||
"class T { api.Api api; }");
|
||||
Path testModuleClasses = base.resolve("test-module-classes");
|
||||
tb.createDirectories(testModuleClasses);
|
||||
|
||||
new JavacTask(tb)
|
||||
.options("--system", "none",
|
||||
"--upgrade-module-path", classes.toString())
|
||||
.outdir(testModuleClasses)
|
||||
.files(findJavaFiles(testModuleSrc))
|
||||
.run()
|
||||
.writeAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncubating(Path base) throws Exception {
|
||||
Path src = base.resolve("src");
|
||||
tb.writeJavaFiles(src,
|
||||
"module jdk.i { exports api; }",
|
||||
"package api; public class Api { }");
|
||||
Path classes = base.resolve("classes");
|
||||
Files.deleteIfExists(classes);
|
||||
Path iClasses = classes.resolve("jdk.i");
|
||||
tb.createDirectories(iClasses);
|
||||
|
||||
new JavacTask(tb)
|
||||
.outdir(iClasses)
|
||||
.files(findJavaFiles(src))
|
||||
.run()
|
||||
.writeAll();
|
||||
|
||||
Path jdkIModuleInfo = iClasses.resolve("module-info.class");
|
||||
addModuleResolutionAttribute(jdkIModuleInfo, ModuleResolution_attribute.WARN_INCUBATING);
|
||||
|
||||
Path testSrc = base.resolve("test-src");
|
||||
tb.writeJavaFiles(testSrc,
|
||||
"class T { api.Api api; }");
|
||||
Path testClasses = base.resolve("test-classes");
|
||||
tb.createDirectories(testClasses);
|
||||
|
||||
List<String> log;
|
||||
List<String> expected;
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("--module-path", classes.toString(),
|
||||
"--add-modules", "jdk.i",
|
||||
"-XDrawDiagnostics",
|
||||
"-Werror")
|
||||
.outdir(testClasses)
|
||||
.files(findJavaFiles(testSrc))
|
||||
.run(Expect.FAIL)
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.DIRECT);
|
||||
|
||||
expected = Arrays.asList(
|
||||
"- compiler.warn.incubating.modules: jdk.i",
|
||||
"- compiler.err.warnings.and.werror",
|
||||
"1 error",
|
||||
"1 warning"
|
||||
);
|
||||
|
||||
if (!expected.equals(log)) {
|
||||
throw new AssertionError("Unexpected output: " + log);
|
||||
}
|
||||
|
||||
Path testModuleSrc = base.resolve("test-module-src");
|
||||
tb.writeJavaFiles(testModuleSrc,
|
||||
"module test { requires jdk.i; }", //explicit requires of an incubating module
|
||||
"class T { api.Api api; }");
|
||||
Path testModuleClasses = base.resolve("test-module-classes");
|
||||
tb.createDirectories(testModuleClasses);
|
||||
|
||||
log = new JavacTask(tb)
|
||||
.options("--module-path", classes.toString(),
|
||||
"-XDrawDiagnostics",
|
||||
"-Werror")
|
||||
.outdir(testModuleClasses)
|
||||
.files(findJavaFiles(testModuleSrc))
|
||||
.run(Expect.FAIL)
|
||||
.writeAll()
|
||||
.getOutputLines(Task.OutputKind.DIRECT);
|
||||
|
||||
expected = Arrays.asList(
|
||||
"- compiler.warn.incubating.modules: jdk.i",
|
||||
"- compiler.err.warnings.and.werror",
|
||||
"1 error",
|
||||
"1 warning"
|
||||
);
|
||||
|
||||
if (!expected.equals(log)) {
|
||||
throw new AssertionError("Unexpected output: " + log);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyJavaBase(Path targetDir) throws IOException {
|
||||
FileSystem jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
|
||||
Path javaBase = jrt.getPath("modules", "java.base");
|
||||
|
||||
if (!Files.exists(javaBase)) {
|
||||
throw new AssertionError("No java.base?");
|
||||
}
|
||||
|
||||
Path javaBaseClasses = targetDir.resolve("java.base");
|
||||
|
||||
for (Path clazz : tb.findFiles("class", javaBase)) {
|
||||
Path target = javaBaseClasses.resolve(javaBase.relativize(clazz).toString());
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(clazz, target);
|
||||
}
|
||||
}
|
||||
|
||||
private void addModuleResolutionAttribute(Path classfile, int resolution_flags) throws Exception {
|
||||
ClassFile cf = ClassFile.read(classfile);
|
||||
Attributes attrs = cf.attributes;
|
||||
List<CPInfo> cpData = new ArrayList<>();
|
||||
cpData.add(null);
|
||||
for (CPInfo info : cf.constant_pool.entries()) {
|
||||
cpData.add(info);
|
||||
if (info.size() == 2)
|
||||
cpData.add(null);
|
||||
}
|
||||
cpData.add(new CONSTANT_Utf8_info(Attribute.ModuleResolution));
|
||||
ConstantPool newCP = new ConstantPool(cpData.toArray(new CPInfo[0]));
|
||||
ModuleResolution_attribute res = new ModuleResolution_attribute(newCP, resolution_flags);
|
||||
Map<String, Attribute> newAttributeMap = new HashMap<>(attrs.map);
|
||||
newAttributeMap.put(Attribute.ModuleResolution, res);
|
||||
Attributes newAttrs = new Attributes(newAttributeMap);
|
||||
ClassFile newCF = new ClassFile(cf.magic,
|
||||
cf.minor_version,
|
||||
cf.major_version,
|
||||
newCP,
|
||||
cf.access_flags,
|
||||
cf.this_class,
|
||||
cf.super_class,
|
||||
cf.interfaces,
|
||||
cf.fields,
|
||||
cf.methods,
|
||||
newAttrs);
|
||||
try (OutputStream out = Files.newOutputStream(classfile)) {
|
||||
new ClassWriter().write(newCF, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2006, 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
|
||||
@ -88,6 +88,8 @@ public class Main {
|
||||
Path path = fm.asPath(file);
|
||||
int moduleIndex = path.getNameCount() - type.split("\\Q.\\E").length - 1;
|
||||
String moduleName = path.getName(moduleIndex).toString();
|
||||
if (moduleName.startsWith("jdk.incubator.")) //incubator modules not in module graph by default
|
||||
continue;
|
||||
try {
|
||||
ModuleElement me = elements.getModuleElement(moduleName);
|
||||
me.getClass();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user