author | duke |
Wed, 05 Jul 2017 20:10:37 +0200 | |
changeset 27929 | ebe3a722c703 |
parent 27579 | d1a63c99cdd5 |
child 28332 | cd3ea1087d2b |
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 |
||
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
28 |
import java.lang.ref.Reference; |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
29 |
import java.lang.ref.WeakReference; |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
30 |
import java.util.HashMap; |
10 | 31 |
import java.util.Locale; |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
32 |
import java.util.Map; |
10 | 33 |
import java.util.logging.Logger; |
34 |
import java.util.logging.Level; |
|
27579 | 35 |
|
10 | 36 |
import static java.util.logging.Level.*; |
37 |
||
38 |
/** |
|
39 |
* Provides methods for locating tool providers, for example, |
|
40 |
* providers of compilers. This class complements the |
|
41 |
* functionality of {@link java.util.ServiceLoader}. |
|
42 |
* |
|
43 |
* @author Peter von der Ahé |
|
44 |
* @since 1.6 |
|
45 |
*/ |
|
46 |
public class ToolProvider { |
|
47 |
||
48 |
private static final String propertyName = "sun.tools.ToolProvider"; |
|
49 |
private static final String loggerName = "javax.tools"; |
|
50 |
||
51 |
/* |
|
52 |
* Define the system property "sun.tools.ToolProvider" to enable |
|
53 |
* debugging: |
|
54 |
* |
|
55 |
* java ... -Dsun.tools.ToolProvider ... |
|
56 |
*/ |
|
57 |
static <T> T trace(Level level, Object reason) { |
|
58 |
// NOTE: do not make this method private as it affects stack traces |
|
59 |
try { |
|
60 |
if (System.getProperty(propertyName) != null) { |
|
61 |
StackTraceElement[] st = Thread.currentThread().getStackTrace(); |
|
62 |
String method = "???"; |
|
63 |
String cls = ToolProvider.class.getName(); |
|
64 |
if (st.length > 2) { |
|
65 |
StackTraceElement frame = st[2]; |
|
66 |
method = String.format((Locale)null, "%s(%s:%s)", |
|
67 |
frame.getMethodName(), |
|
68 |
frame.getFileName(), |
|
69 |
frame.getLineNumber()); |
|
70 |
cls = frame.getClassName(); |
|
71 |
} |
|
72 |
Logger logger = Logger.getLogger(loggerName); |
|
73 |
if (reason instanceof Throwable) { |
|
74 |
logger.logp(level, cls, method, |
|
75 |
reason.getClass().getName(), (Throwable)reason); |
|
76 |
} else { |
|
77 |
logger.logp(level, cls, method, String.valueOf(reason)); |
|
78 |
} |
|
79 |
} |
|
80 |
} catch (SecurityException ex) { |
|
81 |
System.err.format((Locale)null, "%s: %s; %s%n", |
|
82 |
ToolProvider.class.getName(), |
|
83 |
reason, |
|
84 |
ex.getLocalizedMessage()); |
|
85 |
} |
|
86 |
return null; |
|
87 |
} |
|
88 |
||
27579 | 89 |
private static final String systemJavaCompilerName |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
90 |
= "com.sun.tools.javac.api.JavacTool"; |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
91 |
|
10 | 92 |
/** |
25287 | 93 |
* Returns the Java™ programming language compiler provided |
10 | 94 |
* with this platform. |
95 |
* @return the compiler provided with this platform or |
|
96 |
* {@code null} if no compiler is provided |
|
97 |
*/ |
|
98 |
public static JavaCompiler getSystemJavaCompiler() { |
|
27579 | 99 |
return instance().getSystemTool(JavaCompiler.class, systemJavaCompilerName); |
10 | 100 |
} |
101 |
||
27579 | 102 |
private static final String systemDocumentationToolName |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
103 |
= "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
|
104 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
105 |
/** |
25287 | 106 |
* 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
|
107 |
* with this platform. |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
108 |
* @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
|
109 |
* {@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
|
110 |
*/ |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
111 |
public static DocumentationTool getSystemDocumentationTool() { |
27579 | 112 |
return instance().getSystemTool(DocumentationTool.class, systemDocumentationToolName); |
14545
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 |
|
10 | 115 |
/** |
116 |
* Returns the class loader for tools provided with this platform. |
|
117 |
* This does not include user-installed tools. Use the |
|
118 |
* {@linkplain java.util.ServiceLoader service provider mechanism} |
|
119 |
* for locating user installed tools. |
|
120 |
* |
|
121 |
* @return the class loader for tools provided with this platform |
|
122 |
* or {@code null} if no tools are provided |
|
123 |
*/ |
|
124 |
public static ClassLoader getSystemToolClassLoader() { |
|
27579 | 125 |
return ClassLoader.getSystemClassLoader(); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
126 |
} |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
127 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
128 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
129 |
private static ToolProvider instance; |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
130 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
131 |
private static synchronized ToolProvider instance() { |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
132 |
if (instance == null) |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
133 |
instance = new ToolProvider(); |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
134 |
return instance; |
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 |
// Cache for tool classes. |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
138 |
// Use weak references to avoid keeping classes around unnecessarily |
27579 | 139 |
private final Map<String, Reference<Class<?>>> toolClasses = new HashMap<>(); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
140 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
141 |
private ToolProvider() { } |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
142 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
143 |
private <T> T getSystemTool(Class<T> clazz, String name) { |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
144 |
Class<? extends T> c = getSystemToolClass(clazz, name); |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
145 |
try { |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
146 |
return c.asSubclass(clazz).newInstance(); |
27579 | 147 |
} catch (InstantiationException | IllegalAccessException | RuntimeException | Error e) { |
148 |
return trace(WARNING, e); |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
149 |
} |
10 | 150 |
} |
151 |
||
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
152 |
private <T> Class<? extends T> getSystemToolClass(Class<T> clazz, String name) { |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
153 |
Reference<Class<?>> refClass = toolClasses.get(name); |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
154 |
Class<?> c = (refClass == null ? null : refClass.get()); |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
155 |
if (c == null) { |
10 | 156 |
try { |
27579 | 157 |
c = Class.forName(name, false, ClassLoader.getSystemClassLoader()); |
158 |
} catch (ClassNotFoundException | RuntimeException | Error e) { |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
159 |
return trace(WARNING, e); |
10 | 160 |
} |
27579 | 161 |
toolClasses.put(name, new WeakReference<>(c)); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
162 |
} |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
163 |
return c.asSubclass(clazz); |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
164 |
} |
10 | 165 |
} |