Method from com.sun.tools.javac.util.Log Detail: |
public JavaFileObject currentSourceFile() {
return source == null ? null : source.getFile();
}
Return current sourcefile. |
static final PrintWriter defaultWriter(Context context) {
PrintWriter result = context.get(outKey);
if (result == null)
context.put(outKey, result = new PrintWriter(System.err));
return result;
}
The default writer for diagnostics |
protected void directError(String key,
Object args) {
printErrLines(key, args);
errWriter.flush();
}
|
public void flush() {
errWriter.flush();
warnWriter.flush();
noticeWriter.flush();
}
|
public static String format(String fmt,
Object args) {
return String.format((java.util.Locale)null, fmt, args);
}
|
protected int getDefaultMaxErrors() {
return 100;
}
Default value for -Xmaxerrs. |
protected int getDefaultMaxWarnings() {
return 100;
}
Default value for -Xmaxwarns. |
public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
return diagFormatter;
}
Get the current diagnostic formatter. |
public static String getLocalizedString(String key,
Object args) {
return JavacMessages.getDefaultLocalizedString("compiler.misc." + key, args);
}
Find a localized string in the resource bundle.
Because this method is static, it ignores the locale.
Use localize(key, args) when possible. |
protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
switch (dt) {
case FRAGMENT:
throw new IllegalArgumentException();
case NOTE:
return noticeWriter;
case WARNING:
return warnWriter;
case ERROR:
return errWriter;
default:
throw new Error();
}
}
|
public boolean hasDiagnosticListener() {
return diagListener != null;
}
|
public static Log instance(Context context) {
Log instance = context.get(logKey);
if (instance == null)
instance = new Log(context);
return instance;
}
Get the Log instance for this context. |
public String localize(String key,
Object args) {
return messages.getLocalizedString("compiler.misc." + key, args);
}
Find a localized string in the resource bundle. |
public void printErrLines(String key,
Object args) {
printLines(errWriter, localize(key, args));
}
Print the text of a message to the errWriter stream,
translating newlines appropriately for the platform. |
public static void printLines(PrintWriter writer,
String msg) {
int nl;
while ((nl = msg.indexOf('\n')) != -1) {
writer.println(msg.substring(0, nl));
msg = msg.substring(nl+1);
}
if (msg.length() != 0) writer.println(msg);
}
Print the text of a message, translating newlines appropriately
for the platform. |
public void printNoteLines(String key,
Object args) {
printLines(noticeWriter, localize(key, args));
}
Print the text of a message to the noticeWriter stream,
translating newlines appropriately for the platform. |
public void printVerbose(String key,
Object args) {
printLines(noticeWriter, localize("verbose." + key, args));
}
Print the localized text of a "verbose" message to the
noticeWriter stream. |
public void prompt() {
if (promptOnError) {
System.err.println(localize("resume.abort"));
char ch;
try {
while (true) {
switch (System.in.read()) {
case 'a': case 'A':
System.exit(-1);
return;
case 'r': case 'R':
return;
case 'x': case 'X':
throw new AssertionError("user abort");
default:
}
}
} catch (IOException e) {}
}
}
Prompt user after an error. |
public void rawError(int pos,
String msg) {
if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
printRawError(pos, msg);
prompt();
nerrors++;
}
errWriter.flush();
}
|
public void rawWarning(int pos,
String msg) {
if (nwarnings < MaxWarnings && emitWarnings) {
printRawError(pos, "warning: " + msg);
}
prompt();
nwarnings++;
errWriter.flush();
}
|
public void report(JCDiagnostic diagnostic) {
if (deferDiagnostics) {
deferredDiagnostics.add(diagnostic);
return;
}
if (expectDiagKeys != null)
expectDiagKeys.remove(diagnostic.getCode());
switch (diagnostic.getType()) {
case FRAGMENT:
throw new IllegalArgumentException();
case NOTE:
// Print out notes only when we are permitted to report warnings
// Notes are only generated at the end of a compilation, so should be small
// in number.
if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
writeDiagnostic(diagnostic);
}
break;
case WARNING:
if (emitWarnings || diagnostic.isMandatory()) {
if (nwarnings < MaxWarnings) {
writeDiagnostic(diagnostic);
nwarnings++;
}
}
break;
case ERROR:
if (nerrors < MaxErrors
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
writeDiagnostic(diagnostic);
nerrors++;
}
break;
}
}
Common diagnostic handling.
The diagnostic is counted, and depending on the options and how many diagnostics have been
reported so far, the diagnostic may be handed off to writeDiagnostic. |
public void reportDeferredDiagnostics() {
reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
}
Report all deferred diagnostics, and clear the deferDiagnostics flag. |
public void reportDeferredDiagnostics(Set<Kind> kinds) {
deferDiagnostics = false;
JCDiagnostic d;
while ((d = deferredDiagnostics.poll()) != null) {
if (kinds.contains(d.getKind()))
report(d);
}
}
Report selected deferred diagnostics, and clear the deferDiagnostics flag. |
public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
this.diagFormatter = diagFormatter;
}
Set the current diagnostic formatter. |
public void setEndPosTable(JavaFileObject name,
Map<JCTree, Integer> table) {
name.getClass(); // null check
getSource(name).setEndPosTable(table);
}
|
protected boolean shouldReport(JavaFileObject file,
int pos) {
if (multipleErrors || file == null)
return true;
Pair< JavaFileObject,Integer > coords = new Pair< JavaFileObject,Integer >(file, pos);
boolean shouldReport = !recorded.contains(coords);
if (shouldReport)
recorded.add(coords);
return shouldReport;
}
Returns true if an error needs to be reported for a given
source name and pos. |
public void strictWarning(DiagnosticPosition pos,
String key,
Object args) {
writeDiagnostic(diags.warning(source, pos, key, args));
nwarnings++;
}
Report a warning that cannot be suppressed. |
protected void writeDiagnostic(JCDiagnostic diag) {
if (diagListener != null) {
diagListener.report(diag);
return;
}
PrintWriter writer = getWriterForDiagnosticType(diag.getType());
printLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
if (promptOnError) {
switch (diag.getType()) {
case ERROR:
case WARNING:
prompt();
}
}
if (dumpOnError)
new RuntimeException().printStackTrace(writer);
writer.flush();
}
|