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

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/javax/tools/ToolProvider.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,175 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package javax.tools;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.net.URL;
    1.33 +import java.net.URLClassLoader;
    1.34 +import java.net.MalformedURLException;
    1.35 +import java.util.Locale;
    1.36 +import java.util.logging.Logger;
    1.37 +import java.util.logging.Level;
    1.38 +import static java.util.logging.Level.*;
    1.39 +
    1.40 +/**
    1.41 + * Provides methods for locating tool providers, for example,
    1.42 + * providers of compilers.  This class complements the
    1.43 + * functionality of {@link java.util.ServiceLoader}.
    1.44 + *
    1.45 + * @author Peter von der Ahé
    1.46 + * @since 1.6
    1.47 + */
    1.48 +public class ToolProvider {
    1.49 +
    1.50 +    private ToolProvider() {}
    1.51 +
    1.52 +    private static final String propertyName = "sun.tools.ToolProvider";
    1.53 +    private static final String loggerName   = "javax.tools";
    1.54 +
    1.55 +    /*
    1.56 +     * Define the system property "sun.tools.ToolProvider" to enable
    1.57 +     * debugging:
    1.58 +     *
    1.59 +     *     java ... -Dsun.tools.ToolProvider ...
    1.60 +     */
    1.61 +    static <T> T trace(Level level, Object reason) {
    1.62 +        // NOTE: do not make this method private as it affects stack traces
    1.63 +        try {
    1.64 +            if (System.getProperty(propertyName) != null) {
    1.65 +                StackTraceElement[] st = Thread.currentThread().getStackTrace();
    1.66 +                String method = "???";
    1.67 +                String cls = ToolProvider.class.getName();
    1.68 +                if (st.length > 2) {
    1.69 +                    StackTraceElement frame = st[2];
    1.70 +                    method = String.format((Locale)null, "%s(%s:%s)",
    1.71 +                                           frame.getMethodName(),
    1.72 +                                           frame.getFileName(),
    1.73 +                                           frame.getLineNumber());
    1.74 +                    cls = frame.getClassName();
    1.75 +                }
    1.76 +                Logger logger = Logger.getLogger(loggerName);
    1.77 +                if (reason instanceof Throwable) {
    1.78 +                    logger.logp(level, cls, method,
    1.79 +                                reason.getClass().getName(), (Throwable)reason);
    1.80 +                } else {
    1.81 +                    logger.logp(level, cls, method, String.valueOf(reason));
    1.82 +                }
    1.83 +            }
    1.84 +        } catch (SecurityException ex) {
    1.85 +            System.err.format((Locale)null, "%s: %s; %s%n",
    1.86 +                              ToolProvider.class.getName(),
    1.87 +                              reason,
    1.88 +                              ex.getLocalizedMessage());
    1.89 +        }
    1.90 +        return null;
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Gets the Java&trade; programming language compiler provided
    1.95 +     * with this platform.
    1.96 +     * @return the compiler provided with this platform or
    1.97 +     * {@code null} if no compiler is provided
    1.98 +     */
    1.99 +    public static JavaCompiler getSystemJavaCompiler() {
   1.100 +        if (Lazy.compilerClass == null)
   1.101 +            return trace(WARNING, "Lazy.compilerClass == null");
   1.102 +        try {
   1.103 +            return Lazy.compilerClass.newInstance();
   1.104 +        } catch (Throwable e) {
   1.105 +            return trace(WARNING, e);
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Returns the class loader for tools provided with this platform.
   1.111 +     * This does not include user-installed tools.  Use the
   1.112 +     * {@linkplain java.util.ServiceLoader service provider mechanism}
   1.113 +     * for locating user installed tools.
   1.114 +     *
   1.115 +     * @return the class loader for tools provided with this platform
   1.116 +     * or {@code null} if no tools are provided
   1.117 +     */
   1.118 +    public static ClassLoader getSystemToolClassLoader() {
   1.119 +        if (Lazy.compilerClass == null)
   1.120 +            return trace(WARNING, "Lazy.compilerClass == null");
   1.121 +        return Lazy.compilerClass.getClassLoader();
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * This class will not be initialized until one of the above
   1.126 +     * methods are called.  This ensures that searching for the
   1.127 +     * compiler does not affect platform start up.
   1.128 +     */
   1.129 +    static class Lazy  {
   1.130 +        private static final String defaultJavaCompilerName
   1.131 +            = "com.sun.tools.javac.api.JavacTool";
   1.132 +        private static final String[] defaultToolsLocation
   1.133 +            = { "lib", "tools.jar" };
   1.134 +        static final Class<? extends JavaCompiler> compilerClass;
   1.135 +        static {
   1.136 +            Class<? extends JavaCompiler> c = null;
   1.137 +            try {
   1.138 +                c = findClass().asSubclass(JavaCompiler.class);
   1.139 +            } catch (Throwable t) {
   1.140 +                trace(WARNING, t);
   1.141 +            }
   1.142 +            compilerClass = c;
   1.143 +        }
   1.144 +
   1.145 +        private static Class<?> findClass()
   1.146 +            throws MalformedURLException, ClassNotFoundException
   1.147 +        {
   1.148 +            try {
   1.149 +                return enableAsserts(Class.forName(defaultJavaCompilerName, false, null));
   1.150 +            } catch (ClassNotFoundException e) {
   1.151 +                trace(FINE, e);
   1.152 +            }
   1.153 +            File file = new File(System.getProperty("java.home"));
   1.154 +            if (file.getName().equalsIgnoreCase("jre"))
   1.155 +                file = file.getParentFile();
   1.156 +            for (String name : defaultToolsLocation)
   1.157 +                file = new File(file, name);
   1.158 +            URL[] urls = {file.toURI().toURL()};
   1.159 +            trace(FINE, urls[0].toString());
   1.160 +            ClassLoader cl = URLClassLoader.newInstance(urls);
   1.161 +            cl.setPackageAssertionStatus("com.sun.tools.javac", true);
   1.162 +            return Class.forName(defaultJavaCompilerName, false, cl);
   1.163 +        }
   1.164 +
   1.165 +        private static Class<?> enableAsserts(Class<?> cls) {
   1.166 +            try {
   1.167 +                ClassLoader loader = cls.getClassLoader();
   1.168 +                if (loader != null)
   1.169 +                    loader.setPackageAssertionStatus("com.sun.tools.javac", true);
   1.170 +                else
   1.171 +                    trace(FINE, "loader == null");
   1.172 +            } catch (SecurityException ex) {
   1.173 +                trace(FINE, ex);
   1.174 +            }
   1.175 +            return cls;
   1.176 +        }
   1.177 +    }
   1.178 +}

mercurial