diff -r 32e309883246 -r 4cdaaf4c5dca src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java --- a/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java Fri Nov 07 11:45:49 2008 -0800 +++ b/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java Wed Nov 12 14:17:03 2008 +0000 @@ -27,13 +27,13 @@ import java.util.Collection; import java.util.Locale; import javax.tools.JavaFileObject; -import java.util.ResourceBundle; import com.sun.tools.javac.api.DiagnosticFormatter; import com.sun.tools.javac.api.Formattable; import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind; import com.sun.tools.javac.file.JavacFileManager; import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; +import static com.sun.tools.javac.util.LayoutCharacters.*; /** * This abstract class provides a basic implementation of the functionalities that should be provided @@ -72,8 +72,13 @@ public String formatMessage(JCDiagnostic d, Locale l) { //this code should rely on the locale settings but it's not! See RFE 6443132 + StringBuilder buf = new StringBuilder(); Collection args = formatArguments(d, l); - return localize(l, d.getCode(), args.toArray()); + buf.append(localize(l, d.getCode(), args.toArray())); + if (d.isMultiline()) { + buf.append(formatSubdiagnostics(d, l)); + } + return buf.toString(); } public String formatKind(JCDiagnostic d, Locale l) { @@ -165,6 +170,23 @@ 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 string representation of the subdiagnostics + */ + protected String formatSubdiagnostics(JCDiagnostic d, Locale l) { + StringBuilder buf = new StringBuilder(); + for (JCDiagnostic d2 : d.getSubdiagnostics()) { + buf.append('\n'); + String subdiagMsg = format(d2, l); + buf.append(indent(subdiagMsg, DiagInc)); + } + return buf.toString(); + } + /** Format the faulty source code line and point to the error. * @param d The diagnostic for which the error line should be printed */ @@ -201,4 +223,44 @@ public boolean displaySource(JCDiagnostic d) { return showSource && d.getType() != FRAGMENT; } + + /** + * 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(); + } }