Skip to content

Commit

Permalink
8290561: Coalesce incubator-module warnings for single-file source-co…
Browse files Browse the repository at this point in the history
…de programs

Reviewed-by: mcimadamore
  • Loading branch information
lahodaj committed Sep 6, 2022
1 parent 8e22f2b commit 26f2a97
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
Expand Up @@ -165,6 +165,7 @@ public class Modules extends JCTree.Visitor {
private final String limitModsOpt;
private final Set<String> extraLimitMods = new HashSet<>();
private final String moduleVersionOpt;
private final boolean sourceLauncher;

private final boolean lintOptions;

Expand Down Expand Up @@ -214,6 +215,7 @@ protected Modules(Context context) {
addModsOpt = options.get(Option.ADD_MODULES);
limitModsOpt = options.get(Option.LIMIT_MODULES);
moduleVersionOpt = options.get(Option.MODULE_VERSION);
sourceLauncher = options.isSet("sourceLauncher");
}

int depth = -1;
Expand Down Expand Up @@ -1341,9 +1343,9 @@ private void setupAllModules() {
.forEach(result::add);
}

String incubatingModules = result.stream()
String incubatingModules = filterAlreadyWarnedIncubatorModules(result.stream()
.filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
.map(msym -> msym.name.toString())
.map(msym -> msym.name.toString()))
.collect(Collectors.joining(","));

if (!incubatingModules.isEmpty()) {
Expand All @@ -1359,6 +1361,15 @@ private void setupAllModules() {
}
}
//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 =
m -> (m.flags_field & Flags.AUTOMATIC_MODULE) != 0;

Expand Down
Expand Up @@ -368,6 +368,7 @@ private List<String> getJavacOpts(String... runtimeArgs) throws Fault {
javacOpts.add("-Xlint:deprecation");
javacOpts.add("-Xlint:unchecked");
javacOpts.add("-Xlint:-options");
javacOpts.add("-XDsourceLauncher");
return javacOpts;
}

Expand Down
88 changes: 88 additions & 0 deletions test/langtools/tools/javac/launcher/SourceLauncherTest.java
Expand Up @@ -29,13 +29,22 @@
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.launcher
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.classfile
* @build toolbox.JavaTask toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
* @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.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -45,6 +54,8 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -655,6 +666,83 @@ public void testTargetException1(Path base) throws IOException {
"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) {
List<String> args = new ArrayList<>();
args.add(file.toString());
Expand Down

1 comment on commit 26f2a97

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.