8323605: Java source launcher should not require --source ... to enable preview

Reviewed-by: mcimadamore, dholmes
This commit is contained in:
Christian Stein 2024-03-13 06:44:04 +00:00
parent 5d4bfad12b
commit 3b18c5dc5d
5 changed files with 15 additions and 19 deletions

View File

@ -279,6 +279,11 @@ the compilation.
This sets both the source version accepted by compiler and the system
API that may be used by the code in the source file.
.IP \[bu] 2
If \f[V]--enable-preview\f[R] is specified, the \f[V]--source N\f[R]
arguments can be omitted.
If the Java runtime version is \f[V]N\f[R], then \f[V]--release N\f[R]
is implied when compiling source files.
.IP \[bu] 2
If a \f[V]module-info.java\f[R] file exists in the
\f[V]<source-root>\f[R] directory, its module declaration is used to
define a named module that will contain all the classes compiled from

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, 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
@ -112,7 +112,9 @@ record RelevantJavacOptions(List<String> forProgramCompilation,
programOptions.add(opt);
subsequentOptions.add(opt);
if (sourceOpt == null) {
throw new Fault(Errors.EnablePreviewRequiresSource);
String feature = String.valueOf(Runtime.version().feature());
programOptions.addAll(List.of("--release", feature));
subsequentOptions.addAll(List.of("--release", feature));
}
}
default -> {

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2018, 2024, 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
@ -139,9 +139,6 @@ launcher.err.no.value.for.option=\
launcher.err.invalid.value.for.source=\
invalid value for --source option: {0}
launcher.err.enable.preview.requires.source=\
--enable-preview must be used with --source
launcher.err.unnamed.pkg.not.allowed.named.modules=\
unnamed package is not allowed in named modules

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, 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
@ -100,11 +100,7 @@ class BasicSourceLauncherTests {
}
""");
// Replace with plain Run.of(hi) once implict classes are out of preview
System.setProperty("jdk.internal.javac.source", String.valueOf(Runtime.version().feature()));
var run = Run.of(hi, List.of("--enable-preview"), List.of());
System.clearProperty("jdk.internal.javac.source");
assertAll("# " + run,
() -> assertLinesMatch(
"""

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, 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
@ -51,12 +51,9 @@ import java.nio.file.Path;
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;
import java.util.stream.Collectors;
import com.sun.tools.javac.launcher.SourceLauncher;
import com.sun.tools.javac.launcher.Fault;
@ -534,10 +531,9 @@ public class SourceLauncherTest extends TestRunner {
List<String> log = new JavaTask(tb)
.vmOptions("--enable-preview")
.className(base.resolve("HelloWorld.java").toString())
.run(Task.Expect.FAIL)
.getOutputLines(Task.OutputKind.STDERR);
log = log.stream().filter(s->!s.matches("^Picked up .*JAVA.*OPTIONS:.*")).collect(Collectors.toList());
checkEqual("stderr", log, List.of("error: --enable-preview must be used with --source"));
.run(Task.Expect.SUCCESS)
.getOutputLines(Task.OutputKind.STDOUT);
checkEqual("stdout", log, List.of("Hello World! []"));
}
@Test