# HG changeset patch # User jlahoda # Date 1569332426 -7200 # Node ID d885633d9de4cb751e115b68d98f349df686684e # Parent ee07de0d2c1639307549a82f13ddf8ed5488d62f Converting the test to a combo-framework test. diff -r ee07de0d2c16 -r d885633d9de4 src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java Thu Sep 12 17:47:24 2019 +0200 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java Tue Sep 24 15:40:26 2019 +0200 @@ -257,6 +257,7 @@ ((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear(); Types.instance(this).newRound(); Check.instance(this).newRound(); + Check.instance(this).clear(); //clear mandatory warning handlers Modules.instance(this).newRound(); Annotate.instance(this).newRound(); CompileStates.instance(this).clear(); diff -r ee07de0d2c16 -r d885633d9de4 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Thu Sep 12 17:47:24 2019 +0200 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Tue Sep 24 15:40:26 2019 +0200 @@ -465,6 +465,14 @@ localClassNameIndexes.clear(); } + public void clear() { + deprecationHandler.clear(); + removalHandler.clear(); + uncheckedHandler.clear(); + sunApiHandler.clear(); + previewHandler.clear(); + } + public void putCompiled(ClassSymbol csym) { compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym); } diff -r ee07de0d2c16 -r d885633d9de4 src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java Thu Sep 12 17:47:24 2019 +0200 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java Tue Sep 24 15:40:26 2019 +0200 @@ -44,6 +44,7 @@ import java.nio.file.Path; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Objects; @@ -173,6 +174,10 @@ private long lastUsedTime = System.currentTimeMillis(); protected long deferredCloseTimeout = 0; + public void clear() { + new HashSet<>(options.keySet()).forEach(k -> options.remove(k)); + } + protected ClassLoader getClassLoader(URL[] urls) { ClassLoader thisClassLoader = getClass().getClassLoader(); diff -r ee07de0d2c16 -r d885633d9de4 src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java Thu Sep 12 17:47:24 2019 +0200 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java Tue Sep 24 15:40:26 2019 +0200 @@ -186,18 +186,18 @@ /** * The log to which to report warnings. */ - private Log log; + private final Log log; /** * Whether or not to report individual warnings, or simply to report a * single aggregate warning at the end of the compilation. */ - private boolean verbose; + private final boolean verbose; /** * The common prefix for all I18N message keys generated by this handler. */ - private String prefix; + private final String prefix; /** * A set containing the names of the source files for which specific @@ -262,4 +262,11 @@ else log.note(file, new Note("compiler", msg, args)); } + + public void clear() { + sourcesWithReportedWarnings = null; + deferredDiagnosticKind = null; + deferredDiagnosticSource = null; + deferredDiagnosticArg = null; + } } diff -r ee07de0d2c16 -r d885633d9de4 test/langtools/tools/javac/lib/combo/ComboInstance.java --- a/test/langtools/tools/javac/lib/combo/ComboInstance.java Thu Sep 12 17:47:24 2019 +0200 +++ b/test/langtools/tools/javac/lib/combo/ComboInstance.java Tue Sep 24 15:40:26 2019 +0200 @@ -23,6 +23,7 @@ package combo; +import java.lang.reflect.Method; import javax.tools.StandardJavaFileManager; import java.util.Optional; @@ -57,6 +58,14 @@ env.info().lastError = Optional.of(ex); } finally { this.env = null; + try { + Class fmClass = env.fileManager().getClass(); + Method clear = fmClass.getMethod("clear"); + clear.setAccessible(true); + clear.invoke(env.fileManager()); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } } } @@ -125,4 +134,4 @@ return success; } } -} \ No newline at end of file +} diff -r ee07de0d2c16 -r d885633d9de4 test/langtools/tools/javac/preview/PreviewErrors.java --- a/test/langtools/tools/javac/preview/PreviewErrors.java Thu Sep 12 17:47:24 2019 +0200 +++ b/test/langtools/tools/javac/preview/PreviewErrors.java Tue Sep 24 15:40:26 2019 +0200 @@ -25,59 +25,64 @@ * @test * @bug 8226585 * @summary Verify behavior w.r.t. preview feature API errors and warnings - * @library /tools/lib + * @library /tools/lib /tools/javac/lib * @modules * java.base/jdk.internal * jdk.compiler/com.sun.tools.javac.api + * jdk.compiler/com.sun.tools.javac.file * jdk.compiler/com.sun.tools.javac.main + * jdk.compiler/com.sun.tools.javac.util * @build toolbox.ToolBox toolbox.JavacTask + * @build combo.ComboTestHelper * @compile --enable-preview -source ${jdk.version} PreviewErrors.java * @run main/othervm --enable-preview PreviewErrors */ -import toolbox.JavacTask; -import toolbox.Task; -import toolbox.TestRunner; -import toolbox.ToolBox; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; + +import combo.ComboInstance; +import combo.ComboParameter; +import combo.ComboTask; +import combo.ComboTestHelper; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; +import javax.tools.Diagnostic; + import jdk.internal.PreviewFeature; -public class PreviewErrors extends TestRunner { +import toolbox.JavacTask; +import toolbox.ToolBox; + +public class PreviewErrors extends ComboInstance { protected ToolBox tb; PreviewErrors() { - super(System.err); + super(); tb = new ToolBox(); } public static void main(String... args) throws Exception { - PreviewErrors t = new PreviewErrors(); - t.runTests(); + new ComboTestHelper() + .withDimension("ESSENTIAL", (x, essential) -> x.essential = essential, EssentialAPI.values()) + .withDimension("PREVIEW", (x, preview) -> x.preview = preview, Preview.values()) + .withDimension("LINT", (x, lint) -> x.lint = lint, Lint.values()) + .withDimension("SUPPRESS", (x, suppress) -> x.suppress = suppress, Suppress.values()) + .run(PreviewErrors::new); } - /** - * Run all methods annotated with @Test, and throw an exception if any - * errors are reported.. - * - * @throws Exception if any errors occurred - */ - protected void runTests() throws Exception { - runTests(m -> new Object[] { Paths.get(m.getName()) }); - } + private EssentialAPI essential; + private Preview preview; + private Lint lint; + private Suppress suppress; - Path[] findJavaFiles(Path... paths) throws IOException { - return tb.findJavaFiles(paths); - } - - @Test - public void essentialApi(Path base) throws Exception { + @Override + public void doWork() throws IOException { + Path base = Paths.get("."); Path src = base.resolve("src"); Path srcJavaBase = src.resolve("java.base"); Path classes = base.resolve("classes"); @@ -85,110 +90,95 @@ Files.createDirectories(classesJavaBase); - Path srcTest = src.resolve("test"); - Path classesTest = classes.resolve("test"); - - Files.createDirectories(classesTest); + tb.writeJavaFiles(srcJavaBase, + """ + package java.lang; + public class Extra { + @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.${preview} + ${essential}) + public static void test() { } + } + """.replace("${preview}", PreviewFeature.Feature.values()[0].name()) + .replace("${essential}", essential.expand(null))); - for (EssentialAPI essential : EssentialAPI.values()) { - tb.writeJavaFiles(srcJavaBase, - """ - package java.lang; - public class Extra { - @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.${preview} - ${essential}) - public static void test() { } - } - """.replace("${preview}", PreviewFeature.Feature.values()[0].name()) - .replace("${essential}", essential.code())); - - new JavacTask(tb) - .outdir(classesJavaBase) - .options("--patch-module", "java.base=" + srcJavaBase.toString()) - .files(findJavaFiles(srcJavaBase)) - .run() - .writeAll(); + new JavacTask(tb) + .outdir(classesJavaBase) + .options("--patch-module", "java.base=" + srcJavaBase.toString()) + .files(tb.findJavaFiles(srcJavaBase)) + .run() + .writeAll(); - for (Preview preview : Preview.values()) { - for (Lint lint : Lint.values()) { - for (Suppress suppress : Suppress.values()) { - tb.writeJavaFiles(srcTest, - """ - package test; - public class Test { - ${suppress} - public void test() { - Extra.test(); - } - } - """.replace("${suppress}", suppress.code())); + ComboTask task = newCompilationTask() + .withSourceFromTemplate(""" + package test; + public class Test { + #{SUPPRESS} + public void test() { + Extra.test(); + } + } + """) + .withOption("-XDrawDiagnostics") + .withOption("--patch-module") + .withOption("java.base=" + classesJavaBase.toString()) + .withOption("-source") + .withOption(String.valueOf(Runtime.version().feature())); + if (preview.expand(null)!= null) { + task = task.withOption(preview.expand(null)); + } - List options = new ArrayList<>(); + if (lint.expand(null) != null) { + task = task.withOption(lint.expand(null)); + } - options.add("-XDrawDiagnostics"); - options.add("--patch-module"); - options.add("java.base=" + classesJavaBase.toString()); - options.add("-source"); - options.add(String.valueOf(Runtime.version().feature())); - - if (preview.opt() != null) { - options.add(preview.opt()); + task.generate(result -> { + Set actual = Arrays.stream(Diagnostic.Kind.values()) + .flatMap(kind -> result.diagnosticsForKind(kind).stream()) + .map(d -> d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode()) + .collect(Collectors.toSet()); + Set expected; + boolean ok; + if (essential == EssentialAPI.YES) { + if (preview == Preview.YES) { + if (lint == Lint.ENABLE_PREVIEW) { + expected = Set.of("5:14:compiler.warn.is.preview"); + } else { + expected = Set.of("-1:-1:compiler.note.preview.filename", + "-1:-1:compiler.note.preview.recompile"); } - - if (lint.opt() != null) { - options.add(lint.opt()); - } - List output; - List expected; - Task.Expect expect; - - if (essential == EssentialAPI.YES) { - if (preview == Preview.YES) { - if (lint == Lint.ENABLE_PREVIEW) { - expected = List.of("Test.java:5:14: compiler.warn.is.preview: test()", - "1 warning"); - } else { - expected = List.of("- compiler.note.preview.filename: Test.java", - "- compiler.note.preview.recompile"); - } - expect = Task.Expect.SUCCESS; - } else { - expected = List.of("Test.java:5:14: compiler.err.is.preview: test()", - "1 error"); - expect = Task.Expect.FAIL; - } - } else { - if (suppress == Suppress.YES) { - expected = List.of(""); - } else if ((preview == Preview.YES && (lint == Lint.NONE || lint == Lint.DISABLE_PREVIEW)) || - (preview == Preview.NO && lint == Lint.DISABLE_PREVIEW)) { - expected = List.of("- compiler.note.preview.filename: Test.java", - "- compiler.note.preview.recompile"); - } else { - expected = List.of("Test.java:5:14: compiler.warn.is.preview: test()", - "1 warning"); - } - expect = Task.Expect.SUCCESS; - } - - output = new JavacTask(tb) - .outdir(classesTest) - .options(options) - .files(findJavaFiles(srcTest)) - .run(expect) - .writeAll() - .getOutputLines(Task.OutputKind.DIRECT); - - if (!expected.equals(output)) { - throw new IllegalStateException("Unexpected output for " + essential + ", " + preview + ", " + lint + ", " + suppress + ": " + output); - } + ok = true; + } else { + expected = Set.of("5:14:compiler.err.is.preview"); + ok = false; + } + } else { + if (suppress == Suppress.YES) { + expected = Set.of(); + } else if ((preview == Preview.YES && (lint == Lint.NONE || lint == Lint.DISABLE_PREVIEW)) || + (preview == Preview.NO && lint == Lint.DISABLE_PREVIEW)) { + expected = Set.of("-1:-1:compiler.note.preview.filename", + "-1:-1:compiler.note.preview.recompile"); + } else { + expected = Set.of("5:14:compiler.warn.is.preview"); + } + ok = true; + } + if (ok) { + if (!result.get().iterator().hasNext()) { + throw new IllegalStateException("Did not succeed as expected."); + } + } else { + if (result.get().iterator().hasNext()) { + throw new IllegalStateException("Succeed unexpectedly."); } } - } - } + if (!expected.equals(actual)) { + throw new IllegalStateException("Unexpected output for " + essential + ", " + preview + ", " + lint + ", " + suppress + ": " + actual.toString()); + } + }); } - public enum EssentialAPI { + public enum EssentialAPI implements ComboParameter { YES(", essentialAPI=true"), NO(", essentialAPI=false"); @@ -198,12 +188,12 @@ this.code = code; } - public String code() { + public String expand(String optParameter) { return code; } } - public enum Preview { + public enum Preview implements ComboParameter { YES("--enable-preview"), NO(null); @@ -213,12 +203,12 @@ this.opt = opt; } - public String opt() { + public String expand(String optParameter) { return opt; } } - public enum Lint { + public enum Lint implements ComboParameter { NONE(null), ENABLE_PREVIEW("-Xlint:preview"), DISABLE_PREVIEW("-Xlint:-preview"); @@ -229,12 +219,12 @@ this.opt = opt; } - public String opt() { + public String expand(String optParameter) { return opt; } } - public enum Suppress { + public enum Suppress implements ComboParameter { YES("@SuppressWarnings(\"preview\")"), NO(""); @@ -244,7 +234,7 @@ this.code = code; } - public String code() { + public String expand(String optParameter) { return code; } }