8159635: JShell API: Add compiler options
authorrfield
Fri, 01 Jul 2016 14:16:28 -0700
changeset 39369 0469f052203d
parent 39368 59320a0754e7
child 39370 437ba9bd2582
8159635: JShell API: Add compiler options Reviewed-by: jlahoda
langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java
langtools/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java
langtools/test/jdk/jshell/CompilerOptionsTest.java
langtools/test/jdk/jshell/KullaTesting.java
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Thu Jun 30 22:39:09 2016 +0000
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/JShell.java	Fri Jul 01 14:16:28 2016 -0700
@@ -89,6 +89,7 @@
     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 @@
         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 @@
         Supplier<String> tempVariableNameGenerator = null;
         BiFunction<Snippet, Integer, String> idGenerator = null;
         List<String> extraRemoteVMOptions = new ArrayList<>();
+        List<String> extraCompilerOptions = new ArrayList<>();
         ExecutionControl executionControl;
 
         Builder() { }
@@ -289,6 +292,21 @@
         }
 
         /**
+         * 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.
          *
--- a/langtools/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java	Thu Jun 30 22:39:09 2016 +0000
+++ b/langtools/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java	Fri Jul 01 14:16:28 2016 -0700
@@ -328,7 +328,9 @@
                 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());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/jdk/jshell/CompilerOptionsTest.java	Fri Jul 01 14:16:28 2016 -0700
@@ -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));
+    }
+}
--- a/langtools/test/jdk/jshell/KullaTesting.java	Thu Jun 30 22:39:09 2016 +0000
+++ b/langtools/test/jdk/jshell/KullaTesting.java	Fri Jul 01 14:16:28 2016 -0700
@@ -37,6 +37,7 @@
 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 @@
 
     @BeforeMethod
     public void setUp() {
+        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()
+        JShell.Builder builder = JShell.builder()
                 .in(inStream)
                 .out(new PrintStream(outStream))
-                .err(new PrintStream(errStream))
-                .build();
-        allSnippets = new LinkedHashSet<>();
-        idToSnippet = new LinkedHashMap<>();
-        classpath = new ArrayList<>();
-    }
-
-    public void setUp(ExecutionControl ec) {
-        inStream = new TestingInputStream();
-        outStream = new ByteArrayOutputStream();
-        errStream = new ByteArrayOutputStream();
-        state = JShell.builder()
-                .executionEngine(ec)
-                .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<>();