author | naoto |
Fri, 16 Feb 2018 14:03:34 -0800 | |
changeset 48910 | 67cdc215ed70 |
parent 48512 | e8e8c9e6ccf8 |
child 58091 | c0cc906cb29c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
48512
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, 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 |
||
37853 | 28 |
import java.security.AccessController; |
29 |
import java.security.PrivilegedAction; |
|
36526 | 30 |
import java.util.Iterator; |
31 |
import java.util.ServiceConfigurationError; |
|
32 |
import java.util.ServiceLoader; |
|
10 | 33 |
|
34 |
/** |
|
35 |
* Provides methods for locating tool providers, for example, |
|
36 |
* providers of compilers. This class complements the |
|
37 |
* functionality of {@link java.util.ServiceLoader}. |
|
38 |
* |
|
39 |
* @author Peter von der Ahé |
|
40 |
* @since 1.6 |
|
41 |
*/ |
|
42 |
public class ToolProvider { |
|
43 |
||
36526 | 44 |
private static final String systemJavaCompilerModule = "jdk.compiler"; |
45 |
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
|
46 |
|
10 | 47 |
/** |
25287 | 48 |
* Returns the Java™ programming language compiler provided |
10 | 49 |
* with this platform. |
28332
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
50 |
* <p>The file manager returned by calling |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
51 |
* {@link JavaCompiler#getStandardFileManager getStandardFileManager} |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
52 |
* on this compiler supports paths provided by any |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
53 |
* {@linkplain java.nio.file.FileSystem filesystem}.</p> |
10 | 54 |
* @return the compiler provided with this platform or |
55 |
* {@code null} if no compiler is provided |
|
36526 | 56 |
* @implNote This implementation returns the compiler provided |
57 |
* by the {@code jdk.compiler} module if that module is available, |
|
42270
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
58 |
* and {@code null} otherwise. |
10 | 59 |
*/ |
60 |
public static JavaCompiler getSystemJavaCompiler() { |
|
36526 | 61 |
return getSystemTool(JavaCompiler.class, |
62 |
systemJavaCompilerModule, systemJavaCompilerName); |
|
10 | 63 |
} |
64 |
||
36526 | 65 |
private static final String systemDocumentationToolModule = "jdk.javadoc"; |
66 |
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
|
67 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
68 |
/** |
25287 | 69 |
* 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
|
70 |
* with this platform. |
28332
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
71 |
* <p>The file manager returned by calling |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
72 |
* {@link DocumentationTool#getStandardFileManager getStandardFileManager} |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
73 |
* on this tool supports paths provided by any |
cd3ea1087d2b
8059977: StandardJavaFileManager should support java.nio.file.Path
jjg
parents:
27579
diff
changeset
|
74 |
* {@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
|
75 |
* @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
|
76 |
* {@code null} if no documentation tool is provided |
36526 | 77 |
* @implNote This implementation returns the tool provided |
78 |
* by the {@code jdk.javadoc} module if that module is available, |
|
42270
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
79 |
* and {@code null} otherwise. |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
80 |
*/ |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
81 |
public static DocumentationTool getSystemDocumentationTool() { |
36526 | 82 |
return getSystemTool(DocumentationTool.class, |
83 |
systemDocumentationToolModule, systemDocumentationToolName); |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
84 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
8032
diff
changeset
|
85 |
|
10 | 86 |
/** |
42270
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
87 |
* Returns a class loader that may be used to load system tools, |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
88 |
* or {@code null} if no such special loader is provided. |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
89 |
* @implSpec This implementation always returns {@code null}. |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
90 |
* @deprecated This method is subject to removal in a future version of |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
91 |
* Java SE. |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
92 |
* Use the {@link java.util.spi.ToolProvider system tool provider} or |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
93 |
* {@link java.util.ServiceLoader service loader} mechanisms to |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
94 |
* locate system tools as well as user-installed tools. |
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
95 |
* @return a class loader, or {@code null} |
10 | 96 |
*/ |
47462
b1b37e21fb6f
8172818: Add since=9 deprecation information to javax.lang.model classes
darcy
parents:
47216
diff
changeset
|
97 |
@Deprecated(since="9") |
10 | 98 |
public static ClassLoader getSystemToolClassLoader() { |
42270
3bd3e7e378b5
8155765: javax.tools.ToolProvider::getSystemToolClassLoader returns app class loader even if no tool is available
jjg
parents:
38918
diff
changeset
|
99 |
return null; |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
100 |
} |
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
101 |
|
36526 | 102 |
/** |
103 |
* Get an instance of a system tool using the service loader. |
|
104 |
* @implNote By default, this returns the implementation in the specified module. |
|
105 |
* For limited backward compatibility, if this code is run on an older version |
|
106 |
* of the Java platform that does not support modules, this method will |
|
107 |
* try and create an instance of the named class. Note that implies the |
|
108 |
* class must be available on the system class path. |
|
109 |
* @param <T> the interface of the tool |
|
110 |
* @param clazz the interface of the tool |
|
111 |
* @param moduleName the name of the module containing the desired implementation |
|
112 |
* @param className the class name of the desired implementation |
|
113 |
* @return the specified implementation of the tool |
|
114 |
*/ |
|
115 |
private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) { |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
116 |
|
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
117 |
try { |
36526 | 118 |
ServiceLoader<T> sl = ServiceLoader.load(clazz, ClassLoader.getSystemClassLoader()); |
119 |
for (Iterator<T> iter = sl.iterator(); iter.hasNext(); ) { |
|
120 |
T tool = iter.next(); |
|
121 |
if (matches(tool, moduleName)) |
|
122 |
return tool; |
|
123 |
} |
|
124 |
} catch (ServiceConfigurationError e) { |
|
36156
9ff93012d1e3
8149328: remove the dependency on java.logging from java.compiler
vromero
parents:
35426
diff
changeset
|
125 |
throw new Error(e); |
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
126 |
} |
36526 | 127 |
return null; |
10 | 128 |
} |
129 |
||
36526 | 130 |
/** |
37853 | 131 |
* Determine if this is the desired tool instance. |
48512
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
132 |
* @param <T> the interface of the tool |
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
133 |
* @param tool the instance of the tool |
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
134 |
* @param moduleName the name of the module containing the desired implementation |
36526 | 135 |
* @return true if and only if the tool matches the specified criteria |
136 |
*/ |
|
137 |
private static <T> boolean matches(T tool, String moduleName) { |
|
37853 | 138 |
PrivilegedAction<Boolean> pa = () -> { |
48512
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
139 |
Module toolModule = tool.getClass().getModule(); |
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
140 |
String toolModuleName = toolModule.getName(); |
e8e8c9e6ccf8
8194901: remove interim code from javax.tools.ToolProvider
jjg
parents:
47462
diff
changeset
|
141 |
return toolModuleName.equals(moduleName); |
37853 | 142 |
}; |
143 |
return AccessController.doPrivileged(pa); |
|
6577
96c6451389fc
6604599: ToolProvider should be less compiler-specific
jjg
parents:
5520
diff
changeset
|
144 |
} |
10 | 145 |
} |