author | darcy |
Mon, 13 Jun 2016 09:12:35 -0700 | |
changeset 38918 | bf1ed1a40f5b |
parent 37853 | b4ea8806ad1a |
child 42270 | 3bd3e7e378b5 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36156
9ff93012d1e3
8149328: remove the dependency on java.logging from java.compiler
vromero
parents:
35426
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, 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 |
||
36526 | 28 |
import java.lang.reflect.InvocationTargetException; |
29 |
import java.lang.reflect.Method; |
|
37853 | 30 |
import java.security.AccessController; |
31 |
import java.security.PrivilegedAction; |
|
36526 | 32 |
import java.util.Iterator; |
33 |
import java.util.ServiceConfigurationError; |
|
34 |
import java.util.ServiceLoader; |
|
10 | 35 |
|
36 |
/** |
|
37 |
* Provides methods for locating tool providers, for example, |
|
38 |
* providers of compilers. This class complements the |
|
39 |
* functionality of {@link java.util.ServiceLoader}. |
|
40 |
* |
|
41 |
* @author Peter von der Ahé |
|
42 |
* @since 1.6 |
|
43 |
*/ |
|
44 |
public class ToolProvider { |
|
45 |
||
36526 | 46 |
private static final String systemJavaCompilerModule = "jdk.compiler"; |
47 |
private static final String systemJavaCompilerName = "com.sun.tools.javac.api.JavacTool"; |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
48 |
|
10 | 49 |
/** |
25287 | 50 |
* Returns the Java™ programming language compiler provided |
10 | 51 |
* with this platform. |
28332
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
52 |
* <p>The file manager returned by calling |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
53 |
* {@link JavaCompiler#getStandardFileManager getStandardFileManager} |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
54 |
* on this compiler supports paths provided by any |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
55 |
* {@linkplain java.nio.file.FileSystem filesystem}.</p> |
10 | 56 |
* @return the compiler provided with this platform or |
57 |
* {@code null} if no compiler is provided |
|
36526 | 58 |
* @implNote This implementation returns the compiler provided |
59 |
* by the {@code jdk.compiler} module if that module is available, |
|
60 |
* and null otherwise. |
|
10 | 61 |
*/ |
62 |
public static JavaCompiler getSystemJavaCompiler() { |
|
36526 | 63 |
return getSystemTool(JavaCompiler.class, |
64 |
systemJavaCompilerModule, systemJavaCompilerName); |
|
10 | 65 |
} |
66 |
||
36526 | 67 |
private static final String systemDocumentationToolModule = "jdk.javadoc"; |
68 |
private static final String systemDocumentationToolName = "jdk.javadoc.internal.api.JavadocTool"; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
69 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
70 |
/** |
25287 | 71 |
* 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
|
72 |
* with this platform. |
28332
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
73 |
* <p>The file manager returned by calling |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
74 |
* {@link DocumentationTool#getStandardFileManager getStandardFileManager} |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
75 |
* on this tool supports paths provided by any |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
76 |
* {@linkplain java.nio.file.FileSystem filesystem}.</p> |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
77 |
* @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
|
78 |
* {@code null} if no documentation tool is provided |
36526 | 79 |
* @implNote This implementation returns the tool provided |
80 |
* by the {@code jdk.javadoc} module if that module is available, |
|
81 |
* and null otherwise. |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
82 |
*/ |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
83 |
public static DocumentationTool getSystemDocumentationTool() { |
36526 | 84 |
return getSystemTool(DocumentationTool.class, |
85 |
systemDocumentationToolModule, systemDocumentationToolName); |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
86 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
87 |
|
10 | 88 |
/** |
89 |
* Returns the class loader for tools provided with this platform. |
|
90 |
* This does not include user-installed tools. Use the |
|
91 |
* {@linkplain java.util.ServiceLoader service provider mechanism} |
|
92 |
* for locating user installed tools. |
|
93 |
* |
|
94 |
* @return the class loader for tools provided with this platform |
|
95 |
* or {@code null} if no tools are provided |
|
96 |
*/ |
|
97 |
public static ClassLoader getSystemToolClassLoader() { |
|
27579 | 98 |
return ClassLoader.getSystemClassLoader(); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
99 |
} |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
100 |
|
36526 | 101 |
private static final boolean useLegacy; |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
102 |
|
36526 | 103 |
static { |
104 |
Class<?> c = null; |
|
105 |
try { |
|
106 |
c = Class.forName("java.lang.reflect.Module"); |
|
107 |
} catch (Throwable t) { |
|
108 |
} |
|
109 |
useLegacy = (c == null); |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
110 |
} |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
111 |
|
36526 | 112 |
/** |
113 |
* Get an instance of a system tool using the service loader. |
|
114 |
* @implNote By default, this returns the implementation in the specified module. |
|
115 |
* For limited backward compatibility, if this code is run on an older version |
|
116 |
* of the Java platform that does not support modules, this method will |
|
117 |
* try and create an instance of the named class. Note that implies the |
|
118 |
* class must be available on the system class path. |
|
119 |
* @param <T> the interface of the tool |
|
120 |
* @param clazz the interface of the tool |
|
121 |
* @param moduleName the name of the module containing the desired implementation |
|
122 |
* @param className the class name of the desired implementation |
|
123 |
* @return the specified implementation of the tool |
|
124 |
*/ |
|
125 |
private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) { |
|
126 |
if (useLegacy) { |
|
127 |
try { |
|
38918
bf1ed1a40f5b
8155880: Fix langtools usage of the deprecated Class.newInstance method
darcy
parents:
37853
diff
changeset
|
128 |
return Class.forName(className, true, ClassLoader.getSystemClassLoader()). |
bf1ed1a40f5b
8155880: Fix langtools usage of the deprecated Class.newInstance method
darcy
parents:
37853
diff
changeset
|
129 |
asSubclass(clazz).getConstructor().newInstance(); |
36526 | 130 |
} catch (ReflectiveOperationException e) { |
131 |
throw new Error(e); |
|
132 |
} |
|
133 |
} |
|
6577
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 |
try { |
36526 | 136 |
ServiceLoader<T> sl = ServiceLoader.load(clazz, ClassLoader.getSystemClassLoader()); |
137 |
for (Iterator<T> iter = sl.iterator(); iter.hasNext(); ) { |
|
138 |
T tool = iter.next(); |
|
139 |
if (matches(tool, moduleName)) |
|
140 |
return tool; |
|
141 |
} |
|
142 |
} catch (ServiceConfigurationError e) { |
|
36156
9ff93012d1e3
8149328: remove the dependency on java.logging from java.compiler
vromero
parents:
35426
diff
changeset
|
143 |
throw new Error(e); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
144 |
} |
36526 | 145 |
return null; |
10 | 146 |
} |
147 |
||
36526 | 148 |
/** |
37853 | 149 |
* Determine if this is the desired tool instance. |
36526 | 150 |
* @param <T> the interface of the tool |
151 |
* @param tool the instance of the tool |
|
152 |
* @param moduleName the name of the module containing the desired implementation |
|
153 |
* @return true if and only if the tool matches the specified criteria |
|
154 |
*/ |
|
155 |
private static <T> boolean matches(T tool, String moduleName) { |
|
37853 | 156 |
PrivilegedAction<Boolean> pa = () -> { |
157 |
// for now, use reflection to implement |
|
158 |
// return moduleName.equals(tool.getClass().getModule().getName()); |
|
159 |
try { |
|
160 |
Method getModuleMethod = Class.class.getDeclaredMethod("getModule"); |
|
161 |
Object toolModule = getModuleMethod.invoke(tool.getClass()); |
|
162 |
Method getNameMethod = toolModule.getClass().getDeclaredMethod("getName"); |
|
163 |
String toolModuleName = (String) getNameMethod.invoke(toolModule); |
|
164 |
return moduleName.equals(toolModuleName); |
|
165 |
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { |
|
166 |
return false; |
|
167 |
} |
|
168 |
}; |
|
169 |
return AccessController.doPrivileged(pa); |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
170 |
} |
10 | 171 |
} |