Mon, 27 Jul 2009 19:52:42 -0700
6865399: some javac files are missing Sun internal API comment
Reviewed-by: darcy
mcimadamore@83 | 1 | /* |
mcimadamore@238 | 2 | * Copyright 2008-2009 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@221 | 27 | import java.util.Arrays; |
mcimadamore@83 | 28 | import java.util.Collection; |
mcimadamore@221 | 29 | import java.util.EnumSet; |
mcimadamore@221 | 30 | import java.util.HashMap; |
mcimadamore@83 | 31 | import java.util.Locale; |
mcimadamore@221 | 32 | import java.util.Map; |
mcimadamore@221 | 33 | import java.util.Set; |
mcimadamore@83 | 34 | import javax.tools.JavaFileObject; |
mcimadamore@83 | 35 | |
mcimadamore@83 | 36 | import com.sun.tools.javac.api.DiagnosticFormatter; |
mcimadamore@221 | 37 | import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart; |
mcimadamore@221 | 38 | import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.MultilineLimit; |
mcimadamore@221 | 39 | import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind; |
mcimadamore@83 | 40 | import com.sun.tools.javac.api.Formattable; |
mcimadamore@238 | 41 | import com.sun.tools.javac.code.Printer; |
mcimadamore@238 | 42 | import com.sun.tools.javac.code.Symbol; |
mcimadamore@238 | 43 | import com.sun.tools.javac.code.Type; |
mcimadamore@238 | 44 | import com.sun.tools.javac.code.Type.CapturedType; |
mcimadamore@83 | 45 | import com.sun.tools.javac.file.JavacFileManager; |
mcimadamore@221 | 46 | |
mcimadamore@137 | 47 | import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
mcimadamore@83 | 48 | |
mcimadamore@83 | 49 | /** |
mcimadamore@83 | 50 | * This abstract class provides a basic implementation of the functionalities that should be provided |
mcimadamore@83 | 51 | * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are: |
mcimadamore@83 | 52 | * |
mcimadamore@83 | 53 | * <ul> |
mcimadamore@83 | 54 | * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter. |
mcimadamore@83 | 55 | * Those implementations are specifically targeting JCDiagnostic objects. |
mcimadamore@83 | 56 | * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions |
mcimadamore@83 | 57 | * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object. |
mcimadamore@83 | 58 | * <ul> |
mcimadamore@83 | 59 | * |
jjg@333 | 60 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
jjg@333 | 61 | * If you write code that depends on this, you do so at your own risk. |
jjg@333 | 62 | * This code and its internal interfaces are subject to change or |
jjg@333 | 63 | * deletion without notice.</b> |
mcimadamore@83 | 64 | */ |
mcimadamore@83 | 65 | public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> { |
mcimadamore@83 | 66 | |
mcimadamore@83 | 67 | /** |
mcimadamore@221 | 68 | * JavacMessages object used by this formatter for i18n. |
mcimadamore@83 | 69 | */ |
mcimadamore@136 | 70 | protected JavacMessages messages; |
mcimadamore@238 | 71 | |
mcimadamore@238 | 72 | /** |
mcimadamore@238 | 73 | * Configuration object used by this formatter |
mcimadamore@238 | 74 | */ |
mcimadamore@221 | 75 | private SimpleConfiguration config; |
mcimadamore@238 | 76 | |
mcimadamore@238 | 77 | /** |
mcimadamore@238 | 78 | * Current depth level of the disgnostic being formatted |
mcimadamore@238 | 79 | * (!= 0 for subdiagnostics) |
mcimadamore@238 | 80 | */ |
mcimadamore@221 | 81 | protected int depth = 0; |
mcimadamore@83 | 82 | |
mcimadamore@83 | 83 | /** |
mcimadamore@288 | 84 | * All captured types that have been encountered during diagnostic formatting. |
mcimadamore@288 | 85 | * This info is used by the FormatterPrinter in order to print friendly unique |
mcimadamore@288 | 86 | * ids for captured types |
mcimadamore@238 | 87 | */ |
mcimadamore@288 | 88 | private List<Type> allCaptured = List.nil(); |
mcimadamore@238 | 89 | |
mcimadamore@238 | 90 | /** |
mcimadamore@221 | 91 | * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object. |
mcimadamore@83 | 92 | * @param messages |
mcimadamore@83 | 93 | */ |
mcimadamore@221 | 94 | protected AbstractDiagnosticFormatter(JavacMessages messages, SimpleConfiguration config) { |
mcimadamore@83 | 95 | this.messages = messages; |
mcimadamore@221 | 96 | this.config = config; |
mcimadamore@83 | 97 | } |
mcimadamore@83 | 98 | |
mcimadamore@83 | 99 | public String formatKind(JCDiagnostic d, Locale l) { |
mcimadamore@83 | 100 | switch (d.getType()) { |
mcimadamore@83 | 101 | case FRAGMENT: return ""; |
mcimadamore@83 | 102 | case NOTE: return localize(l, "compiler.note.note"); |
mcimadamore@83 | 103 | case WARNING: return localize(l, "compiler.warn.warning"); |
mcimadamore@83 | 104 | case ERROR: return localize(l, "compiler.err.error"); |
mcimadamore@83 | 105 | default: |
mcimadamore@83 | 106 | throw new AssertionError("Unknown diagnostic type: " + d.getType()); |
mcimadamore@83 | 107 | } |
mcimadamore@83 | 108 | } |
mcimadamore@83 | 109 | |
mcimadamore@238 | 110 | @Override |
mcimadamore@238 | 111 | public String format(JCDiagnostic d, Locale locale) { |
mcimadamore@288 | 112 | allCaptured = List.nil(); |
mcimadamore@238 | 113 | return formatDiagnostic(d, locale); |
mcimadamore@238 | 114 | } |
mcimadamore@238 | 115 | |
mcimadamore@304 | 116 | protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale); |
mcimadamore@238 | 117 | |
mcimadamore@83 | 118 | public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) { |
mcimadamore@83 | 119 | assert (d.getPosition() != Position.NOPOS); |
mcimadamore@83 | 120 | return String.valueOf(getPosition(d, pk)); |
mcimadamore@83 | 121 | } |
mcimadamore@221 | 122 | //where |
mcimadamore@221 | 123 | private long getPosition(JCDiagnostic d, PositionKind pk) { |
mcimadamore@83 | 124 | switch (pk) { |
mcimadamore@83 | 125 | case START: return d.getIntStartPosition(); |
mcimadamore@83 | 126 | case END: return d.getIntEndPosition(); |
mcimadamore@83 | 127 | case LINE: return d.getLineNumber(); |
mcimadamore@83 | 128 | case COLUMN: return d.getColumnNumber(); |
mcimadamore@83 | 129 | case OFFSET: return d.getIntPosition(); |
mcimadamore@83 | 130 | default: |
mcimadamore@83 | 131 | throw new AssertionError("Unknown diagnostic position: " + pk); |
mcimadamore@83 | 132 | } |
mcimadamore@83 | 133 | } |
mcimadamore@83 | 134 | |
mcimadamore@100 | 135 | public String formatSource(JCDiagnostic d, boolean fullname, Locale l) { |
mcimadamore@83 | 136 | assert (d.getSource() != null); |
mcimadamore@100 | 137 | return fullname ? d.getSourceName() : d.getSource().getName(); |
mcimadamore@83 | 138 | } |
mcimadamore@83 | 139 | |
mcimadamore@83 | 140 | /** |
mcimadamore@83 | 141 | * Format the arguments of a given diagnostic. |
mcimadamore@83 | 142 | * |
mcimadamore@83 | 143 | * @param d diagnostic whose arguments are to be formatted |
mcimadamore@83 | 144 | * @param l locale object to be used for i18n |
mcimadamore@83 | 145 | * @return a Collection whose elements are the formatted arguments of the diagnostic |
mcimadamore@83 | 146 | */ |
mcimadamore@83 | 147 | protected Collection<String> formatArguments(JCDiagnostic d, Locale l) { |
mcimadamore@83 | 148 | ListBuffer<String> buf = new ListBuffer<String>(); |
mcimadamore@83 | 149 | for (Object o : d.getArgs()) { |
mcimadamore@83 | 150 | buf.append(formatArgument(d, o, l)); |
mcimadamore@83 | 151 | } |
mcimadamore@83 | 152 | return buf.toList(); |
mcimadamore@83 | 153 | } |
mcimadamore@83 | 154 | |
mcimadamore@83 | 155 | /** |
mcimadamore@83 | 156 | * Format a single argument of a given diagnostic. |
mcimadamore@83 | 157 | * |
mcimadamore@83 | 158 | * @param d diagnostic whose argument is to be formatted |
mcimadamore@83 | 159 | * @param arg argument to be formatted |
mcimadamore@83 | 160 | * @param l locale object to be used for i18n |
mcimadamore@83 | 161 | * @return string representation of the diagnostic argument |
mcimadamore@83 | 162 | */ |
mcimadamore@83 | 163 | protected String formatArgument(JCDiagnostic d, Object arg, Locale l) { |
mcimadamore@221 | 164 | if (arg instanceof JCDiagnostic) { |
mcimadamore@221 | 165 | String s = null; |
mcimadamore@221 | 166 | depth++; |
mcimadamore@221 | 167 | try { |
mcimadamore@221 | 168 | s = formatMessage((JCDiagnostic)arg, l); |
mcimadamore@221 | 169 | } |
mcimadamore@221 | 170 | finally { |
mcimadamore@221 | 171 | depth--; |
mcimadamore@221 | 172 | } |
mcimadamore@221 | 173 | return s; |
mcimadamore@221 | 174 | } |
mcimadamore@83 | 175 | else if (arg instanceof Iterable<?>) { |
mcimadamore@83 | 176 | return formatIterable(d, (Iterable<?>)arg, l); |
mcimadamore@83 | 177 | } |
mcimadamore@238 | 178 | else if (arg instanceof Type) { |
mcimadamore@238 | 179 | return printer.visit((Type)arg, l); |
mcimadamore@238 | 180 | } |
mcimadamore@238 | 181 | else if (arg instanceof Symbol) { |
mcimadamore@238 | 182 | return printer.visit((Symbol)arg, l); |
mcimadamore@238 | 183 | } |
mcimadamore@238 | 184 | else if (arg instanceof JavaFileObject) { |
mcimadamore@83 | 185 | return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg); |
mcimadamore@238 | 186 | } |
mcimadamore@238 | 187 | else if (arg instanceof Formattable) { |
mcimadamore@136 | 188 | return ((Formattable)arg).toString(l, messages); |
mcimadamore@238 | 189 | } |
mcimadamore@238 | 190 | else { |
mcimadamore@83 | 191 | return String.valueOf(arg); |
mcimadamore@238 | 192 | } |
mcimadamore@83 | 193 | } |
mcimadamore@83 | 194 | |
mcimadamore@83 | 195 | /** |
mcimadamore@83 | 196 | * Format an iterable argument of a given diagnostic. |
mcimadamore@83 | 197 | * |
mcimadamore@83 | 198 | * @param d diagnostic whose argument is to be formatted |
mcimadamore@83 | 199 | * @param it iterable argument to be formatted |
mcimadamore@83 | 200 | * @param l locale object to be used for i18n |
mcimadamore@83 | 201 | * @return string representation of the diagnostic iterable argument |
mcimadamore@83 | 202 | */ |
mcimadamore@83 | 203 | protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) { |
mcimadamore@83 | 204 | StringBuilder sbuf = new StringBuilder(); |
mcimadamore@83 | 205 | String sep = ""; |
mcimadamore@83 | 206 | for (Object o : it) { |
mcimadamore@83 | 207 | sbuf.append(sep); |
mcimadamore@83 | 208 | sbuf.append(formatArgument(d, o, l)); |
mcimadamore@83 | 209 | sep = ","; |
mcimadamore@83 | 210 | } |
mcimadamore@83 | 211 | return sbuf.toString(); |
mcimadamore@83 | 212 | } |
mcimadamore@83 | 213 | |
mcimadamore@168 | 214 | /** |
mcimadamore@221 | 215 | * Format all the subdiagnostics attached to a given diagnostic. |
mcimadamore@168 | 216 | * |
mcimadamore@168 | 217 | * @param d diagnostic whose subdiagnostics are to be formatted |
mcimadamore@168 | 218 | * @param l locale object to be used for i18n |
mcimadamore@221 | 219 | * @return list of all string representations of the subdiagnostics |
mcimadamore@221 | 220 | */ |
mcimadamore@221 | 221 | protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) { |
mcimadamore@221 | 222 | List<String> subdiagnostics = List.nil(); |
mcimadamore@221 | 223 | int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH); |
mcimadamore@221 | 224 | if (maxDepth == -1 || depth < maxDepth) { |
mcimadamore@221 | 225 | depth++; |
mcimadamore@221 | 226 | try { |
mcimadamore@221 | 227 | int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH); |
mcimadamore@221 | 228 | int count = 0; |
mcimadamore@221 | 229 | for (JCDiagnostic d2 : d.getSubdiagnostics()) { |
mcimadamore@221 | 230 | if (maxCount == -1 || count < maxCount) { |
mcimadamore@221 | 231 | subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l)); |
mcimadamore@221 | 232 | count++; |
mcimadamore@221 | 233 | } |
mcimadamore@221 | 234 | else |
mcimadamore@221 | 235 | break; |
mcimadamore@221 | 236 | } |
mcimadamore@221 | 237 | } |
mcimadamore@221 | 238 | finally { |
mcimadamore@221 | 239 | depth--; |
mcimadamore@221 | 240 | } |
mcimadamore@221 | 241 | } |
mcimadamore@221 | 242 | return subdiagnostics; |
mcimadamore@221 | 243 | } |
mcimadamore@221 | 244 | |
mcimadamore@221 | 245 | /** |
mcimadamore@221 | 246 | * Format a subdiagnostics attached to a given diagnostic. |
mcimadamore@221 | 247 | * |
mcimadamore@221 | 248 | * @param parent multiline diagnostic whose subdiagnostics is to be formatted |
mcimadamore@221 | 249 | * @param sub subdiagnostic to be formatted |
mcimadamore@221 | 250 | * @param l locale object to be used for i18n |
mcimadamore@168 | 251 | * @return string representation of the subdiagnostics |
mcimadamore@168 | 252 | */ |
mcimadamore@221 | 253 | protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) { |
mcimadamore@221 | 254 | return formatMessage(sub, l); |
mcimadamore@168 | 255 | } |
mcimadamore@168 | 256 | |
mcimadamore@137 | 257 | /** Format the faulty source code line and point to the error. |
mcimadamore@137 | 258 | * @param d The diagnostic for which the error line should be printed |
mcimadamore@137 | 259 | */ |
mcimadamore@221 | 260 | protected String formatSourceLine(JCDiagnostic d, int nSpaces) { |
mcimadamore@137 | 261 | StringBuilder buf = new StringBuilder(); |
mcimadamore@137 | 262 | DiagnosticSource source = d.getDiagnosticSource(); |
mcimadamore@137 | 263 | int pos = d.getIntPosition(); |
mcimadamore@221 | 264 | if (d.getIntPosition() == Position.NOPOS) |
mcimadamore@221 | 265 | throw new AssertionError(); |
mcimadamore@221 | 266 | String line = (source == null ? null : source.getLine(pos)); |
mcimadamore@221 | 267 | if (line == null) |
mcimadamore@221 | 268 | return ""; |
mcimadamore@221 | 269 | buf.append(indent(line, nSpaces)); |
mcimadamore@221 | 270 | int col = source.getColumnNumber(pos, false); |
mcimadamore@221 | 271 | if (config.isCaretEnabled()) { |
mcimadamore@221 | 272 | buf.append("\n"); |
mcimadamore@137 | 273 | for (int i = 0; i < col - 1; i++) { |
mcimadamore@137 | 274 | buf.append((line.charAt(i) == '\t') ? "\t" : " "); |
mcimadamore@137 | 275 | } |
mcimadamore@221 | 276 | buf.append(indent("^", nSpaces)); |
mcimadamore@221 | 277 | } |
mcimadamore@221 | 278 | return buf.toString(); |
mcimadamore@137 | 279 | } |
mcimadamore@137 | 280 | |
mcimadamore@83 | 281 | /** |
mcimadamore@221 | 282 | * Converts a String into a locale-dependent representation accordingly to a given locale. |
mcimadamore@83 | 283 | * |
mcimadamore@83 | 284 | * @param l locale object to be used for i18n |
mcimadamore@83 | 285 | * @param key locale-independent key used for looking up in a resource file |
mcimadamore@83 | 286 | * @param args localization arguments |
mcimadamore@83 | 287 | * @return a locale-dependent string |
mcimadamore@83 | 288 | */ |
mcimadamore@83 | 289 | protected String localize(Locale l, String key, Object... args) { |
mcimadamore@136 | 290 | return messages.getLocalizedString(l, key, args); |
mcimadamore@83 | 291 | } |
mcimadamore@137 | 292 | |
mcimadamore@137 | 293 | public boolean displaySource(JCDiagnostic d) { |
mcimadamore@221 | 294 | return config.getVisible().contains(DiagnosticPart.SOURCE) && |
mcimadamore@221 | 295 | d.getType() != FRAGMENT && |
mcimadamore@221 | 296 | d.getIntPosition() != Position.NOPOS; |
mcimadamore@137 | 297 | } |
mcimadamore@168 | 298 | |
mcimadamore@288 | 299 | public boolean isRaw() { |
mcimadamore@288 | 300 | return false; |
mcimadamore@288 | 301 | } |
mcimadamore@288 | 302 | |
mcimadamore@168 | 303 | /** |
mcimadamore@168 | 304 | * Creates a string with a given amount of empty spaces. Useful for |
mcimadamore@168 | 305 | * indenting the text of a diagnostic message. |
mcimadamore@168 | 306 | * |
mcimadamore@168 | 307 | * @param nSpaces the amount of spaces to be added to the result string |
mcimadamore@168 | 308 | * @return the indentation string |
mcimadamore@168 | 309 | */ |
mcimadamore@168 | 310 | protected String indentString(int nSpaces) { |
mcimadamore@168 | 311 | String spaces = " "; |
mcimadamore@168 | 312 | if (nSpaces <= spaces.length()) |
mcimadamore@168 | 313 | return spaces.substring(0, nSpaces); |
mcimadamore@168 | 314 | else { |
mcimadamore@168 | 315 | StringBuilder buf = new StringBuilder(); |
mcimadamore@168 | 316 | for (int i = 0 ; i < nSpaces ; i++) |
mcimadamore@168 | 317 | buf.append(" "); |
mcimadamore@168 | 318 | return buf.toString(); |
mcimadamore@168 | 319 | } |
mcimadamore@168 | 320 | } |
mcimadamore@168 | 321 | |
mcimadamore@168 | 322 | /** |
mcimadamore@168 | 323 | * Indent a string by prepending a given amount of empty spaces to each line |
mcimadamore@221 | 324 | * of the string. |
mcimadamore@168 | 325 | * |
mcimadamore@168 | 326 | * @param s the string to be indented |
mcimadamore@168 | 327 | * @param nSpaces the amount of spaces that should be prepended to each line |
mcimadamore@168 | 328 | * of the string |
mcimadamore@168 | 329 | * @return an indented string |
mcimadamore@168 | 330 | */ |
mcimadamore@168 | 331 | protected String indent(String s, int nSpaces) { |
mcimadamore@168 | 332 | String indent = indentString(nSpaces); |
mcimadamore@168 | 333 | StringBuilder buf = new StringBuilder(); |
mcimadamore@168 | 334 | String nl = ""; |
mcimadamore@168 | 335 | for (String line : s.split("\n")) { |
mcimadamore@168 | 336 | buf.append(nl); |
mcimadamore@168 | 337 | buf.append(indent + line); |
mcimadamore@168 | 338 | nl = "\n"; |
mcimadamore@168 | 339 | } |
mcimadamore@168 | 340 | return buf.toString(); |
mcimadamore@168 | 341 | } |
mcimadamore@221 | 342 | |
mcimadamore@221 | 343 | public SimpleConfiguration getConfiguration() { |
mcimadamore@221 | 344 | return config; |
mcimadamore@221 | 345 | } |
mcimadamore@221 | 346 | |
mcimadamore@221 | 347 | static public class SimpleConfiguration implements Configuration { |
mcimadamore@221 | 348 | |
mcimadamore@221 | 349 | protected Map<MultilineLimit, Integer> multilineLimits; |
mcimadamore@221 | 350 | protected EnumSet<DiagnosticPart> visibleParts; |
mcimadamore@221 | 351 | protected boolean caretEnabled; |
mcimadamore@221 | 352 | |
mcimadamore@221 | 353 | public SimpleConfiguration(Set<DiagnosticPart> parts) { |
mcimadamore@221 | 354 | multilineLimits = new HashMap<MultilineLimit, Integer>(); |
mcimadamore@221 | 355 | setVisible(parts); |
mcimadamore@221 | 356 | setMultilineLimit(MultilineLimit.DEPTH, -1); |
mcimadamore@221 | 357 | setMultilineLimit(MultilineLimit.LENGTH, -1); |
mcimadamore@221 | 358 | setCaretEnabled(true); |
mcimadamore@221 | 359 | } |
mcimadamore@221 | 360 | |
mcimadamore@221 | 361 | @SuppressWarnings("fallthrough") |
mcimadamore@221 | 362 | public SimpleConfiguration(Options options, Set<DiagnosticPart> parts) { |
mcimadamore@221 | 363 | this(parts); |
mcimadamore@221 | 364 | String showSource = null; |
mcimadamore@221 | 365 | if ((showSource = options.get("showSource")) != null) { |
mcimadamore@221 | 366 | if (showSource.equals("true")) |
mcimadamore@288 | 367 | setVisiblePart(DiagnosticPart.SOURCE, true); |
mcimadamore@221 | 368 | else if (showSource.equals("false")) |
mcimadamore@288 | 369 | setVisiblePart(DiagnosticPart.SOURCE, false); |
mcimadamore@221 | 370 | } |
mcimadamore@221 | 371 | String diagOpts = options.get("diags"); |
mcimadamore@221 | 372 | if (diagOpts != null) {//override -XDshowSource |
mcimadamore@221 | 373 | Collection<String> args = Arrays.asList(diagOpts.split(",")); |
mcimadamore@221 | 374 | if (args.contains("short")) { |
mcimadamore@288 | 375 | setVisiblePart(DiagnosticPart.DETAILS, false); |
mcimadamore@288 | 376 | setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false); |
mcimadamore@221 | 377 | } |
mcimadamore@221 | 378 | if (args.contains("source")) |
mcimadamore@288 | 379 | setVisiblePart(DiagnosticPart.SOURCE, true); |
mcimadamore@221 | 380 | if (args.contains("-source")) |
mcimadamore@288 | 381 | setVisiblePart(DiagnosticPart.SOURCE, false); |
mcimadamore@221 | 382 | } |
mcimadamore@221 | 383 | String multiPolicy = null; |
mcimadamore@221 | 384 | if ((multiPolicy = options.get("multilinePolicy")) != null) { |
mcimadamore@221 | 385 | if (multiPolicy.equals("disabled")) |
mcimadamore@288 | 386 | setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false); |
mcimadamore@221 | 387 | else if (multiPolicy.startsWith("limit:")) { |
mcimadamore@221 | 388 | String limitString = multiPolicy.substring("limit:".length()); |
mcimadamore@221 | 389 | String[] limits = limitString.split(":"); |
mcimadamore@221 | 390 | try { |
mcimadamore@221 | 391 | switch (limits.length) { |
mcimadamore@221 | 392 | case 2: { |
mcimadamore@221 | 393 | if (!limits[1].equals("*")) |
mcimadamore@221 | 394 | setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1])); |
mcimadamore@221 | 395 | } |
mcimadamore@221 | 396 | case 1: { |
mcimadamore@221 | 397 | if (!limits[0].equals("*")) |
mcimadamore@221 | 398 | setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0])); |
mcimadamore@221 | 399 | } |
mcimadamore@221 | 400 | } |
mcimadamore@221 | 401 | } |
mcimadamore@221 | 402 | catch(NumberFormatException ex) { |
mcimadamore@221 | 403 | setMultilineLimit(MultilineLimit.DEPTH, -1); |
mcimadamore@221 | 404 | setMultilineLimit(MultilineLimit.LENGTH, -1); |
mcimadamore@221 | 405 | } |
mcimadamore@221 | 406 | } |
mcimadamore@221 | 407 | } |
mcimadamore@221 | 408 | String showCaret = null; |
mcimadamore@221 | 409 | if (((showCaret = options.get("showCaret")) != null) && |
mcimadamore@221 | 410 | showCaret.equals("false")) |
mcimadamore@221 | 411 | setCaretEnabled(false); |
mcimadamore@221 | 412 | else |
mcimadamore@221 | 413 | setCaretEnabled(true); |
mcimadamore@221 | 414 | } |
mcimadamore@221 | 415 | |
mcimadamore@221 | 416 | public int getMultilineLimit(MultilineLimit limit) { |
mcimadamore@221 | 417 | return multilineLimits.get(limit); |
mcimadamore@221 | 418 | } |
mcimadamore@221 | 419 | |
mcimadamore@221 | 420 | public EnumSet<DiagnosticPart> getVisible() { |
mcimadamore@221 | 421 | return EnumSet.copyOf(visibleParts); |
mcimadamore@221 | 422 | } |
mcimadamore@221 | 423 | |
mcimadamore@221 | 424 | public void setMultilineLimit(MultilineLimit limit, int value) { |
mcimadamore@221 | 425 | multilineLimits.put(limit, value < -1 ? -1 : value); |
mcimadamore@221 | 426 | } |
mcimadamore@221 | 427 | |
mcimadamore@221 | 428 | |
mcimadamore@221 | 429 | public void setVisible(Set<DiagnosticPart> diagParts) { |
mcimadamore@221 | 430 | visibleParts = EnumSet.copyOf(diagParts); |
mcimadamore@221 | 431 | } |
mcimadamore@221 | 432 | |
mcimadamore@288 | 433 | public void setVisiblePart(DiagnosticPart diagParts, boolean enabled) { |
mcimadamore@288 | 434 | if (enabled) |
mcimadamore@288 | 435 | visibleParts.add(diagParts); |
mcimadamore@288 | 436 | else |
mcimadamore@288 | 437 | visibleParts.remove(diagParts); |
mcimadamore@288 | 438 | } |
mcimadamore@288 | 439 | |
mcimadamore@221 | 440 | /** |
mcimadamore@221 | 441 | * Shows a '^' sign under the source line displayed by the formatter |
mcimadamore@221 | 442 | * (if applicable). |
mcimadamore@221 | 443 | * |
mcimadamore@221 | 444 | * @param caretEnabled if true enables caret |
mcimadamore@221 | 445 | */ |
mcimadamore@221 | 446 | public void setCaretEnabled(boolean caretEnabled) { |
mcimadamore@221 | 447 | this.caretEnabled = caretEnabled; |
mcimadamore@221 | 448 | } |
mcimadamore@221 | 449 | |
mcimadamore@221 | 450 | /** |
mcimadamore@221 | 451 | * Tells whether the caret display is active or not. |
mcimadamore@221 | 452 | * |
mcimadamore@221 | 453 | * @param caretEnabled if true the caret is enabled |
mcimadamore@221 | 454 | */ |
mcimadamore@221 | 455 | public boolean isCaretEnabled() { |
mcimadamore@221 | 456 | return caretEnabled; |
mcimadamore@221 | 457 | } |
mcimadamore@221 | 458 | } |
mcimadamore@238 | 459 | |
mcimadamore@288 | 460 | public Printer getPrinter() { |
mcimadamore@288 | 461 | return printer; |
mcimadamore@288 | 462 | } |
mcimadamore@288 | 463 | |
mcimadamore@288 | 464 | public void setPrinter(Printer printer) { |
mcimadamore@288 | 465 | this.printer = printer; |
mcimadamore@288 | 466 | } |
mcimadamore@288 | 467 | |
mcimadamore@238 | 468 | /** |
mcimadamore@238 | 469 | * An enhanced printer for formatting types/symbols used by |
mcimadamore@238 | 470 | * AbstractDiagnosticFormatter. Provides alternate numbering of captured |
mcimadamore@238 | 471 | * types (they are numbered starting from 1 on each new diagnostic, instead |
mcimadamore@238 | 472 | * of relying on the underlying hashcode() method which generates unstable |
mcimadamore@238 | 473 | * output). Also detects cycles in wildcard messages (e.g. if the wildcard |
mcimadamore@238 | 474 | * type referred by a given captured type C contains C itself) which might |
mcimadamore@238 | 475 | * lead to infinite loops. |
mcimadamore@238 | 476 | */ |
mcimadamore@288 | 477 | protected Printer printer = new Printer() { |
mcimadamore@238 | 478 | @Override |
mcimadamore@238 | 479 | protected String localize(Locale locale, String key, Object... args) { |
mcimadamore@238 | 480 | return AbstractDiagnosticFormatter.this.localize(locale, key, args); |
mcimadamore@238 | 481 | } |
mcimadamore@238 | 482 | @Override |
mcimadamore@288 | 483 | protected String capturedVarId(CapturedType t, Locale locale) { |
mcimadamore@288 | 484 | return "" + (allCaptured.indexOf(t) + 1); |
mcimadamore@238 | 485 | } |
mcimadamore@296 | 486 | @Override |
mcimadamore@296 | 487 | public String visitCapturedType(CapturedType t, Locale locale) { |
mcimadamore@296 | 488 | if (!allCaptured.contains(t)) { |
mcimadamore@296 | 489 | allCaptured = allCaptured.append(t); |
mcimadamore@296 | 490 | } |
mcimadamore@296 | 491 | return super.visitCapturedType(t, locale); |
mcimadamore@296 | 492 | } |
mcimadamore@288 | 493 | }; |
mcimadamore@83 | 494 | } |