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
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package com.sun.tools.javac.util;
27 import java.util.Locale;
29 import com.sun.tools.javac.api.Formattable;
30 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
32 /**
33 * A raw formatter for diagnostic messages.
34 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
35 * or not the source name and position are set. This formatter provides a standardized, localize-independent
36 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
37 */
38 public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
40 /**
41 * Create a formatter based on the supplied options.
42 * @param msgs
43 */
44 public RawDiagnosticFormatter(Options opts) {
45 super(null, opts, false);
46 }
48 //provide common default formats
49 public String format(JCDiagnostic d, Locale l) {
50 try {
51 StringBuffer buf = new StringBuffer();
52 if (d.getPosition() != Position.NOPOS) {
53 buf.append(formatSource(d, false, null));
54 buf.append(':');
55 buf.append(formatPosition(d, LINE, null));
56 buf.append(':');
57 buf.append(formatPosition(d, COLUMN, null));
58 buf.append(':');
59 }
60 else
61 buf.append('-');
62 buf.append(' ');
63 buf.append(formatMessage(d, null));
64 if (displaySource(d))
65 buf.append("\n" + formatSourceLine(d));
66 return buf.toString();
67 }
68 catch (Exception e) {
69 e.printStackTrace();
70 return null;
71 }
72 }
74 @Override
75 protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
76 String s;
77 if (arg instanceof Formattable)
78 s = arg.toString();
79 else
80 s = super.formatArgument(diag, arg, null);
81 if (arg instanceof JCDiagnostic)
82 return "(" + s + ")";
83 else
84 return s;
85 }
87 @Override
88 protected String localize(Locale l, String s, Object... args) {
89 StringBuffer buf = new StringBuffer();
90 buf.append(s);
91 String sep = ": ";
92 for (Object o : args) {
93 buf.append(sep);
94 buf.append(o);
95 sep = ", ";
96 }
97 return buf.toString();
98 }
99 }