Thu, 09 Oct 2008 16:19:13 +0100
6731573: diagnostic output should optionally include source line
Summary: Added an -XD option to optionally prints out source lines in error messages
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@136 | 30 | import java.util.ResourceBundle; |
mcimadamore@83 | 31 | |
mcimadamore@83 | 32 | import com.sun.tools.javac.api.DiagnosticFormatter; |
mcimadamore@83 | 33 | import com.sun.tools.javac.api.Formattable; |
mcimadamore@83 | 34 | import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind; |
mcimadamore@83 | 35 | import com.sun.tools.javac.file.JavacFileManager; |
mcimadamore@137 | 36 | import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
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@83 | 75 | Collection<String> args = formatArguments(d, l); |
mcimadamore@83 | 76 | return localize(l, d.getCode(), args.toArray()); |
mcimadamore@83 | 77 | } |
mcimadamore@83 | 78 | |
mcimadamore@83 | 79 | public String formatKind(JCDiagnostic d, Locale l) { |
mcimadamore@83 | 80 | switch (d.getType()) { |
mcimadamore@83 | 81 | case FRAGMENT: return ""; |
mcimadamore@83 | 82 | case NOTE: return localize(l, "compiler.note.note"); |
mcimadamore@83 | 83 | case WARNING: return localize(l, "compiler.warn.warning"); |
mcimadamore@83 | 84 | case ERROR: return localize(l, "compiler.err.error"); |
mcimadamore@83 | 85 | default: |
mcimadamore@83 | 86 | throw new AssertionError("Unknown diagnostic type: " + d.getType()); |
mcimadamore@83 | 87 | } |
mcimadamore@83 | 88 | } |
mcimadamore@83 | 89 | |
mcimadamore@83 | 90 | public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) { |
mcimadamore@83 | 91 | assert (d.getPosition() != Position.NOPOS); |
mcimadamore@83 | 92 | return String.valueOf(getPosition(d, pk)); |
mcimadamore@83 | 93 | } |
mcimadamore@83 | 94 | //WHERE |
mcimadamore@83 | 95 | public long getPosition(JCDiagnostic d, PositionKind pk) { |
mcimadamore@83 | 96 | switch (pk) { |
mcimadamore@83 | 97 | case START: return d.getIntStartPosition(); |
mcimadamore@83 | 98 | case END: return d.getIntEndPosition(); |
mcimadamore@83 | 99 | case LINE: return d.getLineNumber(); |
mcimadamore@83 | 100 | case COLUMN: return d.getColumnNumber(); |
mcimadamore@83 | 101 | case OFFSET: return d.getIntPosition(); |
mcimadamore@83 | 102 | default: |
mcimadamore@83 | 103 | throw new AssertionError("Unknown diagnostic position: " + pk); |
mcimadamore@83 | 104 | } |
mcimadamore@83 | 105 | } |
mcimadamore@83 | 106 | |
mcimadamore@100 | 107 | public String formatSource(JCDiagnostic d, boolean fullname, Locale l) { |
mcimadamore@83 | 108 | assert (d.getSource() != null); |
mcimadamore@100 | 109 | return fullname ? d.getSourceName() : d.getSource().getName(); |
mcimadamore@83 | 110 | } |
mcimadamore@83 | 111 | |
mcimadamore@83 | 112 | /** |
mcimadamore@83 | 113 | * Format the arguments of a given diagnostic. |
mcimadamore@83 | 114 | * |
mcimadamore@83 | 115 | * @param d diagnostic whose arguments are to be formatted |
mcimadamore@83 | 116 | * @param l locale object to be used for i18n |
mcimadamore@83 | 117 | * @return a Collection whose elements are the formatted arguments of the diagnostic |
mcimadamore@83 | 118 | */ |
mcimadamore@83 | 119 | protected Collection<String> formatArguments(JCDiagnostic d, Locale l) { |
mcimadamore@83 | 120 | ListBuffer<String> buf = new ListBuffer<String>(); |
mcimadamore@83 | 121 | for (Object o : d.getArgs()) { |
mcimadamore@83 | 122 | buf.append(formatArgument(d, o, l)); |
mcimadamore@83 | 123 | } |
mcimadamore@83 | 124 | return buf.toList(); |
mcimadamore@83 | 125 | } |
mcimadamore@83 | 126 | |
mcimadamore@83 | 127 | /** |
mcimadamore@83 | 128 | * Format a single argument of a given diagnostic. |
mcimadamore@83 | 129 | * |
mcimadamore@83 | 130 | * @param d diagnostic whose argument is to be formatted |
mcimadamore@83 | 131 | * @param arg argument to be formatted |
mcimadamore@83 | 132 | * @param l locale object to be used for i18n |
mcimadamore@83 | 133 | * @return string representation of the diagnostic argument |
mcimadamore@83 | 134 | */ |
mcimadamore@83 | 135 | protected String formatArgument(JCDiagnostic d, Object arg, Locale l) { |
mcimadamore@83 | 136 | if (arg instanceof JCDiagnostic) |
mcimadamore@83 | 137 | return format((JCDiagnostic)arg, l); |
mcimadamore@83 | 138 | else if (arg instanceof Iterable<?>) { |
mcimadamore@83 | 139 | return formatIterable(d, (Iterable<?>)arg, l); |
mcimadamore@83 | 140 | } |
mcimadamore@83 | 141 | else if (arg instanceof JavaFileObject) |
mcimadamore@83 | 142 | return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg); |
mcimadamore@83 | 143 | else if (arg instanceof Formattable) |
mcimadamore@136 | 144 | return ((Formattable)arg).toString(l, messages); |
mcimadamore@83 | 145 | else |
mcimadamore@83 | 146 | return String.valueOf(arg); |
mcimadamore@83 | 147 | } |
mcimadamore@83 | 148 | |
mcimadamore@83 | 149 | /** |
mcimadamore@83 | 150 | * Format an iterable argument of a given diagnostic. |
mcimadamore@83 | 151 | * |
mcimadamore@83 | 152 | * @param d diagnostic whose argument is to be formatted |
mcimadamore@83 | 153 | * @param it iterable argument to be formatted |
mcimadamore@83 | 154 | * @param l locale object to be used for i18n |
mcimadamore@83 | 155 | * @return string representation of the diagnostic iterable argument |
mcimadamore@83 | 156 | */ |
mcimadamore@83 | 157 | protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) { |
mcimadamore@83 | 158 | StringBuilder sbuf = new StringBuilder(); |
mcimadamore@83 | 159 | String sep = ""; |
mcimadamore@83 | 160 | for (Object o : it) { |
mcimadamore@83 | 161 | sbuf.append(sep); |
mcimadamore@83 | 162 | sbuf.append(formatArgument(d, o, l)); |
mcimadamore@83 | 163 | sep = ","; |
mcimadamore@83 | 164 | } |
mcimadamore@83 | 165 | return sbuf.toString(); |
mcimadamore@83 | 166 | } |
mcimadamore@83 | 167 | |
mcimadamore@137 | 168 | /** Format the faulty source code line and point to the error. |
mcimadamore@137 | 169 | * @param d The diagnostic for which the error line should be printed |
mcimadamore@137 | 170 | */ |
mcimadamore@137 | 171 | protected String formatSourceLine(JCDiagnostic d) { |
mcimadamore@137 | 172 | StringBuilder buf = new StringBuilder(); |
mcimadamore@137 | 173 | DiagnosticSource source = d.getDiagnosticSource(); |
mcimadamore@137 | 174 | int pos = d.getIntPosition(); |
mcimadamore@137 | 175 | if (d.getIntPosition() != Position.NOPOS) { |
mcimadamore@137 | 176 | String line = (source == null ? null : source.getLine(pos)); |
mcimadamore@137 | 177 | if (line == null) |
mcimadamore@137 | 178 | return ""; |
mcimadamore@137 | 179 | buf.append(line+"\n"); |
mcimadamore@137 | 180 | int col = source.getColumnNumber(pos, false); |
mcimadamore@137 | 181 | for (int i = 0; i < col - 1; i++) { |
mcimadamore@137 | 182 | buf.append((line.charAt(i) == '\t') ? "\t" : " "); |
mcimadamore@137 | 183 | } |
mcimadamore@137 | 184 | buf.append("^"); |
mcimadamore@137 | 185 | } |
mcimadamore@137 | 186 | return buf.toString(); |
mcimadamore@137 | 187 | } |
mcimadamore@137 | 188 | |
mcimadamore@83 | 189 | /** |
mcimadamore@83 | 190 | * Converts a String into a locale-dependent representation accordingly to a given locale |
mcimadamore@83 | 191 | * |
mcimadamore@83 | 192 | * @param l locale object to be used for i18n |
mcimadamore@83 | 193 | * @param key locale-independent key used for looking up in a resource file |
mcimadamore@83 | 194 | * @param args localization arguments |
mcimadamore@83 | 195 | * @return a locale-dependent string |
mcimadamore@83 | 196 | */ |
mcimadamore@83 | 197 | protected String localize(Locale l, String key, Object... args) { |
mcimadamore@136 | 198 | return messages.getLocalizedString(l, key, args); |
mcimadamore@83 | 199 | } |
mcimadamore@137 | 200 | |
mcimadamore@137 | 201 | public boolean displaySource(JCDiagnostic d) { |
mcimadamore@137 | 202 | return showSource && d.getType() != FRAGMENT; |
mcimadamore@137 | 203 | } |
mcimadamore@83 | 204 | } |