langtools/src/share/classes/javax/tools/ToolProvider.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.net.URLClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Locale;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.logging.Logger;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.logging.Level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import static java.util.logging.Level.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * Provides methods for locating tool providers, for example,
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * providers of compilers.  This class complements the
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * functionality of {@link java.util.ServiceLoader}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Peter von der Ahé
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @since 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class ToolProvider {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private ToolProvider() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private static final String propertyName = "sun.tools.ToolProvider";
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private static final String loggerName   = "javax.tools";
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * Define the system property "sun.tools.ToolProvider" to enable
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * debugging:
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     *     java ... -Dsun.tools.ToolProvider ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    static <T> T trace(Level level, Object reason) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        // NOTE: do not make this method private as it affects stack traces
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            if (System.getProperty(propertyName) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                StackTraceElement[] st = Thread.currentThread().getStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                String method = "???";
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
                String cls = ToolProvider.class.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
                if (st.length > 2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
                    StackTraceElement frame = st[2];
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
                    method = String.format((Locale)null, "%s(%s:%s)",
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                                           frame.getMethodName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                                           frame.getFileName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                                           frame.getLineNumber());
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                    cls = frame.getClassName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                Logger logger = Logger.getLogger(loggerName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
                if (reason instanceof Throwable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                    logger.logp(level, cls, method,
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                                reason.getClass().getName(), (Throwable)reason);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                    logger.logp(level, cls, method, String.valueOf(reason));
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        } catch (SecurityException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            System.err.format((Locale)null, "%s: %s; %s%n",
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                              ToolProvider.class.getName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                              reason,
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                              ex.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * Gets the Java&trade; programming language compiler provided
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * with this platform.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @return the compiler provided with this platform or
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * {@code null} if no compiler is provided
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public static JavaCompiler getSystemJavaCompiler() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        if (Lazy.compilerClass == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            return trace(WARNING, "Lazy.compilerClass == null");
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return Lazy.compilerClass.newInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        } catch (Throwable e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            return trace(WARNING, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Returns the class loader for tools provided with this platform.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * This does not include user-installed tools.  Use the
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * {@linkplain java.util.ServiceLoader service provider mechanism}
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * for locating user installed tools.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * @return the class loader for tools provided with this platform
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     * or {@code null} if no tools are provided
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    public static ClassLoader getSystemToolClassLoader() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        if (Lazy.compilerClass == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            return trace(WARNING, "Lazy.compilerClass == null");
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        return Lazy.compilerClass.getClassLoader();
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * This class will not be initialized until one of the above
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     * methods are called.  This ensures that searching for the
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * compiler does not affect platform start up.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    static class Lazy  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        private static final String defaultJavaCompilerName
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            = "com.sun.tools.javac.api.JavacTool";
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        private static final String[] defaultToolsLocation
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            = { "lib", "tools.jar" };
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        static final Class<? extends JavaCompiler> compilerClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        static {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            Class<? extends JavaCompiler> c = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                c = findClass().asSubclass(JavaCompiler.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                trace(WARNING, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            compilerClass = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        private static Class<?> findClass()
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            throws MalformedURLException, ClassNotFoundException
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                return enableAsserts(Class.forName(defaultJavaCompilerName, false, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            } catch (ClassNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                trace(FINE, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            File file = new File(System.getProperty("java.home"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            if (file.getName().equalsIgnoreCase("jre"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                file = file.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            for (String name : defaultToolsLocation)
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                file = new File(file, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            URL[] urls = {file.toURI().toURL()};
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            trace(FINE, urls[0].toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            ClassLoader cl = URLClassLoader.newInstance(urls);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            cl.setPackageAssertionStatus("com.sun.tools.javac", true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            return Class.forName(defaultJavaCompilerName, false, cl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        private static Class<?> enableAsserts(Class<?> cls) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                ClassLoader loader = cls.getClassLoader();
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                if (loader != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                    loader.setPackageAssertionStatus("com.sun.tools.javac", true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                    trace(FINE, "loader == null");
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            } catch (SecurityException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                trace(FINE, ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            return cls;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
}