KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > tools > ToolProvider


1 /*
2  * @(#)ToolProvider.java 1.11 06/09/25
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.tools;
9
10 import java.io.File JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.net.URLClassLoader JavaDoc;
13 import java.net.MalformedURLException JavaDoc;
14
15 /**
16  * Provides methods for locating tool providers, for example,
17  * providers of compilers. This class complements the
18  * functionality of {@link java.util.ServiceLoader}.
19  *
20  * @author Peter von der Ahé
21  * @since 1.6
22  */

23 public class ToolProvider {
24
25     private ToolProvider() {}
26
27     /**
28      * Gets the Java™ programming language compiler provided with this platform.
29      * @return the compiler provided with this platform or {@code
30      * null} if no compiler is provided
31      */

32     public static JavaCompiler getSystemJavaCompiler() {
33         if (Lazy.compilerClass == null)
34             return null;
35         try {
36             return Lazy.compilerClass.newInstance();
37         } catch (Throwable JavaDoc e) {
38             return null;
39         }
40     }
41
42     /**
43      * Returns the class loader for tools provided with this platform.
44      * This does not include user-installed tools. Use the
45      * {@linkplain java.util.ServiceLoader service provider mechanism}
46      * for locating user installed tools.
47      *
48      * @return the class loader for tools provided with this platform
49      * or {@code null} if no tools are provided
50      */

51     public static ClassLoader JavaDoc getSystemToolClassLoader() {
52         if (Lazy.compilerClass == null)
53             return null;
54         return Lazy.compilerClass.getClassLoader();
55     }
56
57     /**
58      * This class will not be initialized until one of the above
59      * methods are called. This ensures that searching for the
60      * compiler does not affect platform start up.
61      */

62     static class Lazy {
63         private static final String JavaDoc defaultJavaCompilerName
64             = "com.sun.tools.javac.api.JavacTool";
65         private static final String JavaDoc[] defaultToolsLocation
66             = { "lib", "tools.jar" };
67         static final Class JavaDoc<? extends JavaCompiler> compilerClass;
68         static {
69             Class JavaDoc<? extends JavaCompiler> c = null;
70             try {
71                 c = findClass().asSubclass(JavaCompiler.class);
72             } catch (Throwable JavaDoc t) {
73                 // ignored
74
}
75             compilerClass = c;
76         }
77
78         private static Class JavaDoc<?> findClass()
79             throws MalformedURLException JavaDoc, ClassNotFoundException JavaDoc
80         {
81             try {
82                 return enableAsserts(Class.forName(defaultJavaCompilerName, false, null));
83             } catch (ClassNotFoundException JavaDoc e) {
84                 // ignored, try looking else where
85
}
86             File JavaDoc file = new File JavaDoc(System.getProperty("java.home"));
87             if (file.getName().equalsIgnoreCase("jre"))
88                 file = file.getParentFile();
89             for (String JavaDoc name : defaultToolsLocation)
90                 file = new File JavaDoc(file, name);
91             URL JavaDoc[] urls = {file.toURI().toURL()};
92             ClassLoader JavaDoc cl = URLClassLoader.newInstance(urls);
93             cl.setPackageAssertionStatus("com.sun.tools.javac", true);
94             return Class.forName(defaultJavaCompilerName, false, cl);
95         }
96
97         private static Class JavaDoc<?> enableAsserts(Class JavaDoc<?> cls) {
98             try {
99                 ClassLoader JavaDoc loader = cls.getClassLoader();
100                 if (loader != null)
101                     loader.setPackageAssertionStatus("com.sun.tools.javac", true);
102             } catch (SecurityException JavaDoc ex) {
103                 // ignored
104
}
105             return cls;
106         }
107     }
108 }
109
Popular Tags