39105
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package toolbox;
|
|
25 |
|
|
26 |
import java.io.File;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.io.PrintWriter;
|
|
29 |
import java.nio.file.Path;
|
|
30 |
import java.nio.file.Paths;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.Arrays;
|
|
33 |
import java.util.Collections;
|
|
34 |
import java.util.HashMap;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Map;
|
|
37 |
import java.util.stream.Collectors;
|
|
38 |
import java.util.stream.Stream;
|
|
39 |
|
|
40 |
import javax.tools.DocumentationTool.DocumentationTask;
|
|
41 |
import javax.tools.JavaFileManager;
|
|
42 |
import javax.tools.JavaFileObject;
|
|
43 |
import javax.tools.StandardJavaFileManager;
|
|
44 |
import javax.tools.StandardLocation;
|
|
45 |
import javax.tools.ToolProvider;
|
|
46 |
|
|
47 |
import jdk.javadoc.internal.api.JavadocTool;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* A task to configure and run the documentation tool, javadoc.
|
|
51 |
*/
|
|
52 |
public class JavadocTask extends AbstractTask<JavadocTask> {
|
|
53 |
private boolean includeStandardOptions;
|
|
54 |
private List<Path> classpath;
|
|
55 |
private List<Path> sourcepath;
|
|
56 |
private Path outdir;
|
|
57 |
private List<String> options;
|
|
58 |
private List<String> classes;
|
|
59 |
private List<String> files;
|
|
60 |
private List<JavaFileObject> fileObjects;
|
|
61 |
private JavaFileManager fileManager;
|
|
62 |
|
|
63 |
private JavadocTool jdtool;
|
|
64 |
private StandardJavaFileManager internalFileManager;
|
|
65 |
private Class<?> docletClass = null; // use the standard doclet by default
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Creates a task to execute {@code javadoc} using API mode.
|
|
69 |
* @param toolBox the {@code ToolBox} to use
|
|
70 |
*/
|
|
71 |
public JavadocTask(ToolBox toolBox) {
|
|
72 |
super(toolBox, Task.Mode.API);
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Creates a task to execute {@code javadoc} in a specified mode.
|
|
77 |
* @param toolBox the {@code ToolBox} to use
|
|
78 |
* @param mode the mode to be used
|
|
79 |
*/
|
|
80 |
public JavadocTask(ToolBox toolBox, Task.Mode mode) {
|
|
81 |
super(toolBox, mode);
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Sets the classpath.
|
|
86 |
* @param classpath the classpath
|
|
87 |
* @return this task object
|
|
88 |
*/
|
|
89 |
public JavadocTask classpath(String classpath) {
|
|
90 |
this.classpath = Stream.of(classpath.split(File.pathSeparator))
|
|
91 |
.filter(s -> !s.isEmpty())
|
|
92 |
.map(s -> Paths.get(s))
|
|
93 |
.collect(Collectors.toList());
|
|
94 |
return this;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Sets the classpath.
|
|
99 |
* @param classpath the classpath
|
|
100 |
* @return this task object
|
|
101 |
*/
|
|
102 |
public JavadocTask classpath(Path... classpath) {
|
|
103 |
this.classpath = Arrays.asList(classpath);
|
|
104 |
return this;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Sets the classpath.
|
|
109 |
* @param classpath the classpath
|
|
110 |
* @return this task object
|
|
111 |
*/
|
|
112 |
public JavadocTask classpath(List<Path> classpath) {
|
|
113 |
this.classpath = classpath;
|
|
114 |
return this;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Sets the sourcepath.
|
|
119 |
* @param sourcepath the sourcepath
|
|
120 |
* @return this task object
|
|
121 |
*/
|
|
122 |
public JavadocTask sourcepath(String sourcepath) {
|
|
123 |
this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
|
|
124 |
.filter(s -> !s.isEmpty())
|
|
125 |
.map(s -> Paths.get(s))
|
|
126 |
.collect(Collectors.toList());
|
|
127 |
return this;
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* Sets the sourcepath.
|
|
132 |
* @param sourcepath the sourcepath
|
|
133 |
* @return this task object
|
|
134 |
*/
|
|
135 |
public JavadocTask sourcepath(Path... sourcepath) {
|
|
136 |
this.sourcepath = Arrays.asList(sourcepath);
|
|
137 |
return this;
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Sets the sourcepath.
|
|
142 |
* @param sourcepath the sourcepath
|
|
143 |
* @return this task object
|
|
144 |
*/
|
|
145 |
public JavadocTask sourcepath(List<Path> sourcepath) {
|
|
146 |
this.sourcepath = sourcepath;
|
|
147 |
return this;
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Sets the output directory.
|
|
152 |
* @param outdir the output directory
|
|
153 |
* @return this task object
|
|
154 |
*/
|
|
155 |
public JavadocTask outdir(String outdir) {
|
|
156 |
this.outdir = Paths.get(outdir);
|
|
157 |
return this;
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Sets the output directory.
|
|
162 |
* @param outdir the output directory
|
|
163 |
* @return this task object
|
|
164 |
*/
|
|
165 |
public JavadocTask outdir(Path outdir) {
|
|
166 |
this.outdir = outdir;
|
|
167 |
return this;
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Sets the options.
|
|
172 |
* @param options the options
|
|
173 |
* @return this task object
|
|
174 |
*/
|
|
175 |
public JavadocTask options(String... options) {
|
|
176 |
this.options = Arrays.asList(options);
|
|
177 |
return this;
|
|
178 |
}
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Sets the files to be documented.
|
|
182 |
* @param files the files
|
|
183 |
* @return this task object
|
|
184 |
*/
|
|
185 |
public JavadocTask files(String... files) {
|
|
186 |
this.files = Arrays.asList(files);
|
|
187 |
return this;
|
|
188 |
}
|
|
189 |
|
|
190 |
/**
|
|
191 |
* Sets the files to be documented.
|
|
192 |
* @param files the files
|
|
193 |
* @return this task object
|
|
194 |
*/
|
|
195 |
public JavadocTask files(Path... files) {
|
|
196 |
this.files = Stream.of(files)
|
|
197 |
.map(Path::toString)
|
|
198 |
.collect(Collectors.toList());
|
|
199 |
return this;
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Sets the files to be documented.
|
|
204 |
* @param files the files
|
|
205 |
* @return this task object
|
|
206 |
*/
|
|
207 |
public JavadocTask files(List<Path> files) {
|
|
208 |
this.files = files.stream()
|
|
209 |
.map(Path::toString)
|
|
210 |
.collect(Collectors.toList());
|
|
211 |
return this;
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Sets the sources to be documented.
|
|
216 |
* Each source string is converted into an in-memory object that
|
|
217 |
* can be passed directly to the tool.
|
|
218 |
* @param sources the sources
|
|
219 |
* @return this task object
|
|
220 |
*/
|
|
221 |
public JavadocTask sources(String... sources) {
|
|
222 |
fileObjects = Stream.of(sources)
|
|
223 |
.map(s -> new ToolBox.JavaSource(s))
|
|
224 |
.collect(Collectors.toList());
|
|
225 |
return this;
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Sets the file manager to be used by this task.
|
|
230 |
* @param fileManager the file manager
|
|
231 |
* @return this task object
|
|
232 |
*/
|
|
233 |
public JavadocTask fileManager(JavaFileManager fileManager) {
|
|
234 |
this.fileManager = fileManager;
|
|
235 |
return this;
|
|
236 |
}
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Sets the doclet class to be invoked by javadoc.
|
|
240 |
* Note: this is applicable only in API mode.
|
|
241 |
* @param docletClass the user specified doclet
|
|
242 |
* @return this task object
|
|
243 |
*/
|
|
244 |
public JavadocTask docletClass(Class<?> docletClass) {
|
|
245 |
this.docletClass = docletClass;
|
|
246 |
return this;
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* {@inheritDoc}
|
|
251 |
* @return the name "javadoc"
|
|
252 |
*/
|
|
253 |
@Override
|
|
254 |
public String name() {
|
|
255 |
return "javadoc";
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Calls the javadoc tool with the arguments as currently configured.
|
|
260 |
* @return a Result object indicating the outcome of the execution
|
|
261 |
* and the content of any output written to stdout, stderr, or the
|
|
262 |
* main stream by the tool.
|
|
263 |
*/
|
|
264 |
@Override
|
|
265 |
public Task.Result run() {
|
|
266 |
if (mode == Task.Mode.EXEC)
|
|
267 |
return runExec();
|
|
268 |
|
|
269 |
AbstractTask.WriterOutput direct = new AbstractTask.WriterOutput();
|
|
270 |
// The following are to catch output to System.out and System.err,
|
|
271 |
// in case these are used instead of the primary (main) stream
|
|
272 |
AbstractTask.StreamOutput sysOut = new AbstractTask.StreamOutput(System.out, System::setOut);
|
|
273 |
AbstractTask.StreamOutput sysErr = new AbstractTask.StreamOutput(System.err, System::setErr);
|
|
274 |
int rc;
|
|
275 |
Map<Task.OutputKind, String> outputMap = new HashMap<>();
|
|
276 |
try {
|
|
277 |
switch (mode == null ? Task.Mode.API : mode) {
|
|
278 |
case API:
|
|
279 |
rc = runAPI(direct.pw);
|
|
280 |
break;
|
|
281 |
case CMDLINE:
|
|
282 |
rc = runCommand(direct.pw);
|
|
283 |
break;
|
|
284 |
default:
|
|
285 |
throw new IllegalStateException();
|
|
286 |
}
|
|
287 |
} catch (IOException e) {
|
|
288 |
toolBox.out.println("Exception occurred: " + e);
|
|
289 |
rc = 99;
|
|
290 |
} finally {
|
|
291 |
outputMap.put(Task.OutputKind.STDOUT, sysOut.close());
|
|
292 |
outputMap.put(Task.OutputKind.STDERR, sysErr.close());
|
|
293 |
outputMap.put(Task.OutputKind.DIRECT, direct.close());
|
|
294 |
}
|
|
295 |
return checkExit(new Task.Result(toolBox, this, rc, outputMap));
|
|
296 |
}
|
|
297 |
|
|
298 |
private int runAPI(PrintWriter pw) throws IOException {
|
|
299 |
try {
|
|
300 |
jdtool = (JavadocTool) ToolProvider.getSystemDocumentationTool();
|
|
301 |
jdtool = new JavadocTool();
|
|
302 |
|
|
303 |
if (fileManager == null)
|
|
304 |
fileManager = internalFileManager = jdtool.getStandardFileManager(null, null, null);
|
|
305 |
if (outdir != null)
|
|
306 |
setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singletonList(outdir));
|
|
307 |
if (classpath != null)
|
|
308 |
setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
|
|
309 |
if (sourcepath != null)
|
|
310 |
setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcepath);
|
|
311 |
List<String> allOpts = new ArrayList<>();
|
|
312 |
if (options != null)
|
|
313 |
allOpts.addAll(options);
|
|
314 |
|
|
315 |
Iterable<? extends JavaFileObject> allFiles = joinFiles(files, fileObjects);
|
|
316 |
DocumentationTask task = jdtool.getTask(pw,
|
|
317 |
fileManager,
|
|
318 |
null, // diagnostic listener; should optionally collect diags
|
|
319 |
docletClass,
|
|
320 |
allOpts,
|
|
321 |
allFiles);
|
|
322 |
return ((DocumentationTask) task).call() ? 0 : 1;
|
|
323 |
} finally {
|
|
324 |
if (internalFileManager != null)
|
|
325 |
internalFileManager.close();
|
|
326 |
}
|
|
327 |
}
|
|
328 |
|
|
329 |
private void setLocationFromPaths(StandardLocation location, List<Path> files) throws IOException {
|
|
330 |
if (!(fileManager instanceof StandardJavaFileManager))
|
|
331 |
throw new IllegalStateException("not a StandardJavaFileManager");
|
|
332 |
((StandardJavaFileManager) fileManager).setLocationFromPaths(location, files);
|
|
333 |
}
|
|
334 |
|
|
335 |
private int runCommand(PrintWriter pw) {
|
|
336 |
List<String> args = getAllArgs();
|
|
337 |
String[] argsArray = args.toArray(new String[args.size()]);
|
|
338 |
return jdk.javadoc.internal.tool.Main.execute(argsArray, pw);
|
|
339 |
}
|
|
340 |
|
|
341 |
private Task.Result runExec() {
|
|
342 |
List<String> args = new ArrayList<>();
|
|
343 |
Path javadoc = toolBox.getJDKTool("javadoc");
|
|
344 |
args.add(javadoc.toString());
|
|
345 |
if (includeStandardOptions) {
|
|
346 |
args.addAll(toolBox.split(System.getProperty("test.tool.vm.opts"), " +"));
|
|
347 |
}
|
|
348 |
args.addAll(getAllArgs());
|
|
349 |
|
|
350 |
String[] argsArray = args.toArray(new String[args.size()]);
|
|
351 |
ProcessBuilder pb = getProcessBuilder();
|
|
352 |
pb.command(argsArray);
|
|
353 |
try {
|
|
354 |
return runProcess(toolBox, this, pb.start());
|
|
355 |
} catch (IOException | InterruptedException e) {
|
|
356 |
throw new Error(e);
|
|
357 |
}
|
|
358 |
}
|
|
359 |
|
|
360 |
private List<String> getAllArgs() {
|
|
361 |
List<String> args = new ArrayList<>();
|
|
362 |
if (options != null)
|
|
363 |
args.addAll(options);
|
|
364 |
if (outdir != null) {
|
|
365 |
args.add("-d");
|
|
366 |
args.add(outdir.toString());
|
|
367 |
}
|
|
368 |
if (classpath != null) {
|
|
369 |
args.add("-classpath");
|
|
370 |
args.add(toSearchPath(classpath));
|
|
371 |
}
|
|
372 |
if (sourcepath != null) {
|
|
373 |
args.add("-sourcepath");
|
|
374 |
args.add(toSearchPath(sourcepath));
|
|
375 |
}
|
|
376 |
if (classes != null)
|
|
377 |
args.addAll(classes);
|
|
378 |
if (files != null)
|
|
379 |
args.addAll(files);
|
|
380 |
|
|
381 |
return args;
|
|
382 |
}
|
|
383 |
|
|
384 |
private String toSearchPath(List<Path> files) {
|
|
385 |
return files.stream()
|
|
386 |
.map(Path::toString)
|
|
387 |
.collect(Collectors.joining(File.pathSeparator));
|
|
388 |
}
|
|
389 |
|
|
390 |
private Iterable<? extends JavaFileObject> joinFiles(
|
|
391 |
List<String> files, List<JavaFileObject> fileObjects) {
|
|
392 |
if (files == null)
|
|
393 |
return fileObjects;
|
|
394 |
if (internalFileManager == null)
|
|
395 |
internalFileManager = jdtool.getStandardFileManager(null, null, null);
|
|
396 |
Iterable<? extends JavaFileObject> filesAsFileObjects =
|
|
397 |
internalFileManager.getJavaFileObjectsFromStrings(files);
|
|
398 |
if (fileObjects == null)
|
|
399 |
return filesAsFileObjects;
|
|
400 |
List<JavaFileObject> combinedList = new ArrayList<>();
|
|
401 |
for (JavaFileObject o : filesAsFileObjects)
|
|
402 |
combinedList.add(o);
|
|
403 |
combinedList.addAll(fileObjects);
|
|
404 |
return combinedList;
|
|
405 |
}
|
|
406 |
}
|