src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java

Wed, 12 Nov 2008 14:17:03 +0000

author
mcimadamore
date
Wed, 12 Nov 2008 14:17:03 +0000
changeset 168
4cdaaf4c5dca
parent 137
e4eaddca54b7
child 221
6ada6122dd4f
permissions
-rw-r--r--

6768932: Add support for multiline diagnostics
Summary: Added basic support for multiline/tabular diagnostics
Reviewed-by: jjg

mcimadamore@83 1 /*
mcimadamore@83 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@83 7 * published by the Free Software Foundation. Sun designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
mcimadamore@83 9 * by Sun in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
mcimadamore@83 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
mcimadamore@83 22 * CA 95054 USA or visit www.sun.com if you need additional information or
mcimadamore@83 23 * have any questions.
mcimadamore@83 24 */
mcimadamore@83 25 package com.sun.tools.javac.util;
mcimadamore@83 26
mcimadamore@83 27 import java.util.Collection;
mcimadamore@83 28 import java.util.Locale;
mcimadamore@83 29 import javax.tools.JavaFileObject;
mcimadamore@83 30
mcimadamore@83 31 import com.sun.tools.javac.api.DiagnosticFormatter;
mcimadamore@83 32 import com.sun.tools.javac.api.Formattable;
mcimadamore@83 33 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
mcimadamore@83 34 import com.sun.tools.javac.file.JavacFileManager;
mcimadamore@137 35 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
mcimadamore@168 36 import static com.sun.tools.javac.util.LayoutCharacters.*;
mcimadamore@83 37
mcimadamore@83 38 /**
mcimadamore@83 39 * This abstract class provides a basic implementation of the functionalities that should be provided
mcimadamore@83 40 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
mcimadamore@83 41 *
mcimadamore@83 42 * <ul>
mcimadamore@83 43 * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
mcimadamore@83 44 * Those implementations are specifically targeting JCDiagnostic objects.
mcimadamore@83 45 * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
mcimadamore@83 46 * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
mcimadamore@83 47 * <ul>
mcimadamore@83 48 *
mcimadamore@83 49 */
mcimadamore@83 50 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
mcimadamore@83 51
mcimadamore@83 52 /**
mcimadamore@136 53 * JavacMessages object used by this formatter for i18n
mcimadamore@83 54 */
mcimadamore@136 55 protected JavacMessages messages;
mcimadamore@137 56 protected boolean showSource;
mcimadamore@83 57
mcimadamore@83 58 /**
mcimadamore@136 59 * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
mcimadamore@83 60 * @param messages
mcimadamore@83 61 */
mcimadamore@137 62 protected AbstractDiagnosticFormatter(JavacMessages messages, Options options, boolean showSource) {
mcimadamore@83 63 this.messages = messages;
mcimadamore@137 64 this.showSource = options.get("showSource") == null ? showSource :
mcimadamore@137 65 options.get("showSource").equals("true");
mcimadamore@137 66 }
mcimadamore@137 67
mcimadamore@137 68 protected AbstractDiagnosticFormatter(JavacMessages messages, boolean showSource) {
mcimadamore@137 69 this.messages = messages;
mcimadamore@137 70 this.showSource = showSource;
mcimadamore@83 71 }
mcimadamore@83 72
mcimadamore@83 73 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@83 74 //this code should rely on the locale settings but it's not! See RFE 6443132
mcimadamore@168 75 StringBuilder buf = new StringBuilder();
mcimadamore@83 76 Collection<String> args = formatArguments(d, l);
mcimadamore@168 77 buf.append(localize(l, d.getCode(), args.toArray()));
mcimadamore@168 78 if (d.isMultiline()) {
mcimadamore@168 79 buf.append(formatSubdiagnostics(d, l));
mcimadamore@168 80 }
mcimadamore@168 81 return buf.toString();
mcimadamore@83 82 }
mcimadamore@83 83
mcimadamore@83 84 public String formatKind(JCDiagnostic d, Locale l) {
mcimadamore@83 85 switch (d.getType()) {
mcimadamore@83 86 case FRAGMENT: return "";
mcimadamore@83 87 case NOTE: return localize(l, "compiler.note.note");
mcimadamore@83 88 case WARNING: return localize(l, "compiler.warn.warning");
mcimadamore@83 89 case ERROR: return localize(l, "compiler.err.error");
mcimadamore@83 90 default:
mcimadamore@83 91 throw new AssertionError("Unknown diagnostic type: " + d.getType());
mcimadamore@83 92 }
mcimadamore@83 93 }
mcimadamore@83 94
mcimadamore@83 95 public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
mcimadamore@83 96 assert (d.getPosition() != Position.NOPOS);
mcimadamore@83 97 return String.valueOf(getPosition(d, pk));
mcimadamore@83 98 }
mcimadamore@83 99 //WHERE
mcimadamore@83 100 public long getPosition(JCDiagnostic d, PositionKind pk) {
mcimadamore@83 101 switch (pk) {
mcimadamore@83 102 case START: return d.getIntStartPosition();
mcimadamore@83 103 case END: return d.getIntEndPosition();
mcimadamore@83 104 case LINE: return d.getLineNumber();
mcimadamore@83 105 case COLUMN: return d.getColumnNumber();
mcimadamore@83 106 case OFFSET: return d.getIntPosition();
mcimadamore@83 107 default:
mcimadamore@83 108 throw new AssertionError("Unknown diagnostic position: " + pk);
mcimadamore@83 109 }
mcimadamore@83 110 }
mcimadamore@83 111
mcimadamore@100 112 public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
mcimadamore@83 113 assert (d.getSource() != null);
mcimadamore@100 114 return fullname ? d.getSourceName() : d.getSource().getName();
mcimadamore@83 115 }
mcimadamore@83 116
mcimadamore@83 117 /**
mcimadamore@83 118 * Format the arguments of a given diagnostic.
mcimadamore@83 119 *
mcimadamore@83 120 * @param d diagnostic whose arguments are to be formatted
mcimadamore@83 121 * @param l locale object to be used for i18n
mcimadamore@83 122 * @return a Collection whose elements are the formatted arguments of the diagnostic
mcimadamore@83 123 */
mcimadamore@83 124 protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
mcimadamore@83 125 ListBuffer<String> buf = new ListBuffer<String>();
mcimadamore@83 126 for (Object o : d.getArgs()) {
mcimadamore@83 127 buf.append(formatArgument(d, o, l));
mcimadamore@83 128 }
mcimadamore@83 129 return buf.toList();
mcimadamore@83 130 }
mcimadamore@83 131
mcimadamore@83 132 /**
mcimadamore@83 133 * Format a single argument of a given diagnostic.
mcimadamore@83 134 *
mcimadamore@83 135 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 136 * @param arg argument to be formatted
mcimadamore@83 137 * @param l locale object to be used for i18n
mcimadamore@83 138 * @return string representation of the diagnostic argument
mcimadamore@83 139 */
mcimadamore@83 140 protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
mcimadamore@83 141 if (arg instanceof JCDiagnostic)
mcimadamore@83 142 return format((JCDiagnostic)arg, l);
mcimadamore@83 143 else if (arg instanceof Iterable<?>) {
mcimadamore@83 144 return formatIterable(d, (Iterable<?>)arg, l);
mcimadamore@83 145 }
mcimadamore@83 146 else if (arg instanceof JavaFileObject)
mcimadamore@83 147 return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
mcimadamore@83 148 else if (arg instanceof Formattable)
mcimadamore@136 149 return ((Formattable)arg).toString(l, messages);
mcimadamore@83 150 else
mcimadamore@83 151 return String.valueOf(arg);
mcimadamore@83 152 }
mcimadamore@83 153
mcimadamore@83 154 /**
mcimadamore@83 155 * Format an iterable argument of a given diagnostic.
mcimadamore@83 156 *
mcimadamore@83 157 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 158 * @param it iterable argument to be formatted
mcimadamore@83 159 * @param l locale object to be used for i18n
mcimadamore@83 160 * @return string representation of the diagnostic iterable argument
mcimadamore@83 161 */
mcimadamore@83 162 protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
mcimadamore@83 163 StringBuilder sbuf = new StringBuilder();
mcimadamore@83 164 String sep = "";
mcimadamore@83 165 for (Object o : it) {
mcimadamore@83 166 sbuf.append(sep);
mcimadamore@83 167 sbuf.append(formatArgument(d, o, l));
mcimadamore@83 168 sep = ",";
mcimadamore@83 169 }
mcimadamore@83 170 return sbuf.toString();
mcimadamore@83 171 }
mcimadamore@83 172
mcimadamore@168 173 /**
mcimadamore@168 174 * Format all the subdiagnostics attached to a given diagnostic
mcimadamore@168 175 *
mcimadamore@168 176 * @param d diagnostic whose subdiagnostics are to be formatted
mcimadamore@168 177 * @param l locale object to be used for i18n
mcimadamore@168 178 * @return string representation of the subdiagnostics
mcimadamore@168 179 */
mcimadamore@168 180 protected String formatSubdiagnostics(JCDiagnostic d, Locale l) {
mcimadamore@168 181 StringBuilder buf = new StringBuilder();
mcimadamore@168 182 for (JCDiagnostic d2 : d.getSubdiagnostics()) {
mcimadamore@168 183 buf.append('\n');
mcimadamore@168 184 String subdiagMsg = format(d2, l);
mcimadamore@168 185 buf.append(indent(subdiagMsg, DiagInc));
mcimadamore@168 186 }
mcimadamore@168 187 return buf.toString();
mcimadamore@168 188 }
mcimadamore@168 189
mcimadamore@137 190 /** Format the faulty source code line and point to the error.
mcimadamore@137 191 * @param d The diagnostic for which the error line should be printed
mcimadamore@137 192 */
mcimadamore@137 193 protected String formatSourceLine(JCDiagnostic d) {
mcimadamore@137 194 StringBuilder buf = new StringBuilder();
mcimadamore@137 195 DiagnosticSource source = d.getDiagnosticSource();
mcimadamore@137 196 int pos = d.getIntPosition();
mcimadamore@137 197 if (d.getIntPosition() != Position.NOPOS) {
mcimadamore@137 198 String line = (source == null ? null : source.getLine(pos));
mcimadamore@137 199 if (line == null)
mcimadamore@137 200 return "";
mcimadamore@137 201 buf.append(line+"\n");
mcimadamore@137 202 int col = source.getColumnNumber(pos, false);
mcimadamore@137 203 for (int i = 0; i < col - 1; i++) {
mcimadamore@137 204 buf.append((line.charAt(i) == '\t') ? "\t" : " ");
mcimadamore@137 205 }
mcimadamore@137 206 buf.append("^");
mcimadamore@137 207 }
mcimadamore@137 208 return buf.toString();
mcimadamore@137 209 }
mcimadamore@137 210
mcimadamore@83 211 /**
mcimadamore@83 212 * Converts a String into a locale-dependent representation accordingly to a given locale
mcimadamore@83 213 *
mcimadamore@83 214 * @param l locale object to be used for i18n
mcimadamore@83 215 * @param key locale-independent key used for looking up in a resource file
mcimadamore@83 216 * @param args localization arguments
mcimadamore@83 217 * @return a locale-dependent string
mcimadamore@83 218 */
mcimadamore@83 219 protected String localize(Locale l, String key, Object... args) {
mcimadamore@136 220 return messages.getLocalizedString(l, key, args);
mcimadamore@83 221 }
mcimadamore@137 222
mcimadamore@137 223 public boolean displaySource(JCDiagnostic d) {
mcimadamore@137 224 return showSource && d.getType() != FRAGMENT;
mcimadamore@137 225 }
mcimadamore@168 226
mcimadamore@168 227 /**
mcimadamore@168 228 * Creates a string with a given amount of empty spaces. Useful for
mcimadamore@168 229 * indenting the text of a diagnostic message.
mcimadamore@168 230 *
mcimadamore@168 231 * @param nSpaces the amount of spaces to be added to the result string
mcimadamore@168 232 * @return the indentation string
mcimadamore@168 233 */
mcimadamore@168 234 protected String indentString(int nSpaces) {
mcimadamore@168 235 String spaces = " ";
mcimadamore@168 236 if (nSpaces <= spaces.length())
mcimadamore@168 237 return spaces.substring(0, nSpaces);
mcimadamore@168 238 else {
mcimadamore@168 239 StringBuilder buf = new StringBuilder();
mcimadamore@168 240 for (int i = 0 ; i < nSpaces ; i++)
mcimadamore@168 241 buf.append(" ");
mcimadamore@168 242 return buf.toString();
mcimadamore@168 243 }
mcimadamore@168 244 }
mcimadamore@168 245
mcimadamore@168 246 /**
mcimadamore@168 247 * Indent a string by prepending a given amount of empty spaces to each line
mcimadamore@168 248 * of the string
mcimadamore@168 249 *
mcimadamore@168 250 * @param s the string to be indented
mcimadamore@168 251 * @param nSpaces the amount of spaces that should be prepended to each line
mcimadamore@168 252 * of the string
mcimadamore@168 253 * @return an indented string
mcimadamore@168 254 */
mcimadamore@168 255 protected String indent(String s, int nSpaces) {
mcimadamore@168 256 String indent = indentString(nSpaces);
mcimadamore@168 257 StringBuilder buf = new StringBuilder();
mcimadamore@168 258 String nl = "";
mcimadamore@168 259 for (String line : s.split("\n")) {
mcimadamore@168 260 buf.append(nl);
mcimadamore@168 261 buf.append(indent + line);
mcimadamore@168 262 nl = "\n";
mcimadamore@168 263 }
mcimadamore@168 264 return buf.toString();
mcimadamore@168 265 }
mcimadamore@83 266 }

mercurial