src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java

Fri, 24 Oct 2008 12:46:34 +0100

author
mcimadamore
date
Fri, 24 Oct 2008 12:46:34 +0100
changeset 161
ddd75a295501
parent 137
e4eaddca54b7
child 198
b4b1f7732289
permissions
-rw-r--r--

6758789: Some method resolution diagnostic should be improved
Summary: Recent work on diagnostics left out some resolution corner cases
Reviewed-by: jjg

mcimadamore@83 1 /*
mcimadamore@83 2 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
mcimadamore@83 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@83 4 *
mcimadamore@83 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@83 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@83 7 * published by the Free Software Foundation. Sun designates this
mcimadamore@83 8 * particular file as subject to the "Classpath" exception as provided
mcimadamore@83 9 * by Sun in the LICENSE file that accompanied this code.
mcimadamore@83 10 *
mcimadamore@83 11 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@83 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@83 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@83 14 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@83 15 * accompanied this code).
mcimadamore@83 16 *
mcimadamore@83 17 * You should have received a copy of the GNU General Public License version
mcimadamore@83 18 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@83 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@83 20 *
mcimadamore@83 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
mcimadamore@83 22 * CA 95054 USA or visit www.sun.com if you need additional information or
mcimadamore@83 23 * have any questions.
mcimadamore@83 24 */
mcimadamore@83 25
mcimadamore@83 26 package com.sun.tools.javac.util;
mcimadamore@83 27
mcimadamore@83 28 import java.util.HashMap;
mcimadamore@83 29 import java.util.Locale;
mcimadamore@83 30 import java.util.Map;
mcimadamore@83 31 import javax.tools.JavaFileObject;
mcimadamore@83 32
mcimadamore@83 33 import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicFormatKind.*;
mcimadamore@83 34 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
mcimadamore@83 35
mcimadamore@83 36 /**
mcimadamore@83 37 * A basic formatter for diagnostic messages.
mcimadamore@83 38 * The basic formatter will format a diagnostic according to one of three format patterns, depending on whether
mcimadamore@83 39 * or not the source name and position are set. The formatter supports a printf-like string for patterns
mcimadamore@83 40 * with the following special characters:
mcimadamore@83 41 * <ul>
mcimadamore@83 42 * <li>%b: the base of the source name
mcimadamore@83 43 * <li>%f: the source name (full absolute path)
mcimadamore@83 44 * <li>%l: the line number of the diagnostic, derived from the character offset
mcimadamore@83 45 * <li>%c: the column number of the diagnostic, derived from the character offset
mcimadamore@83 46 * <li>%o: the character offset of the diagnostic if set
mcimadamore@83 47 * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
mcimadamore@83 48 * <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is
mcimadamore@83 49 * shown if the type is ERROR and if a source name is set
mcimadamore@83 50 * <li>%m: the text or the diagnostic, including any appropriate arguments
mcimadamore@83 51 * <li>%_: space delimiter, useful for formatting purposes
mcimadamore@83 52 * </ul>
mcimadamore@83 53 */
mcimadamore@83 54 public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter {
mcimadamore@83 55
mcimadamore@83 56 protected Map<BasicFormatKind, String> availableFormats;
mcimadamore@83 57
mcimadamore@83 58 /**
mcimadamore@83 59 * Create a basic formatter based on the supplied options.
mcimadamore@83 60 *
mcimadamore@83 61 * @param opts list of command-line options
mcimadamore@136 62 * @param msgs JavacMessages object used for i18n
mcimadamore@83 63 */
mcimadamore@136 64 BasicDiagnosticFormatter(Options opts, JavacMessages msgs) {
mcimadamore@137 65 super(msgs, opts, true);
mcimadamore@137 66 initAvailableFormats();
mcimadamore@83 67 String fmt = opts.get("diags");
mcimadamore@83 68 if (fmt != null) {
mcimadamore@83 69 String[] formats = fmt.split("\\|");
mcimadamore@83 70 switch (formats.length) {
mcimadamore@83 71 case 3:
mcimadamore@83 72 availableFormats.put(DEFAULT_CLASS_FORMAT, formats[2]);
mcimadamore@83 73 case 2:
mcimadamore@83 74 availableFormats.put(DEFAULT_NO_POS_FORMAT, formats[1]);
mcimadamore@83 75 default:
mcimadamore@83 76 availableFormats.put(DEFAULT_POS_FORMAT, formats[0]);
mcimadamore@83 77 }
mcimadamore@83 78 }
mcimadamore@83 79 }
mcimadamore@83 80
mcimadamore@83 81 /**
mcimadamore@83 82 * Create a standard basic formatter
mcimadamore@83 83 *
mcimadamore@136 84 * @param msgs JavacMessages object used for i18n
mcimadamore@83 85 */
mcimadamore@136 86 public BasicDiagnosticFormatter(JavacMessages msgs) {
mcimadamore@137 87 super(msgs, true);
mcimadamore@137 88 initAvailableFormats();
mcimadamore@137 89 }
mcimadamore@137 90
mcimadamore@137 91 public void initAvailableFormats() {
mcimadamore@83 92 availableFormats = new HashMap<BasicFormatKind, String>();
mcimadamore@83 93 availableFormats.put(DEFAULT_POS_FORMAT, "%f:%l:%_%t%m");
mcimadamore@83 94 availableFormats.put(DEFAULT_NO_POS_FORMAT, "%p%m");
mcimadamore@83 95 availableFormats.put(DEFAULT_CLASS_FORMAT, "%f:%_%t%m");
mcimadamore@83 96 }
mcimadamore@83 97
mcimadamore@83 98 public String format(JCDiagnostic d, Locale l) {
mcimadamore@136 99 if (l == null)
mcimadamore@136 100 l = messages.getCurrentLocale();
mcimadamore@83 101 String format = selectFormat(d);
mcimadamore@83 102 StringBuilder buf = new StringBuilder();
mcimadamore@83 103 for (int i = 0; i < format.length(); i++) {
mcimadamore@83 104 char c = format.charAt(i);
mcimadamore@83 105 boolean meta = false;
mcimadamore@83 106 if (c == '%' && i < format.length() - 1) {
mcimadamore@83 107 meta = true;
mcimadamore@83 108 c = format.charAt(++i);
mcimadamore@83 109 }
mcimadamore@83 110 buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
mcimadamore@83 111 }
mcimadamore@137 112 if (displaySource(d)) {
mcimadamore@137 113 buf.append("\n" + formatSourceLine(d));
mcimadamore@137 114 }
mcimadamore@83 115 return buf.toString();
mcimadamore@83 116 }
mcimadamore@83 117
mcimadamore@83 118 protected String formatMeta(char c, JCDiagnostic d, Locale l) {
mcimadamore@83 119 switch (c) {
mcimadamore@83 120 case 'b':
mcimadamore@100 121 return formatSource(d, false, l);
mcimadamore@83 122 case 'e':
mcimadamore@83 123 return formatPosition(d, END, l);
mcimadamore@83 124 case 'f':
mcimadamore@100 125 return formatSource(d, true, l);
mcimadamore@83 126 case 'l':
mcimadamore@83 127 return formatPosition(d, LINE, l);
mcimadamore@83 128 case 'c':
mcimadamore@83 129 return formatPosition(d, COLUMN, l);
mcimadamore@83 130 case 'o':
mcimadamore@83 131 return formatPosition(d, OFFSET, l);
mcimadamore@83 132 case 'p':
mcimadamore@83 133 return formatKind(d, l);
mcimadamore@83 134 case 's':
mcimadamore@83 135 return formatPosition(d, START, l);
mcimadamore@83 136 case 't': {
mcimadamore@83 137 boolean usePrefix;
mcimadamore@83 138 switch (d.getType()) {
mcimadamore@83 139 case FRAGMENT:
mcimadamore@83 140 usePrefix = false;
mcimadamore@83 141 break;
mcimadamore@83 142 case ERROR:
mcimadamore@83 143 usePrefix = (d.getIntPosition() == Position.NOPOS);
mcimadamore@83 144 break;
mcimadamore@83 145 default:
mcimadamore@83 146 usePrefix = true;
mcimadamore@83 147 }
mcimadamore@83 148 if (usePrefix)
mcimadamore@83 149 return formatKind(d, l);
mcimadamore@83 150 else
mcimadamore@83 151 return "";
mcimadamore@83 152 }
mcimadamore@83 153 case 'm':
mcimadamore@83 154 return formatMessage(d, l);
mcimadamore@83 155 case '_':
mcimadamore@83 156 return " ";
mcimadamore@83 157 case '%':
mcimadamore@83 158 return "%";
mcimadamore@83 159 default:
mcimadamore@83 160 return String.valueOf(c);
mcimadamore@83 161 }
mcimadamore@83 162 }
mcimadamore@83 163
mcimadamore@83 164 private String selectFormat(JCDiagnostic d) {
mcimadamore@83 165 DiagnosticSource source = d.getDiagnosticSource();
mcimadamore@83 166 String format = availableFormats.get(DEFAULT_NO_POS_FORMAT);
mcimadamore@83 167 if (source != null) {
mcimadamore@83 168 if (d.getIntPosition() != Position.NOPOS) {
mcimadamore@83 169 format = availableFormats.get(DEFAULT_POS_FORMAT);
mcimadamore@83 170 } else if (source.getFile() != null &&
mcimadamore@83 171 source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
mcimadamore@83 172 format = availableFormats.get(DEFAULT_CLASS_FORMAT);
mcimadamore@83 173 }
mcimadamore@83 174 }
mcimadamore@83 175 return format;
mcimadamore@83 176 }
mcimadamore@83 177
mcimadamore@83 178 /**
mcimadamore@83 179 * This enum contains all the kinds of formatting patterns supported
mcimadamore@83 180 * by a basic diagnostic formatter.
mcimadamore@83 181 */
mcimadamore@83 182 public enum BasicFormatKind {
mcimadamore@83 183 /**
mcimadamore@83 184 * A format string to be used for diagnostics with a given position.
mcimadamore@83 185 */
mcimadamore@83 186 DEFAULT_POS_FORMAT,
mcimadamore@83 187 /**
mcimadamore@83 188 * A format string to be used for diagnostics without a given position.
mcimadamore@83 189 */
mcimadamore@83 190 DEFAULT_NO_POS_FORMAT,
mcimadamore@83 191 /**
mcimadamore@83 192 * A format string to be used for diagnostics regarding classfiles
mcimadamore@83 193 */
mcimadamore@83 194 DEFAULT_CLASS_FORMAT;
mcimadamore@83 195 }
mcimadamore@83 196 }

mercurial