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