src/share/classes/javax/tools/ToolProvider.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1413
bdcef2ef52d2
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package javax.tools;
aoqi@0 27
aoqi@0 28 import java.io.File;
aoqi@0 29 import java.lang.ref.Reference;
aoqi@0 30 import java.lang.ref.WeakReference;
aoqi@0 31 import java.net.URL;
aoqi@0 32 import java.net.URLClassLoader;
aoqi@0 33 import java.net.MalformedURLException;
aoqi@0 34 import java.util.HashMap;
aoqi@0 35 import java.util.Locale;
aoqi@0 36 import java.util.Map;
aoqi@0 37 import java.util.logging.Logger;
aoqi@0 38 import java.util.logging.Level;
aoqi@0 39 import static java.util.logging.Level.*;
aoqi@0 40
aoqi@0 41 /**
aoqi@0 42 * Provides methods for locating tool providers, for example,
aoqi@0 43 * providers of compilers. This class complements the
aoqi@0 44 * functionality of {@link java.util.ServiceLoader}.
aoqi@0 45 *
aoqi@0 46 * @author Peter von der Ahé
aoqi@0 47 * @since 1.6
aoqi@0 48 */
aoqi@0 49 public class ToolProvider {
aoqi@0 50
aoqi@0 51 private static final String propertyName = "sun.tools.ToolProvider";
aoqi@0 52 private static final String loggerName = "javax.tools";
aoqi@0 53
aoqi@0 54 /*
aoqi@0 55 * Define the system property "sun.tools.ToolProvider" to enable
aoqi@0 56 * debugging:
aoqi@0 57 *
aoqi@0 58 * java ... -Dsun.tools.ToolProvider ...
aoqi@0 59 */
aoqi@0 60 static <T> T trace(Level level, Object reason) {
aoqi@0 61 // NOTE: do not make this method private as it affects stack traces
aoqi@0 62 try {
aoqi@0 63 if (System.getProperty(propertyName) != null) {
aoqi@0 64 StackTraceElement[] st = Thread.currentThread().getStackTrace();
aoqi@0 65 String method = "???";
aoqi@0 66 String cls = ToolProvider.class.getName();
aoqi@0 67 if (st.length > 2) {
aoqi@0 68 StackTraceElement frame = st[2];
aoqi@0 69 method = String.format((Locale)null, "%s(%s:%s)",
aoqi@0 70 frame.getMethodName(),
aoqi@0 71 frame.getFileName(),
aoqi@0 72 frame.getLineNumber());
aoqi@0 73 cls = frame.getClassName();
aoqi@0 74 }
aoqi@0 75 Logger logger = Logger.getLogger(loggerName);
aoqi@0 76 if (reason instanceof Throwable) {
aoqi@0 77 logger.logp(level, cls, method,
aoqi@0 78 reason.getClass().getName(), (Throwable)reason);
aoqi@0 79 } else {
aoqi@0 80 logger.logp(level, cls, method, String.valueOf(reason));
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83 } catch (SecurityException ex) {
aoqi@0 84 System.err.format((Locale)null, "%s: %s; %s%n",
aoqi@0 85 ToolProvider.class.getName(),
aoqi@0 86 reason,
aoqi@0 87 ex.getLocalizedMessage());
aoqi@0 88 }
aoqi@0 89 return null;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 private static final String defaultJavaCompilerName
aoqi@0 93 = "com.sun.tools.javac.api.JavacTool";
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Gets the Java&trade; programming language compiler provided
aoqi@0 97 * with this platform.
aoqi@0 98 * @return the compiler provided with this platform or
aoqi@0 99 * {@code null} if no compiler is provided
aoqi@0 100 */
aoqi@0 101 public static JavaCompiler getSystemJavaCompiler() {
aoqi@0 102 return instance().getSystemTool(JavaCompiler.class, defaultJavaCompilerName);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 private static final String defaultDocumentationToolName
aoqi@0 106 = "com.sun.tools.javadoc.api.JavadocTool";
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * Gets the Java&trade; programming language documentation tool provided
aoqi@0 110 * with this platform.
aoqi@0 111 * @return the documentation tool provided with this platform or
aoqi@0 112 * {@code null} if no documentation tool is provided
aoqi@0 113 */
aoqi@0 114 public static DocumentationTool getSystemDocumentationTool() {
aoqi@0 115 return instance().getSystemTool(DocumentationTool.class, defaultDocumentationToolName);
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Returns the class loader for tools provided with this platform.
aoqi@0 120 * This does not include user-installed tools. Use the
aoqi@0 121 * {@linkplain java.util.ServiceLoader service provider mechanism}
aoqi@0 122 * for locating user installed tools.
aoqi@0 123 *
aoqi@0 124 * @return the class loader for tools provided with this platform
aoqi@0 125 * or {@code null} if no tools are provided
aoqi@0 126 */
aoqi@0 127 public static ClassLoader getSystemToolClassLoader() {
aoqi@0 128 try {
aoqi@0 129 Class<? extends JavaCompiler> c =
aoqi@0 130 instance().getSystemToolClass(JavaCompiler.class, defaultJavaCompilerName);
aoqi@0 131 return c.getClassLoader();
aoqi@0 132 } catch (Throwable e) {
aoqi@0 133 return trace(WARNING, e);
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137
aoqi@0 138 private static ToolProvider instance;
aoqi@0 139
aoqi@0 140 private static synchronized ToolProvider instance() {
aoqi@0 141 if (instance == null)
aoqi@0 142 instance = new ToolProvider();
aoqi@0 143 return instance;
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 // Cache for tool classes.
aoqi@0 147 // Use weak references to avoid keeping classes around unnecessarily
aoqi@0 148 private Map<String, Reference<Class<?>>> toolClasses = new HashMap<String, Reference<Class<?>>>();
aoqi@0 149
aoqi@0 150 // Cache for tool classloader.
aoqi@0 151 // Use a weak reference to avoid keeping it around unnecessarily
aoqi@0 152 private Reference<ClassLoader> refToolClassLoader = null;
aoqi@0 153
aoqi@0 154
aoqi@0 155 private ToolProvider() { }
aoqi@0 156
aoqi@0 157 private <T> T getSystemTool(Class<T> clazz, String name) {
aoqi@0 158 Class<? extends T> c = getSystemToolClass(clazz, name);
aoqi@0 159 try {
aoqi@0 160 return c.asSubclass(clazz).newInstance();
aoqi@0 161 } catch (Throwable e) {
aoqi@0 162 trace(WARNING, e);
aoqi@0 163 return null;
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 private <T> Class<? extends T> getSystemToolClass(Class<T> clazz, String name) {
aoqi@0 168 Reference<Class<?>> refClass = toolClasses.get(name);
aoqi@0 169 Class<?> c = (refClass == null ? null : refClass.get());
aoqi@0 170 if (c == null) {
aoqi@0 171 try {
aoqi@0 172 c = findSystemToolClass(name);
aoqi@0 173 } catch (Throwable e) {
aoqi@0 174 return trace(WARNING, e);
aoqi@0 175 }
aoqi@0 176 toolClasses.put(name, new WeakReference<Class<?>>(c));
aoqi@0 177 }
aoqi@0 178 return c.asSubclass(clazz);
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 private static final String[] defaultToolsLocation = { "lib", "tools.jar" };
aoqi@0 182
aoqi@0 183 private Class<?> findSystemToolClass(String toolClassName)
aoqi@0 184 throws MalformedURLException, ClassNotFoundException
aoqi@0 185 {
aoqi@0 186 // try loading class directly, in case tool is on the bootclasspath
aoqi@0 187 try {
aoqi@0 188 return Class.forName(toolClassName, false, null);
aoqi@0 189 } catch (ClassNotFoundException e) {
aoqi@0 190 trace(FINE, e);
aoqi@0 191
aoqi@0 192 // if tool not on bootclasspath, look in default tools location (tools.jar)
aoqi@0 193 ClassLoader cl = (refToolClassLoader == null ? null : refToolClassLoader.get());
aoqi@0 194 if (cl == null) {
aoqi@0 195 File file = new File(System.getProperty("java.home"));
aoqi@0 196 if (file.getName().equalsIgnoreCase("jre"))
aoqi@0 197 file = file.getParentFile();
aoqi@0 198 for (String name : defaultToolsLocation)
aoqi@0 199 file = new File(file, name);
aoqi@0 200
aoqi@0 201 // if tools not found, no point in trying a URLClassLoader
aoqi@0 202 // so rethrow the original exception.
aoqi@0 203 if (!file.exists())
aoqi@0 204 throw e;
aoqi@0 205
aoqi@0 206 URL[] urls = { file.toURI().toURL() };
aoqi@0 207 trace(FINE, urls[0].toString());
aoqi@0 208
aoqi@0 209 cl = URLClassLoader.newInstance(urls);
aoqi@0 210 refToolClassLoader = new WeakReference<ClassLoader>(cl);
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 return Class.forName(toolClassName, false, cl);
aoqi@0 214 }
aoqi@0 215 }
aoqi@0 216 }

mercurial