author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 7624 | c31b0ea95b37 |
permissions | -rw-r--r-- |
942 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
942 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
942 | 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 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
942 | 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; |
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3380
diff
changeset
|
33 |
import com.sun.tools.javac.file.BaseFileObject; |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
34 |
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
|
35 |
|
942 | 36 |
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
37 |
||
38 |
/** |
|
39 |
* A raw formatter for diagnostic messages. |
|
40 |
* The raw formatter will format a diagnostic according to one of two format patterns, depending on whether |
|
41 |
* or not the source name and position are set. This formatter provides a standardized, localize-independent |
|
42 |
* implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes. |
|
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
2984
diff
changeset
|
43 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
44 |
* <p><b>This is NOT part of any supported API. |
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
2984
diff
changeset
|
45 |
* If you write code that depends on this, you do so at your own risk. |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
2984
diff
changeset
|
46 |
* This code and its internal interfaces are subject to change or |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
2984
diff
changeset
|
47 |
* deletion without notice.</b> |
942 | 48 |
*/ |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
49 |
public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { |
942 | 50 |
|
51 |
/** |
|
52 |
* Create a formatter based on the supplied options. |
|
53 |
* @param msgs |
|
54 |
*/ |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
55 |
public RawDiagnosticFormatter(Options options) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
56 |
super(null, new SimpleConfiguration(options, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
57 |
EnumSet.of(DiagnosticPart.SUMMARY, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
58 |
DiagnosticPart.DETAILS, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
59 |
DiagnosticPart.SUBDIAGNOSTICS))); |
942 | 60 |
} |
61 |
||
62 |
//provide common default formats |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
63 |
public String formatDiagnostic(JCDiagnostic d, Locale l) { |
942 | 64 |
try { |
65 |
StringBuffer buf = new StringBuffer(); |
|
66 |
if (d.getPosition() != Position.NOPOS) { |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
67 |
buf.append(formatSource(d, false, null)); |
942 | 68 |
buf.append(':'); |
69 |
buf.append(formatPosition(d, LINE, null)); |
|
70 |
buf.append(':'); |
|
71 |
buf.append(formatPosition(d, COLUMN, null)); |
|
72 |
buf.append(':'); |
|
73 |
} |
|
74 |
else |
|
75 |
buf.append('-'); |
|
76 |
buf.append(' '); |
|
77 |
buf.append(formatMessage(d, null)); |
|
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
78 |
if (displaySource(d)) |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
79 |
buf.append("\n" + formatSourceLine(d, 0)); |
942 | 80 |
return buf.toString(); |
81 |
} |
|
82 |
catch (Exception e) { |
|
83 |
e.printStackTrace(); |
|
84 |
return null; |
|
85 |
} |
|
86 |
} |
|
87 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
88 |
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
|
89 |
StringBuilder buf = new StringBuilder(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
List<String> subDiags = formatSubdiagnostics(d, null); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
94 |
if (subDiags.nonEmpty()) { |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
95 |
String sep = ""; |
2085
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 |
for (String sub : formatSubdiagnostics(d, null)) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
98 |
buf.append(sep); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
99 |
buf.append("(" + sub + ")"); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
100 |
sep = ","; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
101 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
102 |
buf.append('}'); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
103 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
104 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
105 |
return buf.toString(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
106 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
107 |
|
942 | 108 |
@Override |
109 |
protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { |
|
110 |
String s; |
|
111 |
if (arg instanceof Formattable) |
|
112 |
s = arg.toString(); |
|
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3380
diff
changeset
|
113 |
else if (arg instanceof BaseFileObject) |
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3380
diff
changeset
|
114 |
s = ((BaseFileObject) arg).getShortName(); |
942 | 115 |
else |
116 |
s = super.formatArgument(diag, arg, null); |
|
117 |
if (arg instanceof JCDiagnostic) |
|
118 |
return "(" + s + ")"; |
|
119 |
else |
|
120 |
return s; |
|
121 |
} |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
122 |
|
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
123 |
@Override |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
124 |
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
|
125 |
StringBuilder buf = new StringBuilder(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
126 |
buf.append(key); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
127 |
String sep = ": "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
128 |
for (Object o : args) { |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
129 |
buf.append(sep); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
130 |
buf.append(o); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
131 |
sep = ", "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
132 |
} |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
133 |
return buf.toString(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
134 |
} |
2984
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
135 |
|
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
136 |
@Override |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
137 |
public boolean isRaw() { |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
138 |
return true; |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
139 |
} |
942 | 140 |
} |