author | briangoetz |
Wed, 18 Dec 2013 10:29:25 -0500 | |
changeset 22159 | 682da512ec17 |
parent 14259 | fb94a1df0d53 |
permissions | -rw-r--r-- |
942 | 1 |
/* |
14259 | 2 |
* Copyright (c) 2008, 2012, 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; |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
30 |
import javax.tools.JavaFileObject; |
942 | 31 |
|
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
32 |
import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*; |
942 | 33 |
import com.sun.tools.javac.api.Formattable; |
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3380
diff
changeset
|
34 |
import com.sun.tools.javac.file.BaseFileObject; |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
35 |
import com.sun.tools.javac.tree.JCTree.*; |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
36 |
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
|
37 |
|
942 | 38 |
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
39 |
||
40 |
/** |
|
41 |
* A raw formatter for diagnostic messages. |
|
42 |
* The raw formatter will format a diagnostic according to one of two format patterns, depending on whether |
|
43 |
* or not the source name and position are set. This formatter provides a standardized, localize-independent |
|
44 |
* 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
|
45 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
46 |
* <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
|
47 |
* 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
|
48 |
* 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
|
49 |
* deletion without notice.</b> |
942 | 50 |
*/ |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
51 |
public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter { |
942 | 52 |
|
53 |
/** |
|
54 |
* Create a formatter based on the supplied options. |
|
14259 | 55 |
* @param options |
942 | 56 |
*/ |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
57 |
public RawDiagnosticFormatter(Options options) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
58 |
super(null, new SimpleConfiguration(options, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
59 |
EnumSet.of(DiagnosticPart.SUMMARY, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
60 |
DiagnosticPart.DETAILS, |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
61 |
DiagnosticPart.SUBDIAGNOSTICS))); |
942 | 62 |
} |
63 |
||
64 |
//provide common default formats |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
65 |
public String formatDiagnostic(JCDiagnostic d, Locale l) { |
942 | 66 |
try { |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
67 |
StringBuilder buf = new StringBuilder(); |
942 | 68 |
if (d.getPosition() != Position.NOPOS) { |
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
69 |
buf.append(formatSource(d, false, null)); |
942 | 70 |
buf.append(':'); |
71 |
buf.append(formatPosition(d, LINE, null)); |
|
72 |
buf.append(':'); |
|
73 |
buf.append(formatPosition(d, COLUMN, null)); |
|
74 |
buf.append(':'); |
|
75 |
} |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
76 |
else if (d.getSource() != null && d.getSource().getKind() == JavaFileObject.Kind.CLASS) { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
77 |
buf.append(formatSource(d, false, null)); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
78 |
buf.append(":-:-:"); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
79 |
} |
942 | 80 |
else |
81 |
buf.append('-'); |
|
82 |
buf.append(' '); |
|
83 |
buf.append(formatMessage(d, null)); |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
84 |
if (displaySource(d)) { |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
85 |
buf.append("\n"); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
86 |
buf.append(formatSourceLine(d, 0)); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
87 |
} |
942 | 88 |
return buf.toString(); |
89 |
} |
|
90 |
catch (Exception e) { |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
91 |
//e.printStackTrace(); |
942 | 92 |
return null; |
93 |
} |
|
94 |
} |
|
95 |
||
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
96 |
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
|
97 |
StringBuilder buf = new StringBuilder(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
List<String> subDiags = formatSubdiagnostics(d, null); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
102 |
if (subDiags.nonEmpty()) { |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
103 |
String sep = ""; |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
104 |
buf.append(",{"); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
105 |
for (String sub : formatSubdiagnostics(d, null)) { |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
106 |
buf.append(sep); |
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
107 |
buf.append("("); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
108 |
buf.append(sub); |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
5847
diff
changeset
|
109 |
buf.append(")"); |
2085
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
110 |
sep = ","; |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
111 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
112 |
buf.append('}'); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
113 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
114 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
115 |
return buf.toString(); |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
116 |
} |
4792e12a8ca2
6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents:
1591
diff
changeset
|
117 |
|
942 | 118 |
@Override |
119 |
protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) { |
|
120 |
String s; |
|
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
121 |
if (arg instanceof Formattable) { |
942 | 122 |
s = arg.toString(); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
123 |
} else if (arg instanceof JCExpression) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
124 |
JCExpression tree = (JCExpression)arg; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
125 |
s = "@" + tree.getStartPosition(); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
126 |
} else if (arg instanceof BaseFileObject) { |
3995
73af8b6fb8bc
6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
3380
diff
changeset
|
127 |
s = ((BaseFileObject) arg).getShortName(); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
128 |
} else { |
942 | 129 |
s = super.formatArgument(diag, arg, null); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
130 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
7681
diff
changeset
|
131 |
return (arg instanceof JCDiagnostic) ? "(" + s + ")" : s; |
942 | 132 |
} |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
133 |
|
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
134 |
@Override |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
135 |
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
|
136 |
StringBuilder buf = new StringBuilder(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
137 |
buf.append(key); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
138 |
String sep = ": "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
139 |
for (Object o : args) { |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
140 |
buf.append(sep); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
141 |
buf.append(o); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
142 |
sep = ", "; |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
143 |
} |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
144 |
return buf.toString(); |
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2085
diff
changeset
|
145 |
} |
2984
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
146 |
|
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
147 |
@Override |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
148 |
public boolean isRaw() { |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
149 |
return true; |
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
2223
diff
changeset
|
150 |
} |
942 | 151 |
} |