8290561: Coalesce incubator-module warnings for single-file source-code programs
Reviewed-by: mcimadamore
This commit is contained in:
parent
8e22f2bb40
commit
26f2a978f6
@ -165,6 +165,7 @@ public class Modules extends JCTree.Visitor {
|
|||||||
private final String limitModsOpt;
|
private final String limitModsOpt;
|
||||||
private final Set<String> extraLimitMods = new HashSet<>();
|
private final Set<String> extraLimitMods = new HashSet<>();
|
||||||
private final String moduleVersionOpt;
|
private final String moduleVersionOpt;
|
||||||
|
private final boolean sourceLauncher;
|
||||||
|
|
||||||
private final boolean lintOptions;
|
private final boolean lintOptions;
|
||||||
|
|
||||||
@ -214,6 +215,7 @@ public class Modules extends JCTree.Visitor {
|
|||||||
addModsOpt = options.get(Option.ADD_MODULES);
|
addModsOpt = options.get(Option.ADD_MODULES);
|
||||||
limitModsOpt = options.get(Option.LIMIT_MODULES);
|
limitModsOpt = options.get(Option.LIMIT_MODULES);
|
||||||
moduleVersionOpt = options.get(Option.MODULE_VERSION);
|
moduleVersionOpt = options.get(Option.MODULE_VERSION);
|
||||||
|
sourceLauncher = options.isSet("sourceLauncher");
|
||||||
}
|
}
|
||||||
|
|
||||||
int depth = -1;
|
int depth = -1;
|
||||||
@ -1341,9 +1343,9 @@ public class Modules extends JCTree.Visitor {
|
|||||||
.forEach(result::add);
|
.forEach(result::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
String incubatingModules = result.stream()
|
String incubatingModules = filterAlreadyWarnedIncubatorModules(result.stream()
|
||||||
.filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
|
.filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
|
||||||
.map(msym -> msym.name.toString())
|
.map(msym -> msym.name.toString()))
|
||||||
.collect(Collectors.joining(","));
|
.collect(Collectors.joining(","));
|
||||||
|
|
||||||
if (!incubatingModules.isEmpty()) {
|
if (!incubatingModules.isEmpty()) {
|
||||||
@ -1359,6 +1361,15 @@ public class Modules extends JCTree.Visitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//where:
|
//where:
|
||||||
|
private Stream<String> filterAlreadyWarnedIncubatorModules(Stream<String> incubatingModules) {
|
||||||
|
if (!sourceLauncher) return incubatingModules;
|
||||||
|
Set<String> bootModules = ModuleLayer.boot()
|
||||||
|
.modules()
|
||||||
|
.stream()
|
||||||
|
.map(Module::getName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
return incubatingModules.filter(module -> !bootModules.contains(module));
|
||||||
|
}
|
||||||
private static final Predicate<ModuleSymbol> IS_AUTOMATIC =
|
private static final Predicate<ModuleSymbol> IS_AUTOMATIC =
|
||||||
m -> (m.flags_field & Flags.AUTOMATIC_MODULE) != 0;
|
m -> (m.flags_field & Flags.AUTOMATIC_MODULE) != 0;
|
||||||
|
|
||||||
|
@ -368,6 +368,7 @@ public class Main {
|
|||||||
javacOpts.add("-Xlint:deprecation");
|
javacOpts.add("-Xlint:deprecation");
|
||||||
javacOpts.add("-Xlint:unchecked");
|
javacOpts.add("-Xlint:unchecked");
|
||||||
javacOpts.add("-Xlint:-options");
|
javacOpts.add("-Xlint:-options");
|
||||||
|
javacOpts.add("-XDsourceLauncher");
|
||||||
return javacOpts;
|
return javacOpts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,13 +29,22 @@
|
|||||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||||
* jdk.compiler/com.sun.tools.javac.launcher
|
* jdk.compiler/com.sun.tools.javac.launcher
|
||||||
* jdk.compiler/com.sun.tools.javac.main
|
* jdk.compiler/com.sun.tools.javac.main
|
||||||
|
* jdk.jdeps/com.sun.tools.classfile
|
||||||
* @build toolbox.JavaTask toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
|
* @build toolbox.JavaTask toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
|
||||||
* @run main SourceLauncherTest
|
* @run main SourceLauncherTest
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
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.CPInfo;
|
||||||
|
import com.sun.tools.classfile.ModuleResolution_attribute;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
@ -45,6 +54,8 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -655,6 +666,83 @@ public class SourceLauncherTest extends TestRunner {
|
|||||||
"at Thrower.main(Thrower.java:4)");
|
"at Thrower.main(Thrower.java:4)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoDuplicateIncubatorWarning(Path base) throws Exception {
|
||||||
|
Path module = base.resolve("lib");
|
||||||
|
Path moduleSrc = module.resolve("src");
|
||||||
|
Path moduleClasses = module.resolve("classes");
|
||||||
|
Files.createDirectories(moduleClasses);
|
||||||
|
tb.cleanDirectory(moduleClasses);
|
||||||
|
tb.writeJavaFiles(moduleSrc, "module test {}");
|
||||||
|
new JavacTask(tb)
|
||||||
|
.outdir(moduleClasses)
|
||||||
|
.files(tb.findJavaFiles(moduleSrc))
|
||||||
|
.run()
|
||||||
|
.writeAll();
|
||||||
|
markModuleAsIncubator(moduleClasses.resolve("module-info.class"));
|
||||||
|
tb.writeJavaFiles(base, "public class Main { public static void main(String... args) {}}");
|
||||||
|
String log = new JavaTask(tb)
|
||||||
|
.vmOptions("--module-path", moduleClasses.toString(),
|
||||||
|
"--add-modules", "test")
|
||||||
|
.className(base.resolve("Main.java").toString())
|
||||||
|
.run(Task.Expect.SUCCESS)
|
||||||
|
.writeAll()
|
||||||
|
.getOutput(Task.OutputKind.STDERR);
|
||||||
|
|
||||||
|
int numberOfWarnings = log.split("WARNING").length - 1;
|
||||||
|
|
||||||
|
if (log.contains("warning:") || numberOfWarnings != 1) {
|
||||||
|
error("Unexpected warning in error output: " + log);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> compileLog = new JavacTask(tb)
|
||||||
|
.options("--module-path", moduleClasses.toString(),
|
||||||
|
"--add-modules", "test",
|
||||||
|
"-XDrawDiagnostics",
|
||||||
|
"-XDsourceLauncher",
|
||||||
|
"-XDshould-stop.at=FLOW")
|
||||||
|
.files(base.resolve("Main.java").toString())
|
||||||
|
.run(Task.Expect.SUCCESS)
|
||||||
|
.writeAll()
|
||||||
|
.getOutputLines(Task.OutputKind.DIRECT);
|
||||||
|
|
||||||
|
List<String> expectedOutput = List.of(
|
||||||
|
"- compiler.warn.incubating.modules: test",
|
||||||
|
"1 warning"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!expectedOutput.equals(compileLog)) {
|
||||||
|
error("Unexpected options : " + compileLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//where:
|
||||||
|
private static void markModuleAsIncubator(Path moduleInfoFile) throws Exception {
|
||||||
|
ClassFile cf = ClassFile.read(moduleInfoFile);
|
||||||
|
List<CPInfo> newPool = new ArrayList<>();
|
||||||
|
newPool.add(null);
|
||||||
|
cf.constant_pool.entries().forEach(newPool::add);
|
||||||
|
int moduleResolutionIndex = newPool.size();
|
||||||
|
newPool.add(new ConstantPool.CONSTANT_Utf8_info(Attribute.ModuleResolution));
|
||||||
|
Map<String, Attribute> newAttributes = new HashMap<>(cf.attributes.map);
|
||||||
|
newAttributes.put(Attribute.ModuleResolution,
|
||||||
|
new ModuleResolution_attribute(moduleResolutionIndex,
|
||||||
|
ModuleResolution_attribute.WARN_INCUBATING));
|
||||||
|
ClassFile newClassFile = new ClassFile(cf.magic,
|
||||||
|
cf.minor_version,
|
||||||
|
cf.major_version,
|
||||||
|
new ConstantPool(newPool.toArray(new CPInfo[0])),
|
||||||
|
cf.access_flags,
|
||||||
|
cf.this_class,
|
||||||
|
cf.super_class,
|
||||||
|
cf.interfaces,
|
||||||
|
cf.fields,
|
||||||
|
cf.methods,
|
||||||
|
new Attributes(newAttributes));
|
||||||
|
try (OutputStream out = Files.newOutputStream(moduleInfoFile)) {
|
||||||
|
new ClassWriter().write(newClassFile, out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Result run(Path file, List<String> runtimeArgs, List<String> appArgs) {
|
Result run(Path file, List<String> runtimeArgs, List<String> appArgs) {
|
||||||
List<String> args = new ArrayList<>();
|
List<String> args = new ArrayList<>();
|
||||||
args.add(file.toString());
|
args.add(file.toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user