Skip to content

Commit 6ff978a

Browse files
8267204: Expose access to underlying streams in Reporter
Reviewed-by: prappo
1 parent 76b54a1 commit 6ff978a

File tree

10 files changed

+508
-118
lines changed

10 files changed

+508
-118
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/doclet/Reporter.java

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 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
@@ -25,41 +25,124 @@
2525

2626
package jdk.javadoc.doclet;
2727

28+
import java.io.PrintWriter;
29+
import java.util.Locale;
2830
import javax.lang.model.element.Element;
2931
import javax.tools.Diagnostic;
32+
import javax.tools.FileObject;
3033

3134
import com.sun.source.util.DocTreePath;
3235

3336
/**
34-
* This interface provides error, warning and notice reporting.
37+
* Interface for reporting diagnostics and other messages.
38+
*
39+
* <p>Diagnostics consist of a {@link Diagnostic.Kind diagnostic kind} and a message,
40+
* and may additionally be associated with an {@link Element element},
41+
* a {@link DocTreePath tree node} in a documentation comment,
42+
* or an arbitrary position in a given {@link FileObject file}.
43+
* Other messages may be written directly to one of two streams that are informally
44+
* for use by "standard output" and "diagnostic output", where "standard output"
45+
* means the output that is the expected result of executing some operation,
46+
* such as the command-line help that is generated when using a {@code --help} option,
47+
* and "diagnostic output" refers to any errors, warnings and other output that is
48+
* a side-effect of executing the operation.
49+
*
50+
* <p>The exact manner in which diagnostics are output is unspecified and depends
51+
* on the enclosing context. For example:
52+
* <ul>
53+
* <li>The {@link javax.tools.DocumentationTool} API allows a client to specify a
54+
* {@link javax.tools.DiagnosticListener} to which diagnostics will be
55+
* {@link javax.tools.DiagnosticListener#report reported}. If no listener is specified,
56+
* diagnostics will be written to a given stream, or to {@code System.err} if no such
57+
* stream is provided.
58+
* <li>The {@link java.util.spi.ToolProvider} API allows a client to specify
59+
* the streams to be used for reporting standard and diagnostic output.
60+
* </ul>
3561
*
3662
* @since 9
3763
*/
3864
public interface Reporter {
3965

4066
/**
41-
* Print error message and increment error count.
67+
* Prints a diagnostic message.
68+
*
69+
* @param kind the kind of diagnostic
70+
* @param message the message to be printed
71+
*/
72+
void print(Diagnostic.Kind kind, String message);
73+
74+
/**
75+
* Prints a diagnostic message related to a tree node in a documentation comment.
76+
*
77+
* @param kind the kind of diagnostic
78+
* @param path the path for the tree node
79+
* @param message the message to be printed
80+
*/
81+
void print(Diagnostic.Kind kind, DocTreePath path, String message);
82+
83+
/**
84+
* Prints a diagnostic message related to an element.
4285
*
43-
* @param kind specify the diagnostic kind
44-
* @param msg message to print
86+
* @param kind the kind of diagnostic
87+
* @param element the element
88+
* @param message the message to be printed
4589
*/
46-
void print(Diagnostic.Kind kind, String msg);
90+
void print(Diagnostic.Kind kind, Element element, String message);
4791

4892
/**
49-
* Print an error message and increment error count.
93+
* Prints a diagnostic message related to a position within a range of characters in a file.
94+
* The positions are all 0-based character offsets from the beginning of content of the file.
95+
* The positions should satisfy the relation {@code start <= pos <= end}.
5096
*
51-
* @param kind specify the diagnostic kind
52-
* @param path the DocTreePath of item where the error occurs
53-
* @param msg message to print
97+
* @param kind the kind of diagnostic
98+
* @param file the file
99+
* @param start the beginning of the enclosing range
100+
* @param pos the position
101+
* @param end the end of the enclosing range
102+
* @param message the message to be printed
103+
*
104+
* @since 17
105+
*/
106+
void print(Diagnostic.Kind kind, FileObject file, int start, int pos, int end, String message);
107+
108+
/**
109+
* Returns a writer that can be used to write non-diagnostic output,
110+
* or {@code null} if no such writer is available.
111+
*
112+
* @apiNote
113+
* The value may or may not be the same as that returned by {@link #getDiagnosticWriter()}.
114+
*
115+
* @implSpec
116+
* This implementation returns {@code null}.
117+
* The implementation provided by the {@code javadoc} tool to
118+
* {@link Doclet#init(Locale, Reporter) initialize} a doclet
119+
* always returns a non-{@code null} value.
120+
*
121+
* @return the writer
122+
* @since 17
54123
*/
55-
void print(Diagnostic.Kind kind, DocTreePath path, String msg);
124+
default PrintWriter getStandardWriter() {
125+
return null;
126+
}
56127

57128
/**
58-
* Print an error message and increment error count.
129+
* Returns a writer that can be used to write diagnostic output,
130+
* or {@code null} if no such writer is available.
131+
*
132+
* @apiNote
133+
* The value may or may not be the same as that returned by {@link #getStandardWriter()}.
59134
*
60-
* @param kind specify the diagnostic kind
61-
* @param e the Element for which the error occurs
62-
* @param msg message to print
135+
* @implSpec
136+
* This implementation returns {@code null}.
137+
* The implementation provided by the {@code javadoc} tool to
138+
* {@link Doclet#init(Locale, Reporter) initialize} a doclet
139+
* always returns a non-{@code null} value.
140+
*
141+
* @return the writer
142+
* @since 17
63143
*/
64-
void print(Diagnostic.Kind kind, Element e, String msg);
144+
default PrintWriter getDiagnosticWriter() {
145+
return null;
146+
}
147+
65148
}

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Messages.java

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -26,6 +26,7 @@
2626

2727
import javax.lang.model.element.Element;
2828
import javax.tools.Diagnostic;
29+
import javax.tools.FileObject;
2930

3031
import com.sun.source.util.DocTreePath;
3132
import jdk.javadoc.doclet.Reporter;
@@ -76,8 +77,8 @@ public Resources getResources() {
7677
/**
7778
* Reports an error message to the doclet's reporter.
7879
*
79-
* @param key the name of a resource containing the message to be printed
80-
* @param args optional arguments to be replaced in the message.
80+
* @param key the name of a resource containing the message to be printed
81+
* @param args optional arguments to be replaced in the message
8182
*/
8283
public void error(String key, Object... args) {
8384
report(ERROR, resources.getText(key, args));
@@ -86,22 +87,35 @@ public void error(String key, Object... args) {
8687
/**
8788
* Reports an error message to the doclet's reporter.
8889
*
89-
* @param path a path identifying the position to be included with
90-
* the message
91-
* @param key the name of a resource containing the message to be printed
92-
* @param args optional arguments to be replaced in the message.
90+
* @param path a path identifying the position to be included with the message
91+
* @param key the name of a resource containing the message to be printed
92+
* @param args optional arguments to be replaced in the message
9393
*/
9494
public void error(DocTreePath path, String key, Object... args) {
9595
report(ERROR, path, resources.getText(key, args));
9696
}
9797

98+
/**
99+
* Reports an error message to the doclet's reporter.
100+
*
101+
* @param fo the file object to be associated with the message
102+
* @param start the start of a range of characters to be associated with the message
103+
* @param pos the position to be associated with the message
104+
* @param end the end of a range of characters to be associated with the message
105+
* @param key the name of a resource containing the message to be printed
106+
* @param args optional arguments to be replaced in the message
107+
*/
108+
public void error(FileObject fo, int start, int pos, int end, String key, Object... args) {
109+
report(ERROR, fo, start, pos, end, resources.getText(key, args));
110+
}
111+
98112
// ***** Warnings *****
99113

100114
/**
101115
* Reports a warning message to the doclet's reporter.
102116
*
103-
* @param key the name of a resource containing the message to be printed
104-
* @param args optional arguments to be replaced in the message.
117+
* @param key the name of a resource containing the message to be printed
118+
* @param args optional arguments to be replaced in the message
105119
*/
106120
public void warning(String key, Object... args) {
107121
report(WARNING, resources.getText(key, args));
@@ -110,10 +124,9 @@ public void warning(String key, Object... args) {
110124
/**
111125
* Reports a warning message to the doclet's reporter.
112126
*
113-
* @param path a path identifying the position to be included with
114-
* the message
115-
* @param key the name of a resource containing the message to be printed
116-
* @param args optional arguments to be replaced in the message.
127+
* @param path a path identifying the position to be included with the message
128+
* @param key the name of a resource containing the message to be printed
129+
* @param args optional arguments to be replaced in the message
117130
*/
118131
public void warning(DocTreePath path, String key, Object... args) {
119132
if (configuration.showMessage(path, key)) {
@@ -124,28 +137,43 @@ public void warning(DocTreePath path, String key, Object... args) {
124137
/**
125138
* Reports a warning message to the doclet's reporter.
126139
*
127-
* @param e an element identifying the declaration whose position should
128-
* to be included with the message
129-
* @param key the name of a resource containing the message to be printed
130-
* @param args optional arguments to be replaced in the message.
140+
* @param e an element identifying the position to be included with the message
141+
* @param key the name of a resource containing the message to be printed
142+
* @param args optional arguments to be replaced in the message
131143
*/
132144
public void warning(Element e, String key, Object... args) {
133145
if (configuration.showMessage(e, key)) {
134146
report(WARNING, e, resources.getText(key, args));
135147
}
136148
}
137149

150+
/**
151+
* Reports a warning message to the doclet's reporter.
152+
*
153+
* @param fo the file object to be associated with the message
154+
* @param start the start of a range of characters to be associated with the message
155+
* @param pos the position to be associated with the message
156+
* @param end the end of a range of characters to be associated with the message
157+
* @param key the name of a resource containing the message to be printed
158+
* @param args optional arguments to be replaced in the message
159+
*/
160+
public void warning(FileObject fo, int start, int pos, int end, String key, Object... args) {
161+
report(WARNING, fo, start, pos, end, resources.getText(key, args));
162+
}
163+
138164
// ***** Notices *****
139165

140166
/**
141167
* Reports an informational notice to the doclet's reporter.
168+
* The message is written directly to the reporter's diagnostic stream.
142169
*
143-
* @param key the name of a resource containing the message to be printed
144-
* @param args optional arguments to be replaced in the message.
170+
* @param key the name of a resource containing the message to be printed
171+
* @param args optional arguments to be replaced in the message
145172
*/
146173
public void notice(String key, Object... args) {
147174
if (!configuration.getOptions().quiet()) {
148-
report(NOTE, resources.getText(key, args));
175+
// Note: we do not use report(NOTE, ...) which would prefix the output with "Note:"
176+
reporter.getDiagnosticWriter().println(resources.getText(key, args));
149177
}
150178
}
151179

@@ -162,4 +190,8 @@ private void report(Diagnostic.Kind k, DocTreePath p, String msg) {
162190
private void report(Diagnostic.Kind k, Element e, String msg) {
163191
reporter.print(k, e, msg);
164192
}
193+
194+
private void report(Diagnostic.Kind k, FileObject fo, int start, int pos, int end, String msg) {
195+
reporter.print(k, fo, start, pos, end, msg);
196+
}
165197
}

0 commit comments

Comments
 (0)