src/java.base/share/classes/java/util/spi/ToolProvider.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 53927 03924ad3f4d0
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     1
/*
53927
03924ad3f4d0 8219548: Better Null paramenter checking in ToolProvider
lancea
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     4
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    10
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    15
 * accompanied this code).
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    16
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    20
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    23
 * questions.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    24
 */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    25
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    26
package java.util.spi;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    27
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    28
import java.io.PrintStream;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    29
import java.io.PrintWriter;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    30
import java.security.AccessController;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    31
import java.security.PrivilegedAction;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    32
import java.util.Objects;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    33
import java.util.Optional;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    34
import java.util.ServiceLoader;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    35
import java.util.stream.StreamSupport;
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    36
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    37
/**
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    38
 * An interface for command-line tools to provide a way to
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    39
 * be invoked without necessarily starting a new VM.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    40
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    41
 * <p>Tool providers are normally located using the service-provider
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    42
 * loading facility defined by {@link ServiceLoader}.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    43
 * Each provider must provide a name, and a method to run
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    44
 * an instance of the corresponding tool. When a tool is run,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    45
 * it will be provided with an array of string arguments, and a
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    46
 * pair of streams: one for normal (or expected) output and the other
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    47
 * for reporting any errors that may occur.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    48
 * The interpretation of the string arguments will normally be defined by
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    49
 * each individual tool provider, but will generally correspond to the
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    50
 * arguments that could be provided to the tool when invoking the tool
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    51
 * from the command line.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    52
 *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    53
 * @since 9
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    54
 */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    55
public interface ToolProvider {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    56
    /**
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    57
     * Returns the name of this tool provider.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    58
     *
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    59
     * @apiNote It is recommended that the name be the same as would be
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    60
     * used on the command line: for example, "javac", "jar", "jlink".
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    61
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    62
     * @return the name of this tool provider
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    63
     */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    64
    String name();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    65
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    66
    /**
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    67
     * Runs an instance of the tool, returning zero for a successful run.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    68
     * Any non-zero return value indicates a tool-specific error during the
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    69
     * execution.
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    70
     *
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    71
     * Two streams should be provided, for "expected" output, and for any
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    72
     * error messages. If it is not necessary to distinguish the output,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    73
     * the same stream may be used for both.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    74
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    75
     * @apiNote The interpretation of the arguments will be specific to
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    76
     * each tool.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    77
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    78
     * @param out a stream to which "expected" output should be written
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    79
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    80
     * @param err a stream to which any error messages should be written
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    81
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    82
     * @param args the command-line arguments for the tool
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    83
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    84
     * @return the result of executing the tool.
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    85
     *         A return value of 0 means the tool did not encounter any errors;
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    86
     *         any other value indicates that at least one error occurred
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    87
     *         during execution.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    88
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    89
     * @throws NullPointerException if any of the arguments are {@code null},
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    90
     *         or if there are any {@code null} values in the {@code args}
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    91
     *         array
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    92
     */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    93
    int run(PrintWriter out, PrintWriter err, String... args);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    94
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    95
    /**
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    96
     * Runs an instance of the tool, returning zero for a successful run.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    97
     * Any non-zero return value indicates a tool-specific error during the
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
    98
     * execution.
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
    99
     *
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   100
     * Two streams should be provided, for "expected" output, and for any
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   101
     * error messages. If it is not necessary to distinguish the output,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   102
     * the same stream may be used for both.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   103
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   104
     * @apiNote The interpretation of the arguments will be specific to
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   105
     * each tool.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   106
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   107
     * @implNote This implementation wraps the {@code out} and {@code err}
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   108
     * streams within {@link PrintWriter}s, and then calls
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   109
     * {@link #run(PrintWriter, PrintWriter, String[])}.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   110
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   111
     * @param out a stream to which "expected" output should be written
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   112
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   113
     * @param err a stream to which any error messages should be written
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   114
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   115
     * @param args the command-line arguments for the tool
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   116
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   117
     * @return the result of executing the tool.
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   118
     *         A return value of 0 means the tool did not encounter any errors;
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   119
     *         any other value indicates that at least one error occurred
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   120
     *         during execution.
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   121
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   122
     * @throws NullPointerException if any of the arguments are {@code null},
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   123
     *         or if there are any {@code null} values in the {@code args}
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41376
diff changeset
   124
     *         array
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   125
     */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   126
    default int run(PrintStream out, PrintStream err, String... args) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   127
        Objects.requireNonNull(out);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   128
        Objects.requireNonNull(err);
53927
03924ad3f4d0 8219548: Better Null paramenter checking in ToolProvider
lancea
parents: 47216
diff changeset
   129
        Objects.requireNonNull(args);
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   130
        for (String arg : args) {
53927
03924ad3f4d0 8219548: Better Null paramenter checking in ToolProvider
lancea
parents: 47216
diff changeset
   131
            Objects.requireNonNull(arg);
41376
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   132
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   133
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   134
        PrintWriter outWriter = new PrintWriter(out);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   135
        PrintWriter errWriter = new PrintWriter(err);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   136
        try {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   137
            try {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   138
                return run(outWriter, errWriter, args);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   139
            } finally {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   140
                outWriter.flush();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   141
            }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   142
        } finally {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   143
            errWriter.flush();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   144
        }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   145
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   146
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   147
    /**
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   148
     * Returns the first instance of a {@code ToolProvider} with the given name,
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   149
     * as loaded by {@link ServiceLoader} using the system class loader.
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   150
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   151
     * @param name the name of the desired tool provider
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   152
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   153
     * @return an {@code Optional<ToolProvider>} of the first instance found
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   154
     *
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   155
     * @throws NullPointerException if {@code name} is {@code null}
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   156
     */
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   157
    static Optional<ToolProvider> findFirst(String name) {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   158
        Objects.requireNonNull(name);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   159
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   160
        return AccessController.doPrivileged(
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   161
            (PrivilegedAction<Optional<ToolProvider>>) () -> {
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   162
                ServiceLoader<ToolProvider> sl =
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   163
                    ServiceLoader.load(ToolProvider.class, systemClassLoader);
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   164
                return StreamSupport.stream(sl.spliterator(), false)
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   165
                    .filter(p -> p.name().equals(name))
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   166
                    .findFirst();
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   167
            });
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   168
    }
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   169
}
0908484888a7 8159855: Create an SPI for tools
jjg
parents:
diff changeset
   170