mcimadamore@83: *
jjg@581: * This is NOT part of any supported API.
jjg@333: * If you write code that depends on this, you do so at your own risk.
jjg@333: * This code and its internal interfaces are subject to change or
jjg@333: * deletion without notice.
mcimadamore@83: */
mcimadamore@83: public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter {
mcimadamore@83:
mcimadamore@83: /**
mcimadamore@221: * JavacMessages object used by this formatter for i18n.
mcimadamore@83: */
mcimadamore@136: protected JavacMessages messages;
mcimadamore@238:
mcimadamore@238: /**
mcimadamore@238: * Configuration object used by this formatter
mcimadamore@238: */
mcimadamore@221: private SimpleConfiguration config;
mcimadamore@238:
mcimadamore@238: /**
mcimadamore@238: * Current depth level of the disgnostic being formatted
mcimadamore@238: * (!= 0 for subdiagnostics)
mcimadamore@238: */
mcimadamore@221: protected int depth = 0;
mcimadamore@83:
mcimadamore@83: /**
mcimadamore@288: * All captured types that have been encountered during diagnostic formatting.
mcimadamore@288: * This info is used by the FormatterPrinter in order to print friendly unique
mcimadamore@288: * ids for captured types
mcimadamore@238: */
mcimadamore@288: private List allCaptured = List.nil();
mcimadamore@238:
mcimadamore@238: /**
mcimadamore@221: * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object.
mcimadamore@83: * @param messages
mcimadamore@83: */
mcimadamore@221: protected AbstractDiagnosticFormatter(JavacMessages messages, SimpleConfiguration config) {
mcimadamore@83: this.messages = messages;
mcimadamore@221: this.config = config;
mcimadamore@83: }
mcimadamore@83:
mcimadamore@83: public String formatKind(JCDiagnostic d, Locale l) {
mcimadamore@83: switch (d.getType()) {
mcimadamore@83: case FRAGMENT: return "";
mcimadamore@83: case NOTE: return localize(l, "compiler.note.note");
mcimadamore@83: case WARNING: return localize(l, "compiler.warn.warning");
mcimadamore@83: case ERROR: return localize(l, "compiler.err.error");
mcimadamore@83: default:
mcimadamore@83: throw new AssertionError("Unknown diagnostic type: " + d.getType());
mcimadamore@83: }
mcimadamore@83: }
mcimadamore@83:
mcimadamore@238: @Override
mcimadamore@238: public String format(JCDiagnostic d, Locale locale) {
mcimadamore@288: allCaptured = List.nil();
mcimadamore@238: return formatDiagnostic(d, locale);
mcimadamore@238: }
mcimadamore@238:
mcimadamore@304: protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
mcimadamore@238:
mcimadamore@83: public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
jjg@816: Assert.check(d.getPosition() != Position.NOPOS);
mcimadamore@83: return String.valueOf(getPosition(d, pk));
mcimadamore@83: }
mcimadamore@221: //where
mcimadamore@221: private long getPosition(JCDiagnostic d, PositionKind pk) {
mcimadamore@83: switch (pk) {
mcimadamore@83: case START: return d.getIntStartPosition();
mcimadamore@83: case END: return d.getIntEndPosition();
mcimadamore@83: case LINE: return d.getLineNumber();
mcimadamore@83: case COLUMN: return d.getColumnNumber();
mcimadamore@83: case OFFSET: return d.getIntPosition();
mcimadamore@83: default:
mcimadamore@83: throw new AssertionError("Unknown diagnostic position: " + pk);
mcimadamore@83: }
mcimadamore@83: }
mcimadamore@83:
mcimadamore@100: public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
jjg@415: JavaFileObject fo = d.getSource();
jjg@415: if (fo == null)
jjg@415: throw new IllegalArgumentException(); // d should have source set
jjg@415: if (fullname)
jjg@415: return fo.getName();
jjg@415: else if (fo instanceof BaseFileObject)
jjg@415: return ((BaseFileObject) fo).getShortName();
jjg@415: else
jjg@415: return BaseFileObject.getSimpleName(fo);
mcimadamore@83: }
mcimadamore@83:
mcimadamore@83: /**
mcimadamore@83: * Format the arguments of a given diagnostic.
mcimadamore@83: *
mcimadamore@83: * @param d diagnostic whose arguments are to be formatted
mcimadamore@83: * @param l locale object to be used for i18n
mcimadamore@83: * @return a Collection whose elements are the formatted arguments of the diagnostic
mcimadamore@83: */
mcimadamore@83: protected Collection formatArguments(JCDiagnostic d, Locale l) {
mcimadamore@83: ListBuffer buf = new ListBuffer();
mcimadamore@83: for (Object o : d.getArgs()) {
mcimadamore@83: buf.append(formatArgument(d, o, l));
mcimadamore@83: }
mcimadamore@83: return buf.toList();
mcimadamore@83: }
mcimadamore@83:
mcimadamore@83: /**
mcimadamore@83: * Format a single argument of a given diagnostic.
mcimadamore@83: *
mcimadamore@83: * @param d diagnostic whose argument is to be formatted
mcimadamore@83: * @param arg argument to be formatted
mcimadamore@83: * @param l locale object to be used for i18n
mcimadamore@83: * @return string representation of the diagnostic argument
mcimadamore@83: */
mcimadamore@83: protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
mcimadamore@221: if (arg instanceof JCDiagnostic) {
mcimadamore@221: String s = null;
mcimadamore@221: depth++;
mcimadamore@221: try {
mcimadamore@221: s = formatMessage((JCDiagnostic)arg, l);
mcimadamore@221: }
mcimadamore@221: finally {
mcimadamore@221: depth--;
mcimadamore@221: }
mcimadamore@221: return s;
mcimadamore@221: }
mcimadamore@83: else if (arg instanceof Iterable>) {
mcimadamore@83: return formatIterable(d, (Iterable>)arg, l);
mcimadamore@83: }
mcimadamore@238: else if (arg instanceof Type) {
mcimadamore@238: return printer.visit((Type)arg, l);
mcimadamore@238: }
mcimadamore@238: else if (arg instanceof Symbol) {
mcimadamore@238: return printer.visit((Symbol)arg, l);
mcimadamore@238: }
mcimadamore@238: else if (arg instanceof JavaFileObject) {
jjg@415: return ((JavaFileObject)arg).getName();
mcimadamore@238: }
mcimadamore@238: else if (arg instanceof Formattable) {
mcimadamore@136: return ((Formattable)arg).toString(l, messages);
mcimadamore@238: }
mcimadamore@238: else {
mcimadamore@83: return String.valueOf(arg);
mcimadamore@238: }
mcimadamore@83: }
mcimadamore@83:
mcimadamore@83: /**
mcimadamore@83: * Format an iterable argument of a given diagnostic.
mcimadamore@83: *
mcimadamore@83: * @param d diagnostic whose argument is to be formatted
mcimadamore@83: * @param it iterable argument to be formatted
mcimadamore@83: * @param l locale object to be used for i18n
mcimadamore@83: * @return string representation of the diagnostic iterable argument
mcimadamore@83: */
mcimadamore@83: protected String formatIterable(JCDiagnostic d, Iterable> it, Locale l) {
mcimadamore@83: StringBuilder sbuf = new StringBuilder();
mcimadamore@83: String sep = "";
mcimadamore@83: for (Object o : it) {
mcimadamore@83: sbuf.append(sep);
mcimadamore@83: sbuf.append(formatArgument(d, o, l));
mcimadamore@83: sep = ",";
mcimadamore@83: }
mcimadamore@83: return sbuf.toString();
mcimadamore@83: }
mcimadamore@83:
mcimadamore@168: /**
mcimadamore@221: * Format all the subdiagnostics attached to a given diagnostic.
mcimadamore@168: *
mcimadamore@168: * @param d diagnostic whose subdiagnostics are to be formatted
mcimadamore@168: * @param l locale object to be used for i18n
mcimadamore@221: * @return list of all string representations of the subdiagnostics
mcimadamore@221: */
mcimadamore@221: protected List formatSubdiagnostics(JCDiagnostic d, Locale l) {
mcimadamore@221: List subdiagnostics = List.nil();
mcimadamore@221: int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
mcimadamore@221: if (maxDepth == -1 || depth < maxDepth) {
mcimadamore@221: depth++;
mcimadamore@221: try {
mcimadamore@221: int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
mcimadamore@221: int count = 0;
mcimadamore@221: for (JCDiagnostic d2 : d.getSubdiagnostics()) {
mcimadamore@221: if (maxCount == -1 || count < maxCount) {
mcimadamore@221: subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
mcimadamore@221: count++;
mcimadamore@221: }
mcimadamore@221: else
mcimadamore@221: break;
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: finally {
mcimadamore@221: depth--;
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: return subdiagnostics;
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: /**
mcimadamore@221: * Format a subdiagnostics attached to a given diagnostic.
mcimadamore@221: *
mcimadamore@221: * @param parent multiline diagnostic whose subdiagnostics is to be formatted
mcimadamore@221: * @param sub subdiagnostic to be formatted
mcimadamore@221: * @param l locale object to be used for i18n
mcimadamore@168: * @return string representation of the subdiagnostics
mcimadamore@168: */
mcimadamore@221: protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) {
mcimadamore@221: return formatMessage(sub, l);
mcimadamore@168: }
mcimadamore@168:
mcimadamore@137: /** Format the faulty source code line and point to the error.
mcimadamore@137: * @param d The diagnostic for which the error line should be printed
mcimadamore@137: */
mcimadamore@221: protected String formatSourceLine(JCDiagnostic d, int nSpaces) {
mcimadamore@137: StringBuilder buf = new StringBuilder();
mcimadamore@137: DiagnosticSource source = d.getDiagnosticSource();
mcimadamore@137: int pos = d.getIntPosition();
mcimadamore@221: if (d.getIntPosition() == Position.NOPOS)
mcimadamore@221: throw new AssertionError();
mcimadamore@221: String line = (source == null ? null : source.getLine(pos));
mcimadamore@221: if (line == null)
mcimadamore@221: return "";
mcimadamore@221: buf.append(indent(line, nSpaces));
mcimadamore@221: int col = source.getColumnNumber(pos, false);
mcimadamore@221: if (config.isCaretEnabled()) {
mcimadamore@221: buf.append("\n");
mcimadamore@137: for (int i = 0; i < col - 1; i++) {
mcimadamore@137: buf.append((line.charAt(i) == '\t') ? "\t" : " ");
mcimadamore@137: }
mcimadamore@221: buf.append(indent("^", nSpaces));
mcimadamore@221: }
mcimadamore@221: return buf.toString();
mcimadamore@137: }
mcimadamore@137:
jjg@612: protected String formatLintCategory(JCDiagnostic d, Locale l) {
jjg@612: LintCategory lc = d.getLintCategory();
jjg@612: if (lc == null)
jjg@612: return "";
jjg@612: return localize(l, "compiler.warn.lintOption", lc.option);
jjg@612: }
jjg@612:
mcimadamore@83: /**
mcimadamore@221: * Converts a String into a locale-dependent representation accordingly to a given locale.
mcimadamore@83: *
mcimadamore@83: * @param l locale object to be used for i18n
mcimadamore@83: * @param key locale-independent key used for looking up in a resource file
mcimadamore@83: * @param args localization arguments
mcimadamore@83: * @return a locale-dependent string
mcimadamore@83: */
mcimadamore@83: protected String localize(Locale l, String key, Object... args) {
mcimadamore@136: return messages.getLocalizedString(l, key, args);
mcimadamore@83: }
mcimadamore@137:
mcimadamore@137: public boolean displaySource(JCDiagnostic d) {
mcimadamore@221: return config.getVisible().contains(DiagnosticPart.SOURCE) &&
mcimadamore@221: d.getType() != FRAGMENT &&
mcimadamore@221: d.getIntPosition() != Position.NOPOS;
mcimadamore@137: }
mcimadamore@168:
mcimadamore@288: public boolean isRaw() {
mcimadamore@288: return false;
mcimadamore@288: }
mcimadamore@288:
mcimadamore@168: /**
mcimadamore@168: * Creates a string with a given amount of empty spaces. Useful for
mcimadamore@168: * indenting the text of a diagnostic message.
mcimadamore@168: *
mcimadamore@168: * @param nSpaces the amount of spaces to be added to the result string
mcimadamore@168: * @return the indentation string
mcimadamore@168: */
mcimadamore@168: protected String indentString(int nSpaces) {
mcimadamore@168: String spaces = " ";
mcimadamore@168: if (nSpaces <= spaces.length())
mcimadamore@168: return spaces.substring(0, nSpaces);
mcimadamore@168: else {
mcimadamore@168: StringBuilder buf = new StringBuilder();
mcimadamore@168: for (int i = 0 ; i < nSpaces ; i++)
mcimadamore@168: buf.append(" ");
mcimadamore@168: return buf.toString();
mcimadamore@168: }
mcimadamore@168: }
mcimadamore@168:
mcimadamore@168: /**
mcimadamore@168: * Indent a string by prepending a given amount of empty spaces to each line
mcimadamore@221: * of the string.
mcimadamore@168: *
mcimadamore@168: * @param s the string to be indented
mcimadamore@168: * @param nSpaces the amount of spaces that should be prepended to each line
mcimadamore@168: * of the string
mcimadamore@168: * @return an indented string
mcimadamore@168: */
mcimadamore@168: protected String indent(String s, int nSpaces) {
mcimadamore@168: String indent = indentString(nSpaces);
mcimadamore@168: StringBuilder buf = new StringBuilder();
mcimadamore@168: String nl = "";
mcimadamore@168: for (String line : s.split("\n")) {
mcimadamore@168: buf.append(nl);
mcimadamore@168: buf.append(indent + line);
mcimadamore@168: nl = "\n";
mcimadamore@168: }
mcimadamore@168: return buf.toString();
mcimadamore@168: }
mcimadamore@221:
mcimadamore@221: public SimpleConfiguration getConfiguration() {
mcimadamore@221: return config;
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: static public class SimpleConfiguration implements Configuration {
mcimadamore@221:
mcimadamore@221: protected Map multilineLimits;
mcimadamore@221: protected EnumSet visibleParts;
mcimadamore@221: protected boolean caretEnabled;
mcimadamore@221:
mcimadamore@221: public SimpleConfiguration(Set parts) {
mcimadamore@221: multilineLimits = new HashMap();
mcimadamore@221: setVisible(parts);
mcimadamore@221: setMultilineLimit(MultilineLimit.DEPTH, -1);
mcimadamore@221: setMultilineLimit(MultilineLimit.LENGTH, -1);
mcimadamore@221: setCaretEnabled(true);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: @SuppressWarnings("fallthrough")
mcimadamore@221: public SimpleConfiguration(Options options, Set parts) {
mcimadamore@221: this(parts);
mcimadamore@221: String showSource = null;
mcimadamore@221: if ((showSource = options.get("showSource")) != null) {
mcimadamore@221: if (showSource.equals("true"))
mcimadamore@288: setVisiblePart(DiagnosticPart.SOURCE, true);
mcimadamore@221: else if (showSource.equals("false"))
mcimadamore@288: setVisiblePart(DiagnosticPart.SOURCE, false);
mcimadamore@221: }
mcimadamore@221: String diagOpts = options.get("diags");
mcimadamore@221: if (diagOpts != null) {//override -XDshowSource
mcimadamore@221: Collection args = Arrays.asList(diagOpts.split(","));
mcimadamore@221: if (args.contains("short")) {
mcimadamore@288: setVisiblePart(DiagnosticPart.DETAILS, false);
mcimadamore@288: setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
mcimadamore@221: }
mcimadamore@221: if (args.contains("source"))
mcimadamore@288: setVisiblePart(DiagnosticPart.SOURCE, true);
mcimadamore@221: if (args.contains("-source"))
mcimadamore@288: setVisiblePart(DiagnosticPart.SOURCE, false);
mcimadamore@221: }
mcimadamore@221: String multiPolicy = null;
mcimadamore@221: if ((multiPolicy = options.get("multilinePolicy")) != null) {
mcimadamore@221: if (multiPolicy.equals("disabled"))
mcimadamore@288: setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
mcimadamore@221: else if (multiPolicy.startsWith("limit:")) {
mcimadamore@221: String limitString = multiPolicy.substring("limit:".length());
mcimadamore@221: String[] limits = limitString.split(":");
mcimadamore@221: try {
mcimadamore@221: switch (limits.length) {
mcimadamore@221: case 2: {
mcimadamore@221: if (!limits[1].equals("*"))
mcimadamore@221: setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1]));
mcimadamore@221: }
mcimadamore@221: case 1: {
mcimadamore@221: if (!limits[0].equals("*"))
mcimadamore@221: setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0]));
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: catch(NumberFormatException ex) {
mcimadamore@221: setMultilineLimit(MultilineLimit.DEPTH, -1);
mcimadamore@221: setMultilineLimit(MultilineLimit.LENGTH, -1);
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@221: String showCaret = null;
mcimadamore@221: if (((showCaret = options.get("showCaret")) != null) &&
mcimadamore@221: showCaret.equals("false"))
mcimadamore@221: setCaretEnabled(false);
mcimadamore@221: else
mcimadamore@221: setCaretEnabled(true);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: public int getMultilineLimit(MultilineLimit limit) {
mcimadamore@221: return multilineLimits.get(limit);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: public EnumSet getVisible() {
mcimadamore@221: return EnumSet.copyOf(visibleParts);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: public void setMultilineLimit(MultilineLimit limit, int value) {
mcimadamore@221: multilineLimits.put(limit, value < -1 ? -1 : value);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221:
mcimadamore@221: public void setVisible(Set diagParts) {
mcimadamore@221: visibleParts = EnumSet.copyOf(diagParts);
mcimadamore@221: }
mcimadamore@221:
mcimadamore@288: public void setVisiblePart(DiagnosticPart diagParts, boolean enabled) {
mcimadamore@288: if (enabled)
mcimadamore@288: visibleParts.add(diagParts);
mcimadamore@288: else
mcimadamore@288: visibleParts.remove(diagParts);
mcimadamore@288: }
mcimadamore@288:
mcimadamore@221: /**
mcimadamore@221: * Shows a '^' sign under the source line displayed by the formatter
mcimadamore@221: * (if applicable).
mcimadamore@221: *
mcimadamore@221: * @param caretEnabled if true enables caret
mcimadamore@221: */
mcimadamore@221: public void setCaretEnabled(boolean caretEnabled) {
mcimadamore@221: this.caretEnabled = caretEnabled;
mcimadamore@221: }
mcimadamore@221:
mcimadamore@221: /**
mcimadamore@221: * Tells whether the caret display is active or not.
mcimadamore@221: *
mcimadamore@221: * @param caretEnabled if true the caret is enabled
mcimadamore@221: */
mcimadamore@221: public boolean isCaretEnabled() {
mcimadamore@221: return caretEnabled;
mcimadamore@221: }
mcimadamore@221: }
mcimadamore@238:
mcimadamore@288: public Printer getPrinter() {
mcimadamore@288: return printer;
mcimadamore@288: }
mcimadamore@288:
mcimadamore@288: public void setPrinter(Printer printer) {
mcimadamore@288: this.printer = printer;
mcimadamore@288: }
mcimadamore@288:
mcimadamore@238: /**
mcimadamore@238: * An enhanced printer for formatting types/symbols used by
mcimadamore@238: * AbstractDiagnosticFormatter. Provides alternate numbering of captured
mcimadamore@238: * types (they are numbered starting from 1 on each new diagnostic, instead
mcimadamore@238: * of relying on the underlying hashcode() method which generates unstable
mcimadamore@238: * output). Also detects cycles in wildcard messages (e.g. if the wildcard
mcimadamore@238: * type referred by a given captured type C contains C itself) which might
mcimadamore@238: * lead to infinite loops.
mcimadamore@238: */
mcimadamore@288: protected Printer printer = new Printer() {
mcimadamore@238: @Override
mcimadamore@238: protected String localize(Locale locale, String key, Object... args) {
mcimadamore@238: return AbstractDiagnosticFormatter.this.localize(locale, key, args);
mcimadamore@238: }
mcimadamore@238: @Override
mcimadamore@288: protected String capturedVarId(CapturedType t, Locale locale) {
mcimadamore@288: return "" + (allCaptured.indexOf(t) + 1);
mcimadamore@238: }
mcimadamore@296: @Override
mcimadamore@296: public String visitCapturedType(CapturedType t, Locale locale) {
mcimadamore@296: if (!allCaptured.contains(t)) {
mcimadamore@296: allCaptured = allCaptured.append(t);
mcimadamore@296: }
mcimadamore@296: return super.visitCapturedType(t, locale);
mcimadamore@296: }
mcimadamore@288: };
mcimadamore@83: }