author | tbell |
Mon, 09 Mar 2009 23:53:41 -0700 | |
changeset 2223 | 95e3c21b2919 |
parent 2212 | 1d3dc0e0ba0c |
parent 2221 | cd6557bcaa0a |
child 2984 | e15ff3a34054 |
permissions | -rw-r--r-- |
942 | 1 |
/* |
2212 | 2 |
* Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
942 | 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; |
|
26 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
27 |
import java.util.Collection; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
28 |
import java.util.EnumSet; |
942 | 29 |
import java.util.Locale; |
30 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
31 |
import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*; |
942 | 32 |
import com.sun.tools.javac.api.Formattable; |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
33 |
import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
34 |
|
942 | 35 |
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
36 |
||
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 |
*/ |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
43 |
public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { |
942 | 44 |
|
45 |
/** |
|
46 |
* Create a formatter based on the supplied options. |
|
47 |
* @param msgs |
|
48 |
*/ |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
49 |
public RawDiagnosticFormatter(Options options) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
50 |
super(null, new SimpleConfiguration(options, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
51 |
EnumSet.of(DiagnosticPart.SUMMARY, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
52 |
DiagnosticPart.DETAILS, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
53 |
DiagnosticPart.SUBDIAGNOSTICS))); |
942 | 54 |
} |
55 |
||
56 |
//provide common default formats |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
57 |
public String formatDiagnostic(JCDiagnostic d, Locale l) { |
942 | 58 |
try { |
59 |
StringBuffer buf = new StringBuffer(); |
|
60 |
if (d.getPosition() != Position.NOPOS) { |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
61 |
buf.append(formatSource(d, false, null)); |
942 | 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)); |
|
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
72 |
if (displaySource(d)) |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
73 |
buf.append("\n" + formatSourceLine(d, 0)); |
942 | 74 |
return buf.toString(); |
75 |
} |
|
76 |
catch (Exception e) { |
|
77 |
e.printStackTrace(); |
|
78 |
return null; |
|
79 |
} |
|
80 |
} |
|
81 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
82 |
public String formatMessage(JCDiagnostic d, Locale l) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
83 |
StringBuilder buf = new StringBuilder(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
84 |
Collection<String> args = formatArguments(d, l); |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
85 |
buf.append(localize(null, d.getCode(), args.toArray())); |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
86 |
if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
87 |
List<String> subDiags = formatSubdiagnostics(d, null); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
88 |
if (subDiags.nonEmpty()) { |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
89 |
String sep = ""; |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
90 |
buf.append(",{"); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
91 |
for (String sub : formatSubdiagnostics(d, null)) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
92 |
buf.append(sep); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
93 |
buf.append("(" + sub + ")"); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
94 |
sep = ","; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
95 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
96 |
buf.append('}'); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
97 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
98 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
99 |
return buf.toString(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
100 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
101 |
|
942 | 102 |
@Override |
103 |
protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { |
|
104 |
String s; |
|
105 |
if (arg instanceof Formattable) |
|
106 |
s = arg.toString(); |
|
107 |
else |
|
108 |
s = super.formatArgument(diag, arg, null); |
|
109 |
if (arg instanceof JCDiagnostic) |
|
110 |
return "(" + s + ")"; |
|
111 |
else |
|
112 |
return s; |
|
113 |
} |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
114 |
|
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
115 |
@Override |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
116 |
protected String localize(Locale l, String key, Object... args) { |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
117 |
StringBuilder buf = new StringBuilder(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
118 |
buf.append(key); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
119 |
String sep = ": "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
120 |
for (Object o : args) { |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
121 |
buf.append(sep); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
122 |
buf.append(o); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
123 |
sep = ", "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
124 |
} |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
125 |
return buf.toString(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
126 |
} |
942 | 127 |
} |