8159635: JShell API: Add compiler options

Reviewed-by: jlahoda
This commit is contained in:
Robert Field 2016-07-01 14:16:28 -07:00
parent a7d45966a9
commit c8f9b12b5c
4 changed files with 87 additions and 16 deletions

View File

@ -89,6 +89,7 @@ public class JShell implements AutoCloseable {
final Supplier<String> tempVariableNameGenerator;
final BiFunction<Snippet, Integer, String> idGenerator;
final List<String> extraRemoteVMOptions;
final List<String> extraCompilerOptions;
final ExecutionControl executionControl;
private int nextKeyIndex = 1;
@ -112,6 +113,7 @@ public class JShell implements AutoCloseable {
this.tempVariableNameGenerator = b.tempVariableNameGenerator;
this.idGenerator = b.idGenerator;
this.extraRemoteVMOptions = b.extraRemoteVMOptions;
this.extraCompilerOptions = b.extraCompilerOptions;
this.executionControl = b.executionControl==null
? new FailOverExecutionControl(
new JDIExecutionControl(),
@ -151,6 +153,7 @@ public class JShell implements AutoCloseable {
Supplier<String> tempVariableNameGenerator = null;
BiFunction<Snippet, Integer, String> idGenerator = null;
List<String> extraRemoteVMOptions = new ArrayList<>();
List<String> extraCompilerOptions = new ArrayList<>();
ExecutionControl executionControl;
Builder() { }
@ -288,6 +291,21 @@ public class JShell implements AutoCloseable {
return this;
}
/**
* Adds compiler options. These additional options will be used on
* parsing, analysis, and code generation calls to the compiler.
* Options which interfere with results are not supported and have
* undefined effects on JShell's operation.
*
* @param options the addition options for compiler invocations
* @return the {@code Builder} instance (for use in chained
* initialization)
*/
public Builder compilerOptions(String... options) {
this.extraCompilerOptions.addAll(Arrays.asList(options));
return this;
}
/**
* Sets the custom engine for execution. Snippet execution will be
* provided by the specified {@link ExecutionControl} instance.

View File

@ -328,7 +328,9 @@ class TaskFactory {
SourceHandler<T> sh,
String... extraOptions) {
this.sourceHandler = sh;
List<String> options = Arrays.asList(extraOptions);
List<String> options = new ArrayList<>(extraOptions.length + state.extraCompilerOptions.size());
options.addAll(Arrays.asList(extraOptions));
options.addAll(state.extraCompilerOptions);
Iterable<? extends JavaFileObject> compilationUnits = inputs
.map(in -> sh.sourceToFileObject(fileManager, in))
.collect(Collectors.toList());

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2016, 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 8159635
* @summary Test setting compiler options
* @build KullaTesting TestingInputStream
* @run testng CompilerOptionsTest
*/
import javax.tools.Diagnostic;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import static jdk.jshell.Snippet.Status.VALID;
@Test
public class CompilerOptionsTest extends KullaTesting {
@BeforeMethod
@Override
public void setUp() {
setUp(b -> b.compilerOptions("-source", "7", "-Xlint:cast"));
}
public void testLint() {
assertDeclareWarn1("String s = (String)\"hello\";",
new ExpectedDiagnostic("compiler.warn.redundant.cast", 11, 26, 11, -1, -1, Diagnostic.Kind.WARNING));
}
public void testSourceVersion() {
assertEval("import java.util.function.*;", added(VALID));
assertDeclareFail("Function<Integer,Integer> f = x -> x*2;",
new ExpectedDiagnostic("compiler.err.lambda.not.supported.in.source", 32, 32, 32, -1, -1, Diagnostic.Kind.ERROR));
}
}

View File

@ -37,6 +37,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ -154,29 +155,23 @@ public class KullaTesting {
@BeforeMethod
public void setUp() {
inStream = new TestingInputStream();
outStream = new ByteArrayOutputStream();
errStream = new ByteArrayOutputStream();
state = JShell.builder()
.in(inStream)
.out(new PrintStream(outStream))
.err(new PrintStream(errStream))
.build();
allSnippets = new LinkedHashSet<>();
idToSnippet = new LinkedHashMap<>();
classpath = new ArrayList<>();
setUp(b -> {});
}
public void setUp(ExecutionControl ec) {
setUp(b -> b.executionEngine(ec));
}
public void setUp(Consumer<JShell.Builder> bc) {
inStream = new TestingInputStream();
outStream = new ByteArrayOutputStream();
errStream = new ByteArrayOutputStream();
state = JShell.builder()
.executionEngine(ec)
JShell.Builder builder = JShell.builder()
.in(inStream)
.out(new PrintStream(outStream))
.err(new PrintStream(errStream))
.build();
.err(new PrintStream(errStream));
bc.accept(builder);
state = builder.build();
allSnippets = new LinkedHashSet<>();
idToSnippet = new LinkedHashMap<>();
classpath = new ArrayList<>();