author | mcimadamore |
Thu, 09 Oct 2008 16:19:13 +0100 | |
changeset 1472 | 1e09e143438c |
parent 1471 | 57506cdfb7b4 |
child 1870 | 57a1138dffc8 |
permissions | -rw-r--r-- |
942 | 1 |
/* |
2 |
* Copyright 2005-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 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.util.HashMap; |
|
29 |
import java.util.Locale; |
|
30 |
import java.util.Map; |
|
31 |
import javax.tools.JavaFileObject; |
|
32 |
||
33 |
import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicFormatKind.*; |
|
34 |
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*; |
|
35 |
||
36 |
/** |
|
37 |
* A basic formatter for diagnostic messages. |
|
38 |
* The basic formatter will format a diagnostic according to one of three format patterns, depending on whether |
|
39 |
* or not the source name and position are set. The formatter supports a printf-like string for patterns |
|
40 |
* with the following special characters: |
|
41 |
* <ul> |
|
42 |
* <li>%b: the base of the source name |
|
43 |
* <li>%f: the source name (full absolute path) |
|
44 |
* <li>%l: the line number of the diagnostic, derived from the character offset |
|
45 |
* <li>%c: the column number of the diagnostic, derived from the character offset |
|
46 |
* <li>%o: the character offset of the diagnostic if set |
|
47 |
* <li>%p: the prefix for the diagnostic, derived from the diagnostic type |
|
48 |
* <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is |
|
49 |
* shown if the type is ERROR and if a source name is set |
|
50 |
* <li>%m: the text or the diagnostic, including any appropriate arguments |
|
51 |
* <li>%_: space delimiter, useful for formatting purposes |
|
52 |
* </ul> |
|
53 |
*/ |
|
54 |
public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter { |
|
55 |
||
56 |
protected Map<BasicFormatKind, String> availableFormats; |
|
57 |
||
58 |
/** |
|
59 |
* Create a basic formatter based on the supplied options. |
|
60 |
* |
|
61 |
* @param opts list of command-line options |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
62 |
* @param msgs JavacMessages object used for i18n |
942 | 63 |
*/ |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
64 |
BasicDiagnosticFormatter(Options opts, JavacMessages msgs) { |
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
65 |
super(msgs, opts, true); |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
66 |
initAvailableFormats(); |
942 | 67 |
String fmt = opts.get("diags"); |
68 |
if (fmt != null) { |
|
69 |
String[] formats = fmt.split("\\|"); |
|
70 |
switch (formats.length) { |
|
71 |
case 3: |
|
72 |
availableFormats.put(DEFAULT_CLASS_FORMAT, formats[2]); |
|
73 |
case 2: |
|
74 |
availableFormats.put(DEFAULT_NO_POS_FORMAT, formats[1]); |
|
75 |
default: |
|
76 |
availableFormats.put(DEFAULT_POS_FORMAT, formats[0]); |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Create a standard basic formatter |
|
83 |
* |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
84 |
* @param msgs JavacMessages object used for i18n |
942 | 85 |
*/ |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
86 |
public BasicDiagnosticFormatter(JavacMessages msgs) { |
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
87 |
super(msgs, true); |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
88 |
initAvailableFormats(); |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
89 |
} |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
90 |
|
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
91 |
public void initAvailableFormats() { |
942 | 92 |
availableFormats = new HashMap<BasicFormatKind, String>(); |
93 |
availableFormats.put(DEFAULT_POS_FORMAT, "%f:%l:%_%t%m"); |
|
94 |
availableFormats.put(DEFAULT_NO_POS_FORMAT, "%p%m"); |
|
95 |
availableFormats.put(DEFAULT_CLASS_FORMAT, "%f:%_%t%m"); |
|
96 |
} |
|
97 |
||
98 |
public String format(JCDiagnostic d, Locale l) { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
99 |
if (l == null) |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1109
diff
changeset
|
100 |
l = messages.getCurrentLocale(); |
942 | 101 |
String format = selectFormat(d); |
102 |
StringBuilder buf = new StringBuilder(); |
|
103 |
for (int i = 0; i < format.length(); i++) { |
|
104 |
char c = format.charAt(i); |
|
105 |
boolean meta = false; |
|
106 |
if (c == '%' && i < format.length() - 1) { |
|
107 |
meta = true; |
|
108 |
c = format.charAt(++i); |
|
109 |
} |
|
110 |
buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c)); |
|
111 |
} |
|
1472
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
112 |
if (displaySource(d)) { |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
113 |
buf.append("\n" + formatSourceLine(d)); |
1e09e143438c
6731573: diagnostic output should optionally include source line
mcimadamore
parents:
1471
diff
changeset
|
114 |
} |
942 | 115 |
return buf.toString(); |
116 |
} |
|
117 |
||
118 |
protected String formatMeta(char c, JCDiagnostic d, Locale l) { |
|
119 |
switch (c) { |
|
120 |
case 'b': |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
121 |
return formatSource(d, false, l); |
942 | 122 |
case 'e': |
123 |
return formatPosition(d, END, l); |
|
124 |
case 'f': |
|
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
125 |
return formatSource(d, true, l); |
942 | 126 |
case 'l': |
127 |
return formatPosition(d, LINE, l); |
|
128 |
case 'c': |
|
129 |
return formatPosition(d, COLUMN, l); |
|
130 |
case 'o': |
|
131 |
return formatPosition(d, OFFSET, l); |
|
132 |
case 'p': |
|
133 |
return formatKind(d, l); |
|
134 |
case 's': |
|
135 |
return formatPosition(d, START, l); |
|
136 |
case 't': { |
|
137 |
boolean usePrefix; |
|
138 |
switch (d.getType()) { |
|
139 |
case FRAGMENT: |
|
140 |
usePrefix = false; |
|
141 |
break; |
|
142 |
case ERROR: |
|
143 |
usePrefix = (d.getIntPosition() == Position.NOPOS); |
|
144 |
break; |
|
145 |
default: |
|
146 |
usePrefix = true; |
|
147 |
} |
|
148 |
if (usePrefix) |
|
149 |
return formatKind(d, l); |
|
150 |
else |
|
151 |
return ""; |
|
152 |
} |
|
153 |
case 'm': |
|
154 |
return formatMessage(d, l); |
|
155 |
case '_': |
|
156 |
return " "; |
|
157 |
case '%': |
|
158 |
return "%"; |
|
159 |
default: |
|
160 |
return String.valueOf(c); |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
private String selectFormat(JCDiagnostic d) { |
|
165 |
DiagnosticSource source = d.getDiagnosticSource(); |
|
166 |
String format = availableFormats.get(DEFAULT_NO_POS_FORMAT); |
|
167 |
if (source != null) { |
|
168 |
if (d.getIntPosition() != Position.NOPOS) { |
|
169 |
format = availableFormats.get(DEFAULT_POS_FORMAT); |
|
170 |
} else if (source.getFile() != null && |
|
171 |
source.getFile().getKind() == JavaFileObject.Kind.CLASS) { |
|
172 |
format = availableFormats.get(DEFAULT_CLASS_FORMAT); |
|
173 |
} |
|
174 |
} |
|
175 |
return format; |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* This enum contains all the kinds of formatting patterns supported |
|
180 |
* by a basic diagnostic formatter. |
|
181 |
*/ |
|
182 |
public enum BasicFormatKind { |
|
183 |
/** |
|
184 |
* A format string to be used for diagnostics with a given position. |
|
185 |
*/ |
|
186 |
DEFAULT_POS_FORMAT, |
|
187 |
/** |
|
188 |
* A format string to be used for diagnostics without a given position. |
|
189 |
*/ |
|
190 |
DEFAULT_NO_POS_FORMAT, |
|
191 |
/** |
|
192 |
* A format string to be used for diagnostics regarding classfiles |
|
193 |
*/ |
|
194 |
DEFAULT_CLASS_FORMAT; |
|
195 |
} |
|
196 |
} |