Skip to content

Commit d52392c

Browse files
8276892: Provide a way to emulate exceptional situations in FileManager when using JavadocTester
Reviewed-by: prappo
1 parent 22c15dd commit d52392c

File tree

7 files changed

+653
-10
lines changed

7 files changed

+653
-10
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/resources/doclets.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ doclet.exception.write.file=Error writing file: {0}\n\
4545
\t({1})
4646
doclet.exception.read.resource=Error reading system resource: {0}\n\
4747
\t({1})
48-
doclet.internal.exception=An internal exception has occurred. \n\
48+
doclet.internal.exception=An internal exception has occurred.\n\
4949
\t({0})
5050
doclet.internal.report.bug=\
5151
Please file a bug against the javadoc tool via the Java bug reporting page\n\

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Main.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,12 @@
2424
*/
2525
package jdk.javadoc.internal.tool;
2626

27+
import javax.tools.JavaFileManager;
28+
import javax.tools.StandardJavaFileManager;
2729
import java.io.PrintWriter;
30+
import java.util.Objects;
31+
32+
import com.sun.tools.javac.util.Context;
2833

2934
/**
3035
* Provides external entry points (tool and programmatic) for the javadoc program.
@@ -37,11 +42,6 @@
3742

3843
public class Main {
3944

40-
/**
41-
* This constructor should never be called.
42-
*/
43-
private Main() { throw new AssertionError(); }
44-
4545
/**
4646
* The main entry point called by the launcher. This will call
4747
* System.exit with an appropriate return value.
@@ -88,6 +88,65 @@ public static int execute(String[] args, PrintWriter outWriter, PrintWriter errW
8888
return jdoc.begin(args).exitCode;
8989
}
9090

91+
92+
// builder-style API to run javadoc
93+
94+
private PrintWriter outWriter;
95+
private PrintWriter errWriter;
96+
private StandardJavaFileManager fileManager;
97+
98+
/**
99+
* Creates a default builder to run javadoc.
100+
*/
101+
public Main() { }
102+
103+
/**
104+
* Sets the output and error streams to be used when running javadoc.
105+
* The streams may be the same; they must not be {@code null}.
106+
*
107+
* @param outWriter the output stream
108+
* @param errWriter the error stream
109+
*
110+
* @return this object
111+
*/
112+
public Main setStreams(PrintWriter outWriter, PrintWriter errWriter) {
113+
this.outWriter = Objects.requireNonNull(outWriter);
114+
this.errWriter = Objects.requireNonNull(errWriter);
115+
return this;
116+
}
117+
118+
/**
119+
* Sets the file manager to be used when running javadoc.
120+
* A value of {@code null} means to use the default file manager.
121+
*
122+
* @param fileManager the file manager to use
123+
*
124+
* @return this object
125+
*/
126+
public Main setFileManager(StandardJavaFileManager fileManager) {
127+
this.fileManager = fileManager;
128+
return this;
129+
}
130+
131+
/**
132+
* Runs javadoc with preconfigured values and a given set of arguments.
133+
* Any errors will be reported to the error stream, or to {@link System#err}
134+
* if no error stream has been specified with {@code setStreams}.
135+
*
136+
* @param args the arguments
137+
*
138+
* @return a value indicating the success or otherwise of the run
139+
*/
140+
public Result run(String... args) {
141+
Context context = null;
142+
if (fileManager != null) {
143+
context = new Context();
144+
context.put(JavaFileManager.class, fileManager);
145+
}
146+
Start jdoc = new Start(context, null, outWriter, errWriter, null, null);
147+
return jdoc.begin(args);
148+
}
149+
91150
public enum Result {
92151
/** completed with no errors */
93152
OK(0),

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.IllformedLocaleException;
3737
import java.util.List;
3838
import java.util.Locale;
39+
import java.util.Map;
3940
import java.util.Objects;
4041
import java.util.Set;
4142
import java.util.function.Supplier;
@@ -520,7 +521,21 @@ private Result parseAndExecute(List<String> argList, Iterable<? extends JavaFile
520521
}
521522

522523
if (fileManager instanceof BaseFileManager bfm) {
524+
// standard file manager: use direct support for handling options
523525
bfm.handleOptions(options.fileManagerOptions());
526+
} else {
527+
// unrecognized file manager:
528+
for (Map.Entry<com.sun.tools.javac.main.Option, String> e: options.fileManagerOptions().entrySet()) {
529+
String optName = e.getKey().getPrimaryName();
530+
String optValue = e.getValue();
531+
try {
532+
if (!fileManager.handleOption(optName, List.of(optValue).iterator())) {
533+
log.error("main.unknown.option.for.filemanager", optName);
534+
}
535+
} catch (IllegalArgumentException ex) {
536+
log.error("main.bad.arg.for.filemanager.option", optName, ex.getMessage());
537+
}
538+
}
524539
}
525540

526541
String mr = com.sun.tools.javac.main.Option.MULTIRELEASE.primaryName;

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/resources/javadoc.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ main.warnings.Werror=warnings found and -Werror specified
302302
main.unknown.error=an unknown error has occurred
303303
main.internal.error=an internal error has occurred
304304
main.unexpected.exception=an unexpected exception was caught: {0}
305+
main.unknown.option.for.filemanager=option not supported by file manager: {0}
306+
main.bad.arg.for.filemanager.option=bad value for file manager option {0}: "{1}"
305307
doclet.internal.report.bug=\
306308
Please file a bug against the javadoc tool via the Java bug reporting page\n\
307309
(http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com)\n\

test/langtools/jdk/javadoc/lib/javadoc/tester/JavadocTester.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.regex.Matcher;
5959
import java.util.regex.Pattern;
6060
import java.util.stream.Collectors;
61+
import javax.tools.StandardJavaFileManager;
6162

6263

6364
/**
@@ -245,6 +246,7 @@ void check(Path dir) {
245246
private boolean automaticCheckLinks = true;
246247
private boolean automaticCheckUniqueOUT = true;
247248
private boolean useStandardStreams = false;
249+
private StandardJavaFileManager fileManager = null;
248250

249251
/** The current subtest number. Incremented when checking(...) is called. */
250252
private int numTestsRun = 0;
@@ -371,9 +373,17 @@ public void javadoc(String... args) {
371373
StreamOutput sysErr = new StreamOutput(System.err, System::setErr);
372374

373375
try {
374-
exitCode = useStandardStreams
375-
? jdk.javadoc.internal.tool.Main.execute(args) // use sysOut, sysErr
376-
: jdk.javadoc.internal.tool.Main.execute(args, outOut.pw); // default
376+
jdk.javadoc.internal.tool.Main main = new jdk.javadoc.internal.tool.Main();
377+
if (useStandardStreams) {
378+
// use sysOut, sysErr
379+
} else {
380+
// default: use single explicit stream
381+
main.setStreams(outOut.pw, outOut.pw);
382+
}
383+
if (fileManager != null) {
384+
main.setFileManager(fileManager);
385+
}
386+
exitCode = main.run(args).exitCode;
377387
} finally {
378388
outputMap.put(Output.STDOUT, sysOut.close());
379389
outputMap.put(Output.STDERR, sysErr.close());
@@ -442,6 +452,15 @@ public void setUseStandardStreams(boolean b) {
442452
useStandardStreams = b;
443453
}
444454

455+
/**
456+
* Sets the file manager to use for subsequent invocations of javadoc.
457+
* If {@code null}, a default file manager will be created and used
458+
* for each invocation.
459+
*/
460+
public void setFileManager(StandardJavaFileManager fm) {
461+
fileManager = fm;
462+
}
463+
445464
/**
446465
* The exit codes returned by the javadoc tool.
447466
* @see jdk.javadoc.internal.tool.Main.Result

0 commit comments

Comments
 (0)