AbstractDiagnosticFormatter.java revision 237
1N/A/*
1N/A * Copyright 2008-2009 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.Arrays;
1N/Aimport java.util.Collection;
1N/Aimport java.util.EnumSet;
1N/Aimport java.util.HashMap;
1N/Aimport java.util.Locale;
1N/Aimport java.util.Map;
1N/Aimport java.util.Set;
1N/Aimport javax.tools.JavaFileObject;
1N/A
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter;
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter.Configuration.MultilineLimit;
1N/Aimport com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
1N/Aimport com.sun.tools.javac.api.Formattable;
1N/Aimport com.sun.tools.javac.code.Printer;
1N/Aimport com.sun.tools.javac.code.Symbol;
1N/Aimport com.sun.tools.javac.code.Type;
1N/Aimport com.sun.tools.javac.code.Type.CapturedType;
1N/Aimport com.sun.tools.javac.file.JavacFileManager;
1N/A
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
1N/A /**
1N/A * Configuration object used by this formatter
1N/A */
1N/A private SimpleConfiguration config;
1N/A
1N/A /**
1N/A * Current depth level of the disgnostic being formatted
1N/A * (!= 0 for subdiagnostics)
1N/A */
1N/A protected int depth = 0;
1N/A
1N/A /**
1N/A * Printer instance to be used for formatting types/symbol
1N/A */
1N/A protected Printer printer;
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, SimpleConfiguration config) {
1N/A this.messages = messages;
1N/A this.config = config;
1N/A this.printer = new FormatterPrinter();
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 @Override
1N/A public String format(JCDiagnostic d, Locale locale) {
1N/A printer = new FormatterPrinter();
1N/A return formatDiagnostic(d, locale);
1N/A }
1N/A
1N/A abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
1N/A
public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
assert (d.getPosition() != Position.NOPOS);
return String.valueOf(getPosition(d, pk));
}
//where
private long getPosition(JCDiagnostic d, PositionKind pk) {
switch (pk) {
case START: return d.getIntStartPosition();
case END: return d.getIntEndPosition();
case LINE: return d.getLineNumber();
case COLUMN: return d.getColumnNumber();
case OFFSET: return d.getIntPosition();
default:
throw new AssertionError("Unknown diagnostic position: " + pk);
}
}
public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
assert (d.getSource() != null);
return fullname ? d.getSourceName() : d.getSource().getName();
}
/**
* 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) {
String s = null;
depth++;
try {
s = formatMessage((JCDiagnostic)arg, l);
}
finally {
depth--;
}
return s;
}
else if (arg instanceof Iterable<?>) {
return formatIterable(d, (Iterable<?>)arg, l);
}
else if (arg instanceof Type) {
return printer.visit((Type)arg, l);
}
else if (arg instanceof Symbol) {
return printer.visit((Symbol)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 all the subdiagnostics attached to a given diagnostic.
*
* @param d diagnostic whose subdiagnostics are to be formatted
* @param l locale object to be used for i18n
* @return list of all string representations of the subdiagnostics
*/
protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) {
List<String> subdiagnostics = List.nil();
int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
if (maxDepth == -1 || depth < maxDepth) {
depth++;
try {
int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
int count = 0;
for (JCDiagnostic d2 : d.getSubdiagnostics()) {
if (maxCount == -1 || count < maxCount) {
subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
count++;
}
else
break;
}
}
finally {
depth--;
}
}
return subdiagnostics;
}
/**
* Format a subdiagnostics attached to a given diagnostic.
*
* @param parent multiline diagnostic whose subdiagnostics is to be formatted
* @param sub subdiagnostic to be formatted
* @param l locale object to be used for i18n
* @return string representation of the subdiagnostics
*/
protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) {
return formatMessage(sub, l);
}
/** 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, int nSpaces) {
StringBuilder buf = new StringBuilder();
DiagnosticSource source = d.getDiagnosticSource();
int pos = d.getIntPosition();
if (d.getIntPosition() == Position.NOPOS)
throw new AssertionError();
String line = (source == null ? null : source.getLine(pos));
if (line == null)
return "";
buf.append(indent(line, nSpaces));
int col = source.getColumnNumber(pos, false);
if (config.isCaretEnabled()) {
buf.append("\n");
for (int i = 0; i < col - 1; i++) {
buf.append((line.charAt(i) == '\t') ? "\t" : " ");
}
buf.append(indent("^", nSpaces));
}
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 config.getVisible().contains(DiagnosticPart.SOURCE) &&
d.getType() != FRAGMENT &&
d.getIntPosition() != Position.NOPOS;
}
/**
* Creates a string with a given amount of empty spaces. Useful for
* indenting the text of a diagnostic message.
*
* @param nSpaces the amount of spaces to be added to the result string
* @return the indentation string
*/
protected String indentString(int nSpaces) {
String spaces = " ";
if (nSpaces <= spaces.length())
return spaces.substring(0, nSpaces);
else {
StringBuilder buf = new StringBuilder();
for (int i = 0 ; i < nSpaces ; i++)
buf.append(" ");
return buf.toString();
}
}
/**
* Indent a string by prepending a given amount of empty spaces to each line
* of the string.
*
* @param s the string to be indented
* @param nSpaces the amount of spaces that should be prepended to each line
* of the string
* @return an indented string
*/
protected String indent(String s, int nSpaces) {
String indent = indentString(nSpaces);
StringBuilder buf = new StringBuilder();
String nl = "";
for (String line : s.split("\n")) {
buf.append(nl);
buf.append(indent + line);
nl = "\n";
}
return buf.toString();
}
public SimpleConfiguration getConfiguration() {
return config;
}
static public class SimpleConfiguration implements Configuration {
protected Map<MultilineLimit, Integer> multilineLimits;
protected EnumSet<DiagnosticPart> visibleParts;
protected boolean caretEnabled;
public SimpleConfiguration(Set<DiagnosticPart> parts) {
multilineLimits = new HashMap<MultilineLimit, Integer>();
setVisible(parts);
setMultilineLimit(MultilineLimit.DEPTH, -1);
setMultilineLimit(MultilineLimit.LENGTH, -1);
setCaretEnabled(true);
}
@SuppressWarnings("fallthrough")
public SimpleConfiguration(Options options, Set<DiagnosticPart> parts) {
this(parts);
String showSource = null;
if ((showSource = options.get("showSource")) != null) {
if (showSource.equals("true"))
visibleParts.add(DiagnosticPart.SOURCE);
else if (showSource.equals("false"))
visibleParts.remove(DiagnosticPart.SOURCE);
}
String diagOpts = options.get("diags");
if (diagOpts != null) {//override -XDshowSource
Collection<String> args = Arrays.asList(diagOpts.split(","));
if (args.contains("short")) {
visibleParts.remove(DiagnosticPart.DETAILS);
visibleParts.remove(DiagnosticPart.SUBDIAGNOSTICS);
}
if (args.contains("source"))
visibleParts.add(DiagnosticPart.SOURCE);
if (args.contains("-source"))
visibleParts.remove(DiagnosticPart.SOURCE);
}
String multiPolicy = null;
if ((multiPolicy = options.get("multilinePolicy")) != null) {
if (multiPolicy.equals("disabled"))
visibleParts.remove(DiagnosticPart.SUBDIAGNOSTICS);
else if (multiPolicy.startsWith("limit:")) {
String limitString = multiPolicy.substring("limit:".length());
String[] limits = limitString.split(":");
try {
switch (limits.length) {
case 2: {
if (!limits[1].equals("*"))
setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1]));
}
case 1: {
if (!limits[0].equals("*"))
setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0]));
}
}
}
catch(NumberFormatException ex) {
setMultilineLimit(MultilineLimit.DEPTH, -1);
setMultilineLimit(MultilineLimit.LENGTH, -1);
}
}
}
String showCaret = null;
if (((showCaret = options.get("showCaret")) != null) &&
showCaret.equals("false"))
setCaretEnabled(false);
else
setCaretEnabled(true);
}
public int getMultilineLimit(MultilineLimit limit) {
return multilineLimits.get(limit);
}
public EnumSet<DiagnosticPart> getVisible() {
return EnumSet.copyOf(visibleParts);
}
public void setMultilineLimit(MultilineLimit limit, int value) {
multilineLimits.put(limit, value < -1 ? -1 : value);
}
public void setVisible(Set<DiagnosticPart> diagParts) {
visibleParts = EnumSet.copyOf(diagParts);
}
/**
* Shows a '^' sign under the source line displayed by the formatter
* (if applicable).
*
* @param caretEnabled if true enables caret
*/
public void setCaretEnabled(boolean caretEnabled) {
this.caretEnabled = caretEnabled;
}
/**
* Tells whether the caret display is active or not.
*
* @param caretEnabled if true the caret is enabled
*/
public boolean isCaretEnabled() {
return caretEnabled;
}
}
/**
* An enhanced printer for formatting types/symbols used by
* AbstractDiagnosticFormatter. Provides alternate numbering of captured
* types (they are numbered starting from 1 on each new diagnostic, instead
* of relying on the underlying hashcode() method which generates unstable
* output). Also detects cycles in wildcard messages (e.g. if the wildcard
* type referred by a given captured type C contains C itself) which might
* lead to infinite loops.
*/
protected class FormatterPrinter extends Printer {
List<Type> allCaptured = List.nil();
List<Type> seenCaptured = List.nil();
@Override
protected String localize(Locale locale, String key, Object... args) {
return AbstractDiagnosticFormatter.this.localize(locale, key, args);
}
@Override
public String visitCapturedType(CapturedType t, Locale locale) {
if (seenCaptured.contains(t))
return localize(locale, "compiler.misc.type.captureof.1",
allCaptured.indexOf(t) + 1);
else {
try {
seenCaptured = seenCaptured.prepend(t);
allCaptured = allCaptured.append(t);
return localize(locale, "compiler.misc.type.captureof",
allCaptured.indexOf(t) + 1,
visit(t.wildcard, locale));
}
finally {
seenCaptured = seenCaptured.tail;
}
}
}
}
}