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