AbstractDiagnosticFormatter.java revision 136
1N/A/*
1N/A * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
1N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1N/A *
1N/A * This code is free software; you can redistribute it and/or modify it
1N/A * under the terms of the GNU General Public License version 2 only, as
1N/A * published by the Free Software Foundation. Sun designates this
1N/A * particular file as subject to the "Classpath" exception as provided
1N/A * by Sun in the LICENSE file that accompanied this code.
1N/A *
1N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1N/A * version 2 for more details (a copy is included in the LICENSE file that
1N/A * accompanied this code).
1N/A *
1N/A * You should have received a copy of the GNU General Public License version
1N/A * 2 along with this work; if not, write to the Free Software Foundation,
1N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1N/A *
1N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1N/A * have any questions.
1N/A */
1N/Apackage com.sun.tools.javac.util;
1N/A
1N/Aimport java.util.Collection;
1N/Aimport java.util.Locale;
1N/Aimport javax.tools.JavaFileObject;
1N/Aimport java.util.ResourceBundle;
1N/A
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter;
1N/Aimport com.sun.tools.javac.api.Formattable;
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
1N/Aimport com.sun.tools.javac.file.JavacFileManager;
1N/Aimport static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
1N/A
1N/A/**
1N/A * This abstract class provides a basic implementation of the functionalities that should be provided
1N/A * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
1N/A *
1N/A * <ul>
1N/A * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
1N/A * Those implementations are specifically targeting JCDiagnostic objects.
1N/A * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
1N/A * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
1N/A * <ul>
1N/A *
1N/A */
1N/Apublic abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
1N/A
1N/A /**
1N/A * JavacMessages object used by this formatter for i18n
1N/A */
1N/A protected JavacMessages messages;
1N/A protected boolean showSource;
1N/A
1N/A /**
1N/A * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
1N/A * @param messages
1N/A */
1N/A protected AbstractDiagnosticFormatter(JavacMessages messages, Options options, boolean showSource) {
1N/A this.messages = messages;
1N/A this.showSource = options.get("showSource") == null ? showSource :
1N/A options.get("showSource").equals("true");
1N/A }
1N/A
1N/A protected AbstractDiagnosticFormatter(JavacMessages messages, boolean showSource) {
1N/A this.messages = messages;
1N/A this.showSource = showSource;
1N/A }
1N/A
1N/A public String formatMessage(JCDiagnostic d, Locale l) {
1N/A //this code should rely on the locale settings but it's not! See RFE 6443132
1N/A Collection<String> args = formatArguments(d, l);
1N/A return localize(l, d.getCode(), args.toArray());
1N/A }
1N/A
1N/A public String formatKind(JCDiagnostic d, Locale l) {
1N/A switch (d.getType()) {
1N/A case FRAGMENT: return "";
1N/A case NOTE: return localize(l, "compiler.note.note");
1N/A case WARNING: return localize(l, "compiler.warn.warning");
1N/A case ERROR: return localize(l, "compiler.err.error");
1N/A default:
1N/A throw new AssertionError("Unknown diagnostic type: " + d.getType());
1N/A }
1N/A }
1N/A
1N/A public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
1N/A assert (d.getPosition() != Position.NOPOS);
1N/A return String.valueOf(getPosition(d, pk));
1N/A }
1N/A //WHERE
1N/A public long getPosition(JCDiagnostic d, PositionKind pk) {
1N/A switch (pk) {
1N/A case START: return d.getIntStartPosition();
1N/A case END: return d.getIntEndPosition();
1N/A case LINE: return d.getLineNumber();
1N/A case COLUMN: return d.getColumnNumber();
1N/A case OFFSET: return d.getIntPosition();
1N/A default:
1N/A throw new AssertionError("Unknown diagnostic position: " + pk);
1N/A }
1N/A }
1N/A
1N/A public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
1N/A assert (d.getSource() != null);
1N/A return fullname ? d.getSourceName() : d.getSource().getName();
1N/A }
1N/A
1N/A /**
* Format the arguments of a given diagnostic.
*
* @param d diagnostic whose arguments are to be formatted
* @param l locale object to be used for i18n
* @return a Collection whose elements are the formatted arguments of the diagnostic
*/
protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
ListBuffer<String> buf = new ListBuffer<String>();
for (Object o : d.getArgs()) {
buf.append(formatArgument(d, o, l));
}
return buf.toList();
}
/**
* Format a single argument of a given diagnostic.
*
* @param d diagnostic whose argument is to be formatted
* @param arg argument to be formatted
* @param l locale object to be used for i18n
* @return string representation of the diagnostic argument
*/
protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
if (arg instanceof JCDiagnostic)
return format((JCDiagnostic)arg, l);
else if (arg instanceof Iterable<?>) {
return formatIterable(d, (Iterable<?>)arg, l);
}
else if (arg instanceof JavaFileObject)
return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
else if (arg instanceof Formattable)
return ((Formattable)arg).toString(l, messages);
else
return String.valueOf(arg);
}
/**
* Format an iterable argument of a given diagnostic.
*
* @param d diagnostic whose argument is to be formatted
* @param it iterable argument to be formatted
* @param l locale object to be used for i18n
* @return string representation of the diagnostic iterable argument
*/
protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
StringBuilder sbuf = new StringBuilder();
String sep = "";
for (Object o : it) {
sbuf.append(sep);
sbuf.append(formatArgument(d, o, l));
sep = ",";
}
return sbuf.toString();
}
/** Format the faulty source code line and point to the error.
* @param d The diagnostic for which the error line should be printed
*/
protected String formatSourceLine(JCDiagnostic d) {
StringBuilder buf = new StringBuilder();
DiagnosticSource source = d.getDiagnosticSource();
int pos = d.getIntPosition();
if (d.getIntPosition() != Position.NOPOS) {
String line = (source == null ? null : source.getLine(pos));
if (line == null)
return "";
buf.append(line+"\n");
int col = source.getColumnNumber(pos, false);
for (int i = 0; i < col - 1; i++) {
buf.append((line.charAt(i) == '\t') ? "\t" : " ");
}
buf.append("^");
}
return buf.toString();
}
/**
* Converts a String into a locale-dependent representation accordingly to a given locale
*
* @param l locale object to be used for i18n
* @param key locale-independent key used for looking up in a resource file
* @param args localization arguments
* @return a locale-dependent string
*/
protected String localize(Locale l, String key, Object... args) {
return messages.getLocalizedString(l, key, args);
}
public boolean displaySource(JCDiagnostic d) {
return showSource && d.getType() != FRAGMENT;
}
}