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

Mon, 09 Mar 2009 23:53:41 -0700

author
tbell
date
Mon, 09 Mar 2009 23:53:41 -0700
changeset 240
8c55d5b0ed71
parent 229
03bcd66bd8e7
parent 238
86b60aa941c6
child 288
d402db1005ad
permissions
-rw-r--r--

Merge

mcimadamore@83 1 /*
xdono@229 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.Collection;
mcimadamore@221 28 import java.util.EnumSet;
mcimadamore@83 29 import java.util.Locale;
mcimadamore@83 30
mcimadamore@221 31 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
mcimadamore@83 32 import com.sun.tools.javac.api.Formattable;
mcimadamore@221 33 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
mcimadamore@221 34
mcimadamore@83 35 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
mcimadamore@83 36
mcimadamore@83 37 /**
mcimadamore@83 38 * A raw formatter for diagnostic messages.
mcimadamore@83 39 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
mcimadamore@83 40 * or not the source name and position are set. This formatter provides a standardized, localize-independent
mcimadamore@83 41 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
mcimadamore@83 42 */
mcimadamore@221 43 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
mcimadamore@83 44
mcimadamore@83 45 /**
mcimadamore@83 46 * Create a formatter based on the supplied options.
mcimadamore@83 47 * @param msgs
mcimadamore@83 48 */
mcimadamore@221 49 public RawDiagnosticFormatter(Options options) {
mcimadamore@221 50 super(null, new SimpleConfiguration(options,
mcimadamore@221 51 EnumSet.of(DiagnosticPart.SUMMARY,
mcimadamore@221 52 DiagnosticPart.DETAILS,
mcimadamore@221 53 DiagnosticPart.SUBDIAGNOSTICS)));
mcimadamore@83 54 }
mcimadamore@83 55
mcimadamore@83 56 //provide common default formats
mcimadamore@238 57 public String formatDiagnostic(JCDiagnostic d, Locale l) {
mcimadamore@83 58 try {
mcimadamore@83 59 StringBuffer buf = new StringBuffer();
mcimadamore@83 60 if (d.getPosition() != Position.NOPOS) {
mcimadamore@100 61 buf.append(formatSource(d, false, null));
mcimadamore@83 62 buf.append(':');
mcimadamore@83 63 buf.append(formatPosition(d, LINE, null));
mcimadamore@83 64 buf.append(':');
mcimadamore@83 65 buf.append(formatPosition(d, COLUMN, null));
mcimadamore@83 66 buf.append(':');
mcimadamore@83 67 }
mcimadamore@83 68 else
mcimadamore@83 69 buf.append('-');
mcimadamore@83 70 buf.append(' ');
mcimadamore@83 71 buf.append(formatMessage(d, null));
mcimadamore@137 72 if (displaySource(d))
mcimadamore@221 73 buf.append("\n" + formatSourceLine(d, 0));
mcimadamore@83 74 return buf.toString();
mcimadamore@83 75 }
mcimadamore@83 76 catch (Exception e) {
mcimadamore@83 77 e.printStackTrace();
mcimadamore@83 78 return null;
mcimadamore@83 79 }
mcimadamore@83 80 }
mcimadamore@83 81
mcimadamore@221 82 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@221 83 StringBuilder buf = new StringBuilder();
mcimadamore@221 84 Collection<String> args = formatArguments(d, l);
mcimadamore@238 85 buf.append(localize(null, d.getCode(), args.toArray()));
mcimadamore@221 86 if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
mcimadamore@221 87 List<String> subDiags = formatSubdiagnostics(d, null);
mcimadamore@221 88 if (subDiags.nonEmpty()) {
mcimadamore@238 89 String sep = "";
mcimadamore@221 90 buf.append(",{");
mcimadamore@221 91 for (String sub : formatSubdiagnostics(d, null)) {
mcimadamore@221 92 buf.append(sep);
mcimadamore@221 93 buf.append("(" + sub + ")");
mcimadamore@221 94 sep = ",";
mcimadamore@221 95 }
mcimadamore@221 96 buf.append('}');
mcimadamore@221 97 }
mcimadamore@221 98 }
mcimadamore@221 99 return buf.toString();
mcimadamore@221 100 }
mcimadamore@221 101
mcimadamore@83 102 @Override
mcimadamore@83 103 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
mcimadamore@83 104 String s;
mcimadamore@83 105 if (arg instanceof Formattable)
mcimadamore@83 106 s = arg.toString();
mcimadamore@83 107 else
mcimadamore@83 108 s = super.formatArgument(diag, arg, null);
mcimadamore@83 109 if (arg instanceof JCDiagnostic)
mcimadamore@83 110 return "(" + s + ")";
mcimadamore@83 111 else
mcimadamore@83 112 return s;
mcimadamore@83 113 }
mcimadamore@238 114
mcimadamore@238 115 @Override
mcimadamore@238 116 protected String localize(Locale l, String key, Object... args) {
mcimadamore@238 117 StringBuilder buf = new StringBuilder();
mcimadamore@238 118 buf.append(key);
mcimadamore@238 119 String sep = ": ";
mcimadamore@238 120 for (Object o : args) {
mcimadamore@238 121 buf.append(sep);
mcimadamore@238 122 buf.append(o);
mcimadamore@238 123 sep = ", ";
mcimadamore@238 124 }
mcimadamore@238 125 return buf.toString();
mcimadamore@238 126 }
mcimadamore@83 127 }

mercurial