author | yan |
Mon, 10 Nov 2014 16:23:30 +0300 | |
changeset 27763 | 1fba965925e8 |
parent 25874 | 83c19f00452c |
child 27579 | d1a63c99cdd5 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
25287 | 2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package javax.tools; |
|
27 |
||
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 | 31 |
import java.net.URL; |
32 |
import java.net.URLClassLoader; |
|
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 | 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 | 37 |
import java.util.logging.Logger; |
38 |
import java.util.logging.Level; |
|
39 |
import static java.util.logging.Level.*; |
|
40 |
||
41 |
/** |
|
42 |
* Provides methods for locating tool providers, for example, |
|
43 |
* providers of compilers. This class complements the |
|
44 |
* functionality of {@link java.util.ServiceLoader}. |
|
45 |
* |
|
46 |
* @author Peter von der Ahé |
|
47 |
* @since 1.6 |
|
48 |
*/ |
|
49 |
public class ToolProvider { |
|
50 |
||
51 |
private static final String propertyName = "sun.tools.ToolProvider"; |
|
52 |
private static final String loggerName = "javax.tools"; |
|
53 |
||
54 |
/* |
|
55 |
* Define the system property "sun.tools.ToolProvider" to enable |
|
56 |
* debugging: |
|
57 |
* |
|
58 |
* java ... -Dsun.tools.ToolProvider ... |
|
59 |
*/ |
|
60 |
static <T> T trace(Level level, Object reason) { |
|
61 |
// NOTE: do not make this method private as it affects stack traces |
|
62 |
try { |
|
63 |
if (System.getProperty(propertyName) != null) { |
|
64 |
StackTraceElement[] st = Thread.currentThread().getStackTrace(); |
|
65 |
String method = "???"; |
|
66 |
String cls = ToolProvider.class.getName(); |
|
67 |
if (st.length > 2) { |
|
68 |
StackTraceElement frame = st[2]; |
|
69 |
method = String.format((Locale)null, "%s(%s:%s)", |
|
70 |
frame.getMethodName(), |
|
71 |
frame.getFileName(), |
|
72 |
frame.getLineNumber()); |
|
73 |
cls = frame.getClassName(); |
|
74 |
} |
|
75 |
Logger logger = Logger.getLogger(loggerName); |
|
76 |
if (reason instanceof Throwable) { |
|
77 |
logger.logp(level, cls, method, |
|
78 |
reason.getClass().getName(), (Throwable)reason); |
|
79 |
} else { |
|
80 |
logger.logp(level, cls, method, String.valueOf(reason)); |
|
81 |
} |
|
82 |
} |
|
83 |
} catch (SecurityException ex) { |
|
84 |
System.err.format((Locale)null, "%s: %s; %s%n", |
|
85 |
ToolProvider.class.getName(), |
|
86 |
reason, |
|
87 |
ex.getLocalizedMessage()); |
|
88 |
} |
|
89 |
return null; |
|
90 |
} |
|
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 | 95 |
/** |
25287 | 96 |
* Returns the Java™ programming language compiler provided |
10 | 97 |
* with this platform. |
98 |
* @return the compiler provided with this platform or |
|
99 |
* {@code null} if no compiler is provided |
|
100 |
*/ |
|
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 | 103 |
} |
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 | 109 |
* Returns the Java™ 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 | 118 |
/** |
119 |
* Returns the class loader for tools provided with this platform. |
|
120 |
* This does not include user-installed tools. Use the |
|
121 |
* {@linkplain java.util.ServiceLoader service provider mechanism} |
|
122 |
* for locating user installed tools. |
|
123 |
* |
|
124 |
* @return the class loader for tools provided with this platform |
|
125 |
* or {@code null} if no tools are provided |
|
126 |
*/ |
|
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 | 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 | 165 |
} |
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 | 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 | 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 | 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 | 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 | 214 |
} |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
215 |
} |
10 | 216 |
} |