src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellToolProvider.java
changeset 47216 71c04702a3d5
parent 42843 a8d83044a192
child 47840 e0f08a49f3e3
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.jshell.tool;
       
    27 
       
    28 import java.io.InputStream;
       
    29 import java.io.OutputStream;
       
    30 import java.io.PrintStream;
       
    31 import java.util.Collections;
       
    32 import java.util.EnumSet;
       
    33 import java.util.Set;
       
    34 import javax.lang.model.SourceVersion;
       
    35 import javax.tools.Tool;
       
    36 import jdk.jshell.tool.JavaShellToolBuilder;
       
    37 
       
    38 /**
       
    39  * Provider for launching the jshell tool.
       
    40  */
       
    41 public class JShellToolProvider implements Tool {
       
    42 
       
    43     /**
       
    44      * Returns the name of this Java shell tool provider.
       
    45      *
       
    46      * @return the name of this tool provider
       
    47      */
       
    48     @Override
       
    49     public String name() {
       
    50         return "jshell";
       
    51     }
       
    52 
       
    53     /**
       
    54      * Run the jshell tool.  The streams {@code out} and {@code err} are
       
    55      * converted to {@code PrintStream} if they are not already.
       
    56      * Any {@code Exception} is caught, printed and results in a non-zero return.
       
    57      *
       
    58      * @param in command line input (snippets and commands), and execution
       
    59      * "standard" input; use System.in if null
       
    60      * @param out command line output, feedback including errors, and execution
       
    61      * "standard" output; use System.out if null
       
    62      * @param err start-up errors and execution "standard" error; use System.err
       
    63      * if null
       
    64      * @param arguments arguments to pass to the tool
       
    65      * @return 0 for success; nonzero otherwise
       
    66      * @throws NullPointerException if the array of arguments contains
       
    67      * any {@code null} elements.
       
    68      */
       
    69     @Override
       
    70     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
       
    71         InputStream xin =
       
    72                 (in == null)
       
    73                         ? System.in
       
    74                         : in;
       
    75         PrintStream xout =
       
    76                 (out == null)
       
    77                         ? System.out
       
    78                         : (out instanceof PrintStream)
       
    79                                 ? (PrintStream) out
       
    80                                 : new PrintStream(out);
       
    81         PrintStream xerr =
       
    82                 (err == null)
       
    83                         ? System.err
       
    84                         : (err instanceof PrintStream)
       
    85                                 ? (PrintStream) err
       
    86                                 : new PrintStream(err);
       
    87         try {
       
    88             JavaShellToolBuilder
       
    89                     .builder()
       
    90                     .in(xin, null)
       
    91                     .out(xout)
       
    92                     .err(xerr)
       
    93                     .run(arguments);
       
    94             return 0;
       
    95         } catch (Throwable ex) {
       
    96             xerr.println(ex.getMessage());
       
    97             return 1;
       
    98         }
       
    99     }
       
   100 
       
   101     /**
       
   102      * Returns the source versions of the jshell tool.
       
   103      * @return a set of supported source versions
       
   104      */
       
   105     @Override
       
   106     public Set<SourceVersion> getSourceVersions() {
       
   107         return Collections.unmodifiableSet(
       
   108                 EnumSet.range(SourceVersion.RELEASE_9, SourceVersion.latest()));
       
   109     }
       
   110 
       
   111     /**
       
   112      * Launch the tool.
       
   113      * @param arguments the command-line arguments (including options), if any
       
   114      * @throws Exception an unexpected fatal exception
       
   115      */
       
   116     public static void main(String[] arguments) throws Exception {
       
   117         JavaShellToolBuilder
       
   118                 .builder()
       
   119                 .run(arguments);
       
   120     }
       
   121 }