--- a/langtools/src/share/classes/javax/tools/ToolProvider.java Wed Aug 25 15:31:46 2010 -0700
+++ b/langtools/src/share/classes/javax/tools/ToolProvider.java Thu Aug 26 15:17:17 2010 -0700
@@ -26,10 +26,14 @@
package javax.tools;
import java.io.File;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
import java.util.logging.Logger;
import java.util.logging.Level;
import static java.util.logging.Level.*;
@@ -44,8 +48,6 @@
*/
public class ToolProvider {
- private ToolProvider() {}
-
private static final String propertyName = "sun.tools.ToolProvider";
private static final String loggerName = "javax.tools";
@@ -87,6 +89,9 @@
return null;
}
+ private static final String defaultJavaCompilerName
+ = "com.sun.tools.javac.api.JavacTool";
+
/**
* Gets the Java™ programming language compiler provided
* with this platform.
@@ -94,13 +99,7 @@
* {@code null} if no compiler is provided
*/
public static JavaCompiler getSystemJavaCompiler() {
- if (Lazy.compilerClass == null)
- return trace(WARNING, "Lazy.compilerClass == null");
- try {
- return Lazy.compilerClass.newInstance();
- } catch (Throwable e) {
- return trace(WARNING, e);
- }
+ return instance().getSystemTool(JavaCompiler.class, defaultJavaCompilerName);
}
/**
@@ -113,63 +112,109 @@
* or {@code null} if no tools are provided
*/
public static ClassLoader getSystemToolClassLoader() {
- if (Lazy.compilerClass == null)
- return trace(WARNING, "Lazy.compilerClass == null");
- return Lazy.compilerClass.getClassLoader();
+ try {
+ Class<? extends JavaCompiler> c =
+ instance().getSystemToolClass(JavaCompiler.class, defaultJavaCompilerName);
+ return c.getClassLoader();
+ } catch (Throwable e) {
+ return trace(WARNING, e);
+ }
+ }
+
+
+ private static ToolProvider instance;
+
+ private static synchronized ToolProvider instance() {
+ if (instance == null)
+ instance = new ToolProvider();
+ return instance;
+ }
+
+ // Cache for tool classes.
+ // Use weak references to avoid keeping classes around unnecessarily
+ private Map<String, Reference<Class<?>>> toolClasses = new HashMap<String, Reference<Class<?>>>();
+
+ // Cache for tool classloader.
+ // Use a weak reference to avoid keeping it around unnecessarily
+ private Reference<ClassLoader> refToolClassLoader = null;
+
+
+ private ToolProvider() { }
+
+ private <T> T getSystemTool(Class<T> clazz, String name) {
+ Class<? extends T> c = getSystemToolClass(clazz, name);
+ try {
+ return c.asSubclass(clazz).newInstance();
+ } catch (Throwable e) {
+ trace(WARNING, e);
+ return null;
+ }
}
- /**
- * This class will not be initialized until one of the above
- * methods are called. This ensures that searching for the
- * compiler does not affect platform start up.
- */
- static class Lazy {
- private static final String defaultJavaCompilerName
- = "com.sun.tools.javac.api.JavacTool";
- private static final String[] defaultToolsLocation
- = { "lib", "tools.jar" };
- static final Class<? extends JavaCompiler> compilerClass;
- static {
- Class<? extends JavaCompiler> c = null;
+ private <T> Class<? extends T> getSystemToolClass(Class<T> clazz, String name) {
+ Reference<Class<?>> refClass = toolClasses.get(name);
+ Class<?> c = (refClass == null ? null : refClass.get());
+ if (c == null) {
try {
- c = findClass().asSubclass(JavaCompiler.class);
- } catch (Throwable t) {
- trace(WARNING, t);
+ c = findSystemToolClass(name);
+ } catch (Throwable e) {
+ return trace(WARNING, e);
}
- compilerClass = c;
+ toolClasses.put(name, new WeakReference<Class<?>>(c));
+ }
+ return c.asSubclass(clazz);
+ }
+
+ private static final String[] defaultToolsLocation = { "lib", "tools.jar" };
+
+ private Class<?> findSystemToolClass(String toolClassName)
+ throws MalformedURLException, ClassNotFoundException
+ {
+ // try loading class directly, in case tool is on the bootclasspath
+ try {
+ return enableAsserts(Class.forName(toolClassName, false, null));
+ } catch (ClassNotFoundException e) {
+ trace(FINE, e);
+
+ // if tool not on bootclasspath, look in default tools location (tools.jar)
+ ClassLoader cl = (refToolClassLoader == null ? null : refToolClassLoader.get());
+ if (cl == null) {
+ File file = new File(System.getProperty("java.home"));
+ if (file.getName().equalsIgnoreCase("jre"))
+ file = file.getParentFile();
+ for (String name : defaultToolsLocation)
+ file = new File(file, name);
+
+ // if tools not found, no point in trying a URLClassLoader
+ // so rethrow the original exception.
+ if (!file.exists())
+ throw e;
+
+ URL[] urls = { file.toURI().toURL() };
+ trace(FINE, urls[0].toString());
+
+ cl = URLClassLoader.newInstance(urls);
+ cl.setPackageAssertionStatus("com.sun.tools.javac", true);
+ refToolClassLoader = new WeakReference<ClassLoader>(cl);
+ }
+
+ return Class.forName(toolClassName, false, cl);
}
- private static Class<?> findClass()
- throws MalformedURLException, ClassNotFoundException
- {
- try {
- return enableAsserts(Class.forName(defaultJavaCompilerName, false, null));
- } catch (ClassNotFoundException e) {
- trace(FINE, e);
- }
- File file = new File(System.getProperty("java.home"));
- if (file.getName().equalsIgnoreCase("jre"))
- file = file.getParentFile();
- for (String name : defaultToolsLocation)
- file = new File(file, name);
- URL[] urls = {file.toURI().toURL()};
- trace(FINE, urls[0].toString());
- ClassLoader cl = URLClassLoader.newInstance(urls);
- cl.setPackageAssertionStatus("com.sun.tools.javac", true);
- return Class.forName(defaultJavaCompilerName, false, cl);
+ }
+
+ private static Class<?> enableAsserts(Class<?> cls) {
+ try {
+ ClassLoader loader = cls.getClassLoader();
+ if (loader != null)
+ loader.setPackageAssertionStatus("com.sun.tools.javac", true);
+ else
+ trace(FINE, "loader == null");
+ } catch (SecurityException ex) {
+ trace(FINE, ex);
}
+ return cls;
+ }
- private static Class<?> enableAsserts(Class<?> cls) {
- try {
- ClassLoader loader = cls.getClassLoader();
- if (loader != null)
- loader.setPackageAssertionStatus("com.sun.tools.javac", true);
- else
- trace(FINE, "loader == null");
- } catch (SecurityException ex) {
- trace(FINE, ex);
- }
- return cls;
- }
- }
+
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/ToolProvider/HelloWorldTest.java Thu Aug 26 15:17:17 2010 -0700
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6604599
+ * @summary ToolProvider should be less compiler-specific
+ */
+
+import java.io.*;
+import java.util.*;
+
+// verify that running a simple program, such as this one, does not trigger
+// the loading of ToolProvider or any com.sun.tools.javac class
+public class HelloWorldTest {
+ public static void main(String... args) throws Exception {
+ if (args.length > 0) {
+ System.err.println(Arrays.asList(args));
+ return;
+ }
+
+ new HelloWorldTest().run();
+ }
+
+ void run() throws Exception {
+ File javaHome = new File(System.getProperty("java.home"));
+ if (javaHome.getName().equals("jre"))
+ javaHome = javaHome.getParentFile();
+ File javaExe = new File(new File(javaHome, "bin"), "java");
+ String classpath = System.getProperty("java.class.path");
+
+ String[] cmd = {
+ javaExe.getPath(),
+ "-verbose:class",
+ "-classpath", classpath,
+ HelloWorldTest.class.getName(),
+ "Hello", "World"
+ };
+
+ ProcessBuilder pb = new ProcessBuilder(cmd).redirectErrorStream(true);
+ Process p = pb.start();
+ BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line;
+ while ((line = r.readLine()) != null) {
+ System.err.println(line);
+ if (line.contains("javax.tools.ToolProvider") || line.contains("com.sun.tools.javac."))
+ error(">>> " + line);
+ }
+ int rc = p.waitFor();
+ if (rc != 0)
+ error("Unexpected exit code: " + rc);
+
+ if (errors > 0)
+ throw new Exception(errors + " errors occurred");
+ }
+
+ void error(String msg) {
+ System.err.println(msg);
+ errors++;
+ }
+
+ int errors;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/ToolProvider/ToolProviderTest1.java Thu Aug 26 15:17:17 2010 -0700
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6604599
+ * @summary ToolProvider should be less compiler-specific
+ */
+
+import java.io.*;
+
+// verify that running accessing ToolProvider by itself does not
+// trigger loading com.sun.tools.javac.*
+public class ToolProviderTest1 {
+ public static void main(String... args) throws Exception {
+ if (args.length > 0) {
+ System.err.println(Class.forName(args[0], true, null));
+ return;
+ }
+
+ new ToolProviderTest1().run();
+ }
+
+ void run() throws Exception {
+ File javaHome = new File(System.getProperty("java.home"));
+ if (javaHome.getName().equals("jre"))
+ javaHome = javaHome.getParentFile();
+ File javaExe = new File(new File(javaHome, "bin"), "java");
+ String classpath = System.getProperty("java.class.path");
+
+ String[] cmd = {
+ javaExe.getPath(),
+ "-verbose:class",
+ "-classpath", classpath,
+ ToolProviderTest1.class.getName(),
+ "javax.tools.ToolProvider"
+ };
+
+ ProcessBuilder pb = new ProcessBuilder(cmd).redirectErrorStream(true);
+ Process p = pb.start();
+ BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line;
+ while ((line = r.readLine()) != null) {
+ System.err.println(line);
+ if (line.contains("com.sun.tools.javac."))
+ error(">>> " + line);
+ }
+ int rc = p.waitFor();
+ if (rc != 0)
+ error("Unexpected exit code: " + rc);
+
+ if (errors > 0)
+ throw new Exception(errors + " errors occurred");
+ }
+
+ void error(String msg) {
+ System.err.println(msg);
+ errors++;
+ }
+
+ int errors;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/ToolProvider/ToolProviderTest2.java Thu Aug 26 15:17:17 2010 -0700
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6604599
+ * @summary ToolProvider should be less compiler-specific
+ */
+
+import java.io.*;
+import javax.tools.*;
+
+// control for ToolProviderTest1 -- verify that using ToolProvider to
+// access the compiler does trigger loading com.sun.tools.javac.*
+public class ToolProviderTest2 {
+ public static void main(String... args) throws Exception {
+ if (args.length > 0) {
+ System.err.println(ToolProvider.getSystemJavaCompiler());
+ return;
+ }
+
+ new ToolProviderTest2().run();
+ }
+
+ void run() throws Exception {
+ File javaHome = new File(System.getProperty("java.home"));
+ if (javaHome.getName().equals("jre"))
+ javaHome = javaHome.getParentFile();
+ File javaExe = new File(new File(javaHome, "bin"), "java");
+ String classpath = System.getProperty("java.class.path");
+
+ String[] cmd = {
+ javaExe.getPath(),
+ "-verbose:class",
+ "-classpath", classpath,
+ ToolProviderTest2.class.getName(),
+ "javax.tools.ToolProvider"
+ };
+
+ ProcessBuilder pb = new ProcessBuilder(cmd).redirectErrorStream(true);
+ Process p = pb.start();
+ BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ String line;
+ boolean found = false;
+ while ((line = r.readLine()) != null) {
+ System.err.println(line);
+ if (line.contains("com.sun.tools.javac."))
+ found = true;
+ }
+ int rc = p.waitFor();
+ if (rc != 0)
+ error("Unexpected exit code: " + rc);
+
+ if (!found)
+ System.err.println("expected class name not found");
+
+ if (errors > 0)
+ throw new Exception(errors + " errors occurred");
+ }
+
+ void error(String msg) {
+ System.err.println(msg);
+ errors++;
+ }
+
+ int errors;
+}