Fri, 29 Aug 2008 11:10:12 -0700
6597471: unused imports in javax.tools.JavaCompiler
6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java
Reviewed-by: mcimadamore
Contributed-by: davide.angelocola@gmail.com
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 */
26 package com.sun.tools.javac.util;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31 import javax.tools.JavaFileObject;
33 import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicFormatKind.*;
34 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
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 {
56 protected Map<BasicFormatKind, String> availableFormats;
58 /**
59 * Create a basic formatter based on the supplied options.
60 *
61 * @param opts list of command-line options
62 * @param msgs Messages object used for i18n
63 */
64 BasicDiagnosticFormatter(Options opts, Messages msgs) {
65 this(msgs); //common init
66 String fmt = opts.get("diags");
67 if (fmt != null) {
68 String[] formats = fmt.split("\\|");
69 switch (formats.length) {
70 case 3:
71 availableFormats.put(DEFAULT_CLASS_FORMAT, formats[2]);
72 case 2:
73 availableFormats.put(DEFAULT_NO_POS_FORMAT, formats[1]);
74 default:
75 availableFormats.put(DEFAULT_POS_FORMAT, formats[0]);
76 }
77 }
78 }
80 /**
81 * Create a standard basic formatter
82 *
83 * @param msgs Messages object used for i18n
84 */
85 public BasicDiagnosticFormatter(Messages msgs) {
86 super(msgs);
87 availableFormats = new HashMap<BasicFormatKind, String>();
88 availableFormats.put(DEFAULT_POS_FORMAT, "%f:%l:%_%t%m");
89 availableFormats.put(DEFAULT_NO_POS_FORMAT, "%p%m");
90 availableFormats.put(DEFAULT_CLASS_FORMAT, "%f:%_%t%m");
91 }
93 public String format(JCDiagnostic d, Locale l) {
94 String format = selectFormat(d);
95 StringBuilder buf = new StringBuilder();
96 for (int i = 0; i < format.length(); i++) {
97 char c = format.charAt(i);
98 boolean meta = false;
99 if (c == '%' && i < format.length() - 1) {
100 meta = true;
101 c = format.charAt(++i);
102 }
103 buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
104 }
105 return buf.toString();
106 }
108 protected String formatMeta(char c, JCDiagnostic d, Locale l) {
109 switch (c) {
110 case 'b':
111 return formatSource(d, false, l);
112 case 'e':
113 return formatPosition(d, END, l);
114 case 'f':
115 return formatSource(d, true, l);
116 case 'l':
117 return formatPosition(d, LINE, l);
118 case 'c':
119 return formatPosition(d, COLUMN, l);
120 case 'o':
121 return formatPosition(d, OFFSET, l);
122 case 'p':
123 return formatKind(d, l);
124 case 's':
125 return formatPosition(d, START, l);
126 case 't': {
127 boolean usePrefix;
128 switch (d.getType()) {
129 case FRAGMENT:
130 usePrefix = false;
131 break;
132 case ERROR:
133 usePrefix = (d.getIntPosition() == Position.NOPOS);
134 break;
135 default:
136 usePrefix = true;
137 }
138 if (usePrefix)
139 return formatKind(d, l);
140 else
141 return "";
142 }
143 case 'm':
144 return formatMessage(d, l);
145 case '_':
146 return " ";
147 case '%':
148 return "%";
149 default:
150 return String.valueOf(c);
151 }
152 }
154 private String selectFormat(JCDiagnostic d) {
155 DiagnosticSource source = d.getDiagnosticSource();
156 String format = availableFormats.get(DEFAULT_NO_POS_FORMAT);
157 if (source != null) {
158 if (d.getIntPosition() != Position.NOPOS) {
159 format = availableFormats.get(DEFAULT_POS_FORMAT);
160 } else if (source.getFile() != null &&
161 source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
162 format = availableFormats.get(DEFAULT_CLASS_FORMAT);
163 }
164 }
165 return format;
166 }
168 public boolean displaySource(JCDiagnostic d) {
169 return true;
170 }
172 /**
173 * This enum contains all the kinds of formatting patterns supported
174 * by a basic diagnostic formatter.
175 */
176 public enum BasicFormatKind {
177 /**
178 * A format string to be used for diagnostics with a given position.
179 */
180 DEFAULT_POS_FORMAT,
181 /**
182 * A format string to be used for diagnostics without a given position.
183 */
184 DEFAULT_NO_POS_FORMAT,
185 /**
186 * A format string to be used for diagnostics regarding classfiles
187 */
188 DEFAULT_CLASS_FORMAT;
189 }
190 }