Path: blob/master/src/java.compiler/share/classes/javax/tools/ToolProvider.java
41152 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.tools;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.util.Objects;30import java.util.ServiceConfigurationError;31import java.util.ServiceLoader;3233/**34* Provides methods for locating tool providers, for example,35* providers of compilers. This class complements the36* functionality of {@link java.util.ServiceLoader}.37*38* @author Peter von der Ahé39* @since 1.640*/41public class ToolProvider {4243private static final String systemJavaCompilerModule = "jdk.compiler";44private static final String systemJavaCompilerName = "com.sun.tools.javac.api.JavacTool";4546private ToolProvider() {}4748/**49* Returns the Java programming language compiler provided50* with this platform.51* <p>The file manager returned by calling52* {@link JavaCompiler#getStandardFileManager getStandardFileManager}53* on this compiler supports paths provided by any54* {@linkplain java.nio.file.FileSystem filesystem}.</p>55* @return the compiler provided with this platform or56* {@code null} if no compiler is provided57* @implNote This implementation returns the compiler provided58* by the {@code jdk.compiler} module if that module is available,59* and {@code null} otherwise.60*/61public static JavaCompiler getSystemJavaCompiler() {62return getSystemTool(JavaCompiler.class,63systemJavaCompilerModule, systemJavaCompilerName);64}6566private static final String systemDocumentationToolModule = "jdk.javadoc";67private static final String systemDocumentationToolName = "jdk.javadoc.internal.api.JavadocTool";6869/**70* Returns the Java programming language documentation tool provided71* with this platform.72* <p>The file manager returned by calling73* {@link DocumentationTool#getStandardFileManager getStandardFileManager}74* on this tool supports paths provided by any75* {@linkplain java.nio.file.FileSystem filesystem}.</p>76* @return the documentation tool provided with this platform or77* {@code null} if no documentation tool is provided78* @implNote This implementation returns the tool provided79* by the {@code jdk.javadoc} module if that module is available,80* and {@code null} otherwise.81*/82public static DocumentationTool getSystemDocumentationTool() {83return getSystemTool(DocumentationTool.class,84systemDocumentationToolModule, systemDocumentationToolName);85}8687/**88* Returns a class loader that may be used to load system tools,89* or {@code null} if no such special loader is provided.90* @implSpec This implementation always returns {@code null}.91* @deprecated This method is subject to removal in a future version of92* Java SE.93* Use the {@link java.util.spi.ToolProvider system tool provider} or94* {@link java.util.ServiceLoader service loader} mechanisms to95* locate system tools as well as user-installed tools.96* @return a class loader, or {@code null}97*/98@Deprecated(since="9")99public static ClassLoader getSystemToolClassLoader() {100return null;101}102103/**104* Get an instance of a system tool using the service loader.105* @implNote By default, this returns the implementation in the specified module.106* For limited backward compatibility, if this code is run on an older version107* of the Java platform that does not support modules, this method will108* try and create an instance of the named class. Note that implies the109* class must be available on the system class path.110* @param <T> the interface of the tool111* @param clazz the interface of the tool112* @param moduleName the name of the module containing the desired implementation113* @param className the class name of the desired implementation114* @return the specified implementation of the tool115*/116private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) {117118try {119ServiceLoader<T> sl = ServiceLoader.load(clazz, ClassLoader.getSystemClassLoader());120for (T tool : sl) {121if (matches(tool, moduleName))122return tool;123}124} catch (ServiceConfigurationError e) {125throw new Error(e);126}127return null;128}129130/**131* Determine if this is the desired tool instance.132* @param <T> the interface of the tool133* @param tool the instance of the tool134* @param moduleName the name of the module containing the desired implementation135* @return true if and only if the tool matches the specified criteria136*/137@SuppressWarnings("removal")138private static <T> boolean matches(T tool, String moduleName) {139PrivilegedAction<Boolean> pa = () -> {140Module toolModule = tool.getClass().getModule();141String toolModuleName = toolModule.getName();142return Objects.equals(toolModuleName, moduleName);143};144return AccessController.doPrivileged(pa);145}146}147148149