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