author | jjg |
Mon, 09 May 2016 16:52:15 -0700 | |
changeset 37938 | 42baa89d2156 |
parent 37757 | langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/DocletInvoker.java@f38cc75b6fa0 |
child 38617 | d93a7f64e231 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36526 | 2 |
* Copyright (c) 1998, 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 |
||
37938
42baa89d2156
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents:
37757
diff
changeset
|
26 |
package com.sun.tools.javadoc.main; |
10 | 27 |
|
8633
41311e88ffb7
6964914: javadoc does not output number of warnings using user written doclet
jjg
parents:
7681
diff
changeset
|
28 |
import java.io.File; |
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
29 |
import java.io.IOException; |
14258 | 30 |
import java.lang.reflect.InvocationTargetException; |
10 | 31 |
import java.lang.reflect.Method; |
32 |
import java.lang.reflect.Modifier; |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
33 |
import java.net.MalformedURLException; |
8633
41311e88ffb7
6964914: javadoc does not output number of warnings using user written doclet
jjg
parents:
7681
diff
changeset
|
34 |
import java.net.URL; |
41311e88ffb7
6964914: javadoc does not output number of warnings using user written doclet
jjg
parents:
7681
diff
changeset
|
35 |
import java.net.URLClassLoader; |
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
36 |
import java.nio.file.Path; |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
37 |
import java.nio.file.Paths; |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
38 |
import java.util.ArrayList; |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
39 |
import java.util.regex.Pattern; |
10 | 40 |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
41 |
import javax.tools.DocumentationTool; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
42 |
import javax.tools.JavaFileManager; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
43 |
|
14258 | 44 |
import com.sun.javadoc.*; |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
45 |
import com.sun.tools.javac.util.ClientCodeException; |
14258 | 46 |
import com.sun.tools.javac.util.List; |
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
47 |
|
14258 | 48 |
import static com.sun.javadoc.LanguageVersion.*; |
49 |
||
10 | 50 |
|
51 |
/** |
|
52 |
* Class creates, controls and invokes doclets. |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
53 |
* |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
54 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
55 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
56 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
57 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
58 |
* |
10 | 59 |
* @author Neal Gafter (rewrite) |
60 |
*/ |
|
61 |
public class DocletInvoker { |
|
62 |
||
868 | 63 |
private final Class<?> docletClass; |
10 | 64 |
|
65 |
private final String docletClassName; |
|
66 |
||
67 |
private final ClassLoader appClassLoader; |
|
68 |
||
69 |
private final Messager messager; |
|
70 |
||
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
71 |
/** |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
72 |
* In API mode, exceptions thrown while calling the doclet are |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
73 |
* propagated using ClientCodeException. |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
74 |
*/ |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
75 |
private final boolean apiMode; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
76 |
|
36526 | 77 |
/** |
78 |
* Whether javadoc internal API should be exported to doclets |
|
79 |
* and (indirectly) to taglets |
|
80 |
*/ |
|
81 |
private final boolean exportInternalAPI; |
|
82 |
||
10 | 83 |
private static class DocletInvokeException extends Exception { |
84 |
private static final long serialVersionUID = 0; |
|
85 |
} |
|
86 |
||
87 |
private String appendPath(String path1, String path2) { |
|
88 |
if (path1 == null || path1.length() == 0) { |
|
89 |
return path2 == null ? "." : path2; |
|
90 |
} else if (path2 == null || path2.length() == 0) { |
|
91 |
return path1; |
|
92 |
} else { |
|
93 |
return path1 + File.pathSeparator + path2; |
|
94 |
} |
|
95 |
} |
|
96 |
||
36526 | 97 |
public DocletInvoker(Messager messager, Class<?> docletClass, boolean apiMode, boolean exportInternalAPI) { |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
98 |
this.messager = messager; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
99 |
this.docletClass = docletClass; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
100 |
docletClassName = docletClass.getName(); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
101 |
appClassLoader = null; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
102 |
this.apiMode = apiMode; |
36526 | 103 |
this.exportInternalAPI = exportInternalAPI; // for backdoor use by standard doclet for taglets |
104 |
||
105 |
// this may not be soon enough if the class has already been loaded |
|
106 |
if (exportInternalAPI) { |
|
107 |
exportInternalAPI(docletClass.getClassLoader()); |
|
108 |
} |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
109 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
110 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
111 |
public DocletInvoker(Messager messager, JavaFileManager fileManager, |
1365 | 112 |
String docletClassName, String docletPath, |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
113 |
ClassLoader docletParentClassLoader, |
36526 | 114 |
boolean apiMode, |
115 |
boolean exportInternalAPI) { |
|
10 | 116 |
this.messager = messager; |
117 |
this.docletClassName = docletClassName; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
118 |
this.apiMode = apiMode; |
36526 | 119 |
this.exportInternalAPI = exportInternalAPI; // for backdoor use by standard doclet for taglets |
10 | 120 |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
121 |
if (fileManager != null && fileManager.hasLocation(DocumentationTool.Location.DOCLET_PATH)) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
122 |
appClassLoader = fileManager.getClassLoader(DocumentationTool.Location.DOCLET_PATH); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
123 |
} else { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
124 |
// construct class loader |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
125 |
String cpString = null; // make sure env.class.path defaults to dot |
10 | 126 |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
127 |
// do prepends to get correct ordering |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
128 |
cpString = appendPath(System.getProperty("env.class.path"), cpString); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
129 |
cpString = appendPath(System.getProperty("java.class.path"), cpString); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
130 |
cpString = appendPath(docletPath, cpString); |
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
131 |
URL[] urls = pathToURLs(cpString); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
132 |
if (docletParentClassLoader == null) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
133 |
appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName)); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
134 |
else |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
135 |
appClassLoader = new URLClassLoader(urls, docletParentClassLoader); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
136 |
} |
10 | 137 |
|
36526 | 138 |
if (exportInternalAPI) { |
139 |
exportInternalAPI(appClassLoader); |
|
140 |
} |
|
141 |
||
10 | 142 |
// attempt to find doclet |
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1365
diff
changeset
|
143 |
Class<?> dc = null; |
10 | 144 |
try { |
145 |
dc = appClassLoader.loadClass(docletClassName); |
|
146 |
} catch (ClassNotFoundException exc) { |
|
14543 | 147 |
messager.error(Messager.NOPOS, "main.doclet_class_not_found", docletClassName); |
10 | 148 |
messager.exit(); |
149 |
} |
|
150 |
docletClass = dc; |
|
151 |
} |
|
152 |
||
1990
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
153 |
/* |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
154 |
* Returns the delegation class loader to use when creating |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
155 |
* appClassLoader (used to load the doclet). The context class |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
156 |
* loader is the best choice, but legacy behavior was to use the |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
157 |
* default delegation class loader (aka system class loader). |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
158 |
* |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
159 |
* Here we favor using the context class loader. To ensure |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
160 |
* compatibility with existing apps, we revert to legacy |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
161 |
* behavior if either or both of the following conditions hold: |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
162 |
* |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
163 |
* 1) the doclet is loadable from the system class loader but not |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
164 |
* from the context class loader, |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
165 |
* |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
166 |
* 2) this.getClass() is loadable from the system class loader but not |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
167 |
* from the context class loader. |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
168 |
*/ |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
169 |
private ClassLoader getDelegationClassLoader(String docletClassName) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
170 |
ClassLoader ctxCL = Thread.currentThread().getContextClassLoader(); |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
171 |
ClassLoader sysCL = ClassLoader.getSystemClassLoader(); |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
172 |
if (sysCL == null) |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
173 |
return ctxCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
174 |
if (ctxCL == null) |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
175 |
return sysCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
176 |
|
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
177 |
// Condition 1. |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
178 |
try { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
179 |
sysCL.loadClass(docletClassName); |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
180 |
try { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
181 |
ctxCL.loadClass(docletClassName); |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
182 |
} catch (ClassNotFoundException e) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
183 |
return sysCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
184 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
185 |
} catch (ClassNotFoundException e) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
186 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
187 |
|
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
188 |
// Condition 2. |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
189 |
try { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
190 |
if (getClass() == sysCL.loadClass(getClass().getName())) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
191 |
try { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
192 |
if (getClass() != ctxCL.loadClass(getClass().getName())) |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
193 |
return sysCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
194 |
} catch (ClassNotFoundException e) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
195 |
return sysCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
196 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
197 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
198 |
} catch (ClassNotFoundException e) { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
199 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
200 |
|
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
201 |
return ctxCL; |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
202 |
} |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
203 |
|
10 | 204 |
/** |
205 |
* Generate documentation here. Return true on success. |
|
206 |
*/ |
|
207 |
public boolean start(RootDoc root) { |
|
208 |
Object retVal; |
|
209 |
String methodName = "start"; |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
210 |
Class<?>[] paramTypes = { RootDoc.class }; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
211 |
Object[] params = { root }; |
10 | 212 |
try { |
213 |
retVal = invoke(methodName, null, paramTypes, params); |
|
214 |
} catch (DocletInvokeException exc) { |
|
215 |
return false; |
|
216 |
} |
|
217 |
if (retVal instanceof Boolean) { |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
218 |
return ((Boolean)retVal); |
10 | 219 |
} else { |
14543 | 220 |
messager.error(Messager.NOPOS, "main.must_return_boolean", |
10 | 221 |
docletClassName, methodName); |
222 |
return false; |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Check for doclet added options here. Zero return means |
|
228 |
* option not known. Positive value indicates number of |
|
229 |
* arguments to option. Negative value means error occurred. |
|
230 |
*/ |
|
231 |
public int optionLength(String option) { |
|
232 |
Object retVal; |
|
233 |
String methodName = "optionLength"; |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
234 |
Class<?>[] paramTypes = { String.class }; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
235 |
Object[] params = { option }; |
10 | 236 |
try { |
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14545
diff
changeset
|
237 |
retVal = invoke(methodName, 0, paramTypes, params); |
10 | 238 |
} catch (DocletInvokeException exc) { |
239 |
return -1; |
|
240 |
} |
|
241 |
if (retVal instanceof Integer) { |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
242 |
return ((Integer)retVal); |
10 | 243 |
} else { |
14543 | 244 |
messager.error(Messager.NOPOS, "main.must_return_int", |
10 | 245 |
docletClassName, methodName); |
246 |
return -1; |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Let doclet check that all options are OK. Returning true means |
|
252 |
* options are OK. If method does not exist, assume true. |
|
253 |
*/ |
|
254 |
public boolean validOptions(List<String[]> optlist) { |
|
255 |
Object retVal; |
|
256 |
String options[][] = optlist.toArray(new String[optlist.length()][]); |
|
257 |
String methodName = "validOptions"; |
|
258 |
DocErrorReporter reporter = messager; |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
259 |
Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class }; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
260 |
Object[] params = { options, reporter }; |
10 | 261 |
try { |
262 |
retVal = invoke(methodName, Boolean.TRUE, paramTypes, params); |
|
263 |
} catch (DocletInvokeException exc) { |
|
264 |
return false; |
|
265 |
} |
|
266 |
if (retVal instanceof Boolean) { |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
267 |
return ((Boolean)retVal); |
10 | 268 |
} else { |
14543 | 269 |
messager.error(Messager.NOPOS, "main.must_return_boolean", |
10 | 270 |
docletClassName, methodName); |
271 |
return false; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Return the language version supported by this doclet. |
|
277 |
* If the method does not exist in the doclet, assume version 1.1. |
|
278 |
*/ |
|
279 |
public LanguageVersion languageVersion() { |
|
280 |
try { |
|
281 |
Object retVal; |
|
282 |
String methodName = "languageVersion"; |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1365
diff
changeset
|
283 |
Class<?>[] paramTypes = new Class<?>[0]; |
10 | 284 |
Object[] params = new Object[0]; |
285 |
try { |
|
286 |
retVal = invoke(methodName, JAVA_1_1, paramTypes, params); |
|
287 |
} catch (DocletInvokeException exc) { |
|
288 |
return JAVA_1_1; |
|
289 |
} |
|
290 |
if (retVal instanceof LanguageVersion) { |
|
291 |
return (LanguageVersion)retVal; |
|
292 |
} else { |
|
14543 | 293 |
messager.error(Messager.NOPOS, "main.must_return_languageversion", |
10 | 294 |
docletClassName, methodName); |
295 |
return JAVA_1_1; |
|
296 |
} |
|
297 |
} catch (NoClassDefFoundError ex) { // for boostrapping, no Enum class. |
|
298 |
return null; |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Utility method for calling doclet functionality |
|
304 |
*/ |
|
305 |
private Object invoke(String methodName, Object returnValueIfNonExistent, |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1365
diff
changeset
|
306 |
Class<?>[] paramTypes, Object[] params) |
10 | 307 |
throws DocletInvokeException { |
308 |
Method meth; |
|
309 |
try { |
|
310 |
meth = docletClass.getMethod(methodName, paramTypes); |
|
311 |
} catch (NoSuchMethodException exc) { |
|
312 |
if (returnValueIfNonExistent == null) { |
|
14543 | 313 |
messager.error(Messager.NOPOS, "main.doclet_method_not_found", |
10 | 314 |
docletClassName, methodName); |
315 |
throw new DocletInvokeException(); |
|
316 |
} else { |
|
317 |
return returnValueIfNonExistent; |
|
318 |
} |
|
319 |
} catch (SecurityException exc) { |
|
14543 | 320 |
messager.error(Messager.NOPOS, "main.doclet_method_not_accessible", |
10 | 321 |
docletClassName, methodName); |
322 |
throw new DocletInvokeException(); |
|
323 |
} |
|
324 |
if (!Modifier.isStatic(meth.getModifiers())) { |
|
14543 | 325 |
messager.error(Messager.NOPOS, "main.doclet_method_must_be_static", |
10 | 326 |
docletClassName, methodName); |
327 |
throw new DocletInvokeException(); |
|
328 |
} |
|
1990
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
329 |
ClassLoader savedCCL = |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
330 |
Thread.currentThread().getContextClassLoader(); |
10 | 331 |
try { |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
332 |
if (appClassLoader != null) // will be null if doclet class provided via API |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
333 |
Thread.currentThread().setContextClassLoader(appClassLoader); |
10 | 334 |
return meth.invoke(null , params); |
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14545
diff
changeset
|
335 |
} catch (IllegalArgumentException | NullPointerException exc) { |
14543 | 336 |
messager.error(Messager.NOPOS, "main.internal_error_exception_thrown", |
10 | 337 |
docletClassName, methodName, exc.toString()); |
338 |
throw new DocletInvokeException(); |
|
339 |
} catch (IllegalAccessException exc) { |
|
14543 | 340 |
messager.error(Messager.NOPOS, "main.doclet_method_not_accessible", |
10 | 341 |
docletClassName, methodName); |
342 |
throw new DocletInvokeException(); |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14545
diff
changeset
|
343 |
} |
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14545
diff
changeset
|
344 |
catch (InvocationTargetException exc) { |
10 | 345 |
Throwable err = exc.getTargetException(); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
346 |
if (apiMode) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
347 |
throw new ClientCodeException(err); |
10 | 348 |
if (err instanceof java.lang.OutOfMemoryError) { |
14543 | 349 |
messager.error(Messager.NOPOS, "main.out.of.memory"); |
10 | 350 |
} else { |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
351 |
messager.error(Messager.NOPOS, "main.exception_thrown", |
10 | 352 |
docletClassName, methodName, exc.toString()); |
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
353 |
exc.getTargetException().printStackTrace(System.err); |
10 | 354 |
} |
355 |
throw new DocletInvokeException(); |
|
1990
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
356 |
} finally { |
5d90be5d60bb
6176978: current Javadoc's invocation and extension (Doclet) mechanisms are problematic
jjg
parents:
1789
diff
changeset
|
357 |
Thread.currentThread().setContextClassLoader(savedCCL); |
10 | 358 |
} |
359 |
} |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
360 |
|
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
361 |
/** |
36526 | 362 |
* Export javadoc internal API to the unnamed module for a classloader. |
363 |
* This is to support continued use of existing non-standard doclets that |
|
364 |
* use the internal toolkit API and related classes. |
|
365 |
* @param cl the classloader |
|
366 |
*/ |
|
367 |
private void exportInternalAPI(ClassLoader cl) { |
|
368 |
String[] packages = { |
|
369 |
"com.sun.tools.doclets", |
|
370 |
"com.sun.tools.doclets.standard", |
|
371 |
"com.sun.tools.doclets.internal.toolkit", |
|
372 |
"com.sun.tools.doclets.internal.toolkit.taglets", |
|
373 |
"com.sun.tools.doclets.internal.toolkit.builders", |
|
374 |
"com.sun.tools.doclets.internal.toolkit.util", |
|
375 |
"com.sun.tools.doclets.internal.toolkit.util.links", |
|
376 |
"com.sun.tools.doclets.formats.html", |
|
377 |
"com.sun.tools.doclets.formats.html.markup" |
|
378 |
}; |
|
379 |
||
380 |
try { |
|
381 |
Method getModuleMethod = Class.class.getDeclaredMethod("getModule"); |
|
382 |
Object thisModule = getModuleMethod.invoke(getClass()); |
|
383 |
||
384 |
Class<?> moduleClass = Class.forName("java.lang.reflect.Module"); |
|
385 |
Method addExportsMethod = moduleClass.getDeclaredMethod("addExports", String.class, moduleClass); |
|
386 |
||
387 |
Method getUnnamedModuleMethod = ClassLoader.class.getDeclaredMethod("getUnnamedModule"); |
|
388 |
Object target = getUnnamedModuleMethod.invoke(cl); |
|
389 |
||
390 |
for (String pack : packages) { |
|
391 |
addExportsMethod.invoke(thisModule, pack, target); |
|
392 |
} |
|
393 |
} catch (Exception e) { |
|
394 |
// do nothing |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
/** |
|
28707
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
399 |
* Utility method for converting a search path string to an array of directory and JAR file |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
400 |
* URLs. |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
401 |
* |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
402 |
* Note that this method is called by the DocletInvoker. |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
403 |
* |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
404 |
* @param path the search path string |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
405 |
* @return the resulting array of directory and JAR file URLs |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
406 |
*/ |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
407 |
private static URL[] pathToURLs(String path) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
408 |
java.util.List<URL> urls = new ArrayList<>(); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
409 |
for (String s: path.split(Pattern.quote(File.pathSeparator))) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
410 |
if (!s.isEmpty()) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
411 |
URL url = fileToURL(Paths.get(s)); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
412 |
if (url != null) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
413 |
urls.add(url); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
414 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
415 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
416 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
417 |
return urls.toArray(new URL[urls.size()]); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
418 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
419 |
|
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
420 |
/** |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
421 |
* Returns the directory or JAR file URL corresponding to the specified local file name. |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
422 |
* |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
423 |
* @param file the Path object |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
424 |
* @return the resulting directory or JAR file URL, or null if unknown |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
425 |
*/ |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
426 |
private static URL fileToURL(Path file) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
427 |
Path p; |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
428 |
try { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
429 |
p = file.toRealPath(); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
430 |
} catch (IOException e) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
431 |
p = file.toAbsolutePath(); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
432 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
433 |
try { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
434 |
return p.normalize().toUri().toURL(); |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
435 |
} catch (MalformedURLException e) { |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
436 |
return null; |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
437 |
} |
2ec91c67b30b
8071617: move pathToURLs from javac.file.Locations to javadoc.DocletInvoker
jjg
parents:
25874
diff
changeset
|
438 |
} |
10 | 439 |
} |