|
Java example source code file (Log.java)
The Log.java Java example source code
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.javac.util;
import java.io.*;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.api.DiagnosticFormatter;
import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
import static com.sun.tools.javac.main.Option.*;
/** A class for error logs. Reports errors and warnings, and
* keeps track of error numbers and positions.
*
* <p>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class Log extends AbstractLog {
/** The context key for the log. */
public static final Context.Key<Log> logKey
= new Context.Key<Log>();
/** The context key for the output PrintWriter. */
public static final Context.Key<PrintWriter> outKey =
new Context.Key<PrintWriter>();
/* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */
public enum PrefixKind {
JAVAC("javac."),
COMPILER_MISC("compiler.misc.");
PrefixKind(String v) {
value = v;
}
public String key(String k) {
return value + k;
}
final String value;
}
/**
* DiagnosticHandler's provide the initial handling for diagnostics.
* When a diagnostic handler is created and has been initialized, it
* should install itself as the current diagnostic handler. When a
* client has finished using a handler, the client should call
* {@code log.removeDiagnosticHandler();}
*
* Note that javax.tools.DiagnosticListener (if set) is called later in the
* diagnostic pipeline.
*/
public static abstract class DiagnosticHandler {
/**
* The previously installed diagnostic handler.
*/
protected DiagnosticHandler prev;
/**
* Install this diagnostic handler as the current one,
* recording the previous one.
*/
protected void install(Log log) {
prev = log.diagnosticHandler;
log.diagnosticHandler = this;
}
/**
* Handle a diagnostic.
*/
public abstract void report(JCDiagnostic diag);
}
/**
* A DiagnosticHandler that discards all diagnostics.
*/
public static class DiscardDiagnosticHandler extends DiagnosticHandler {
public DiscardDiagnosticHandler(Log log) {
install(log);
}
public void report(JCDiagnostic diag) { }
}
/**
* A DiagnosticHandler that can defer some or all diagnostics,
* by buffering them for later examination and/or reporting.
* If a diagnostic is not deferred, or is subsequently reported
* with reportAllDiagnostics(), it will be reported to the previously
* active diagnostic handler.
*/
public static class DeferredDiagnosticHandler extends DiagnosticHandler {
private Queue<JCDiagnostic> deferred = new ListBuffer<>();
private final Filter<JCDiagnostic> filter;
public DeferredDiagnosticHandler(Log log) {
this(log, null);
}
public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
this.filter = filter;
install(log);
}
public void report(JCDiagnostic diag) {
if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) &&
(filter == null || filter.accepts(diag))) {
deferred.add(diag);
} else {
prev.report(diag);
}
}
public Queue<JCDiagnostic> getDiagnostics() {
return deferred;
}
/** Report all deferred diagnostics. */
public void reportDeferredDiagnostics() {
reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
}
/** Report selected deferred diagnostics. */
public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
JCDiagnostic d;
while ((d = deferred.poll()) != null) {
if (kinds.contains(d.getKind()))
prev.report(d);
}
deferred = null; // prevent accidental ongoing use
}
}
public enum WriterKind { NOTICE, WARNING, ERROR };
protected PrintWriter errWriter;
protected PrintWriter warnWriter;
protected PrintWriter noticeWriter;
/** The maximum number of errors/warnings that are reported.
*/
protected int MaxErrors;
protected int MaxWarnings;
/** Switch: prompt user on each error.
*/
public boolean promptOnError;
/** Switch: emit warning messages.
*/
public boolean emitWarnings;
/** Switch: suppress note messages.
*/
public boolean suppressNotes;
/** Print stack trace on errors?
*/
public boolean dumpOnError;
/** Print multiple errors for same source locations.
*/
public boolean multipleErrors;
/**
* Diagnostic listener, if provided through programmatic
* interface to javac (JSR 199).
*/
protected DiagnosticListener<? super JavaFileObject> diagListener;
/**
* Formatter for diagnostics.
*/
private DiagnosticFormatter<JCDiagnostic> diagFormatter;
/**
* Keys for expected diagnostics.
*/
public Set<String> expectDiagKeys;
/**
* Set to true if a compressed diagnostic is reported
*/
public boolean compressedOutput;
/**
* JavacMessages object used for localization.
*/
private JavacMessages messages;
/**
* Handler for initial dispatch of diagnostics.
*/
private DiagnosticHandler diagnosticHandler;
/** Construct a log with given I/O redirections.
*/
protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
super(JCDiagnostic.Factory.instance(context));
context.put(logKey, this);
this.errWriter = errWriter;
this.warnWriter = warnWriter;
this.noticeWriter = noticeWriter;
@SuppressWarnings("unchecked") // FIXME
DiagnosticListener<? super JavaFileObject> dl =
context.get(DiagnosticListener.class);
this.diagListener = dl;
diagnosticHandler = new DefaultDiagnosticHandler();
messages = JavacMessages.instance(context);
messages.add(Main.javacBundleName);
final Options options = Options.instance(context);
initOptions(options);
options.addListener(new Runnable() {
public void run() {
initOptions(options);
}
});
}
// where
private void initOptions(Options options) {
this.dumpOnError = options.isSet(DOE);
this.promptOnError = options.isSet(PROMPT);
this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
this.suppressNotes = options.isSet("suppressNotes");
this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
boolean rawDiagnostics = options.isSet("rawDiagnostics");
this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
new BasicDiagnosticFormatter(options, messages);
String ek = options.get("expectKeys");
if (ek != null)
expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
}
private int getIntOption(Options options, Option option, int defaultValue) {
String s = options.get(option);
try {
if (s != null) {
int n = Integer.parseInt(s);
return (n <= 0 ? Integer.MAX_VALUE : n);
}
} catch (NumberFormatException e) {
// silently ignore ill-formed numbers
}
return defaultValue;
}
/** Default value for -Xmaxerrs.
*/
protected int getDefaultMaxErrors() {
return 100;
}
/** Default value for -Xmaxwarns.
*/
protected int getDefaultMaxWarnings() {
return 100;
}
/** The default writer for diagnostics
*/
static PrintWriter defaultWriter(Context context) {
PrintWriter result = context.get(outKey);
if (result == null)
context.put(outKey, result = new PrintWriter(System.err));
return result;
}
/** Construct a log with default settings.
*/
protected Log(Context context) {
this(context, defaultWriter(context));
}
/** Construct a log with all output redirected.
*/
protected Log(Context context, PrintWriter defaultWriter) {
this(context, defaultWriter, defaultWriter, defaultWriter);
}
/** Get the Log instance for this context. */
public static Log instance(Context context) {
Log instance = context.get(logKey);
if (instance == null)
instance = new Log(context);
return instance;
}
/** The number of errors encountered so far.
*/
public int nerrors = 0;
/** The number of warnings encountered so far.
*/
public int nwarnings = 0;
/** A set of all errors generated so far. This is used to avoid printing an
* error message more than once. For each error, a pair consisting of the
* source file name and source code position of the error is added to the set.
*/
private Set<Pair
Other Java examples (source code examples)Here is a short list of links related to this Java Log.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.