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

Mon, 28 Jul 2008 10:22:10 +0100

author
mcimadamore
date
Mon, 28 Jul 2008 10:22:10 +0100
changeset 83
37470f5ea179
child 100
37551dc0f591
permissions
-rw-r--r--

6720185: DiagnosticFormatter refactoring
Summary: Brand new hierarchy of diagnostic formatters for achieving better reusability
Reviewed-by: jjg

mcimadamore@83 1 /*
mcimadamore@83 2 * Copyright 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 package com.sun.tools.javac.util;
mcimadamore@83 26
mcimadamore@83 27 import java.util.Collection;
mcimadamore@83 28 import java.util.Locale;
mcimadamore@83 29 import javax.tools.JavaFileObject;
mcimadamore@83 30
mcimadamore@83 31 import com.sun.tools.javac.api.DiagnosticFormatter;
mcimadamore@83 32 import com.sun.tools.javac.api.Formattable;
mcimadamore@83 33 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
mcimadamore@83 34 import com.sun.tools.javac.file.JavacFileManager;
mcimadamore@83 35
mcimadamore@83 36 /**
mcimadamore@83 37 * This abstract class provides a basic implementation of the functionalities that should be provided
mcimadamore@83 38 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
mcimadamore@83 39 *
mcimadamore@83 40 * <ul>
mcimadamore@83 41 * <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
mcimadamore@83 42 * Those implementations are specifically targeting JCDiagnostic objects.
mcimadamore@83 43 * <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
mcimadamore@83 44 * <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
mcimadamore@83 45 * <ul>
mcimadamore@83 46 *
mcimadamore@83 47 */
mcimadamore@83 48 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
mcimadamore@83 49
mcimadamore@83 50 /**
mcimadamore@83 51 * Messages object used by this formatter for i18n
mcimadamore@83 52 */
mcimadamore@83 53 protected Messages messages;
mcimadamore@83 54
mcimadamore@83 55 /**
mcimadamore@83 56 * Initialize an AbstractDiagnosticFormatter by setting its Messages object
mcimadamore@83 57 * @param messages
mcimadamore@83 58 */
mcimadamore@83 59 protected AbstractDiagnosticFormatter(Messages messages) {
mcimadamore@83 60 this.messages = messages;
mcimadamore@83 61 }
mcimadamore@83 62
mcimadamore@83 63 public String formatMessage(JCDiagnostic d, Locale l) {
mcimadamore@83 64 //this code should rely on the locale settings but it's not! See RFE 6443132
mcimadamore@83 65 Collection<String> args = formatArguments(d, l);
mcimadamore@83 66 return localize(l, d.getCode(), args.toArray());
mcimadamore@83 67 }
mcimadamore@83 68
mcimadamore@83 69 public String formatKind(JCDiagnostic d, Locale l) {
mcimadamore@83 70 switch (d.getType()) {
mcimadamore@83 71 case FRAGMENT: return "";
mcimadamore@83 72 case NOTE: return localize(l, "compiler.note.note");
mcimadamore@83 73 case WARNING: return localize(l, "compiler.warn.warning");
mcimadamore@83 74 case ERROR: return localize(l, "compiler.err.error");
mcimadamore@83 75 default:
mcimadamore@83 76 throw new AssertionError("Unknown diagnostic type: " + d.getType());
mcimadamore@83 77 }
mcimadamore@83 78 }
mcimadamore@83 79
mcimadamore@83 80 public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
mcimadamore@83 81 assert (d.getPosition() != Position.NOPOS);
mcimadamore@83 82 return String.valueOf(getPosition(d, pk));
mcimadamore@83 83 }
mcimadamore@83 84 //WHERE
mcimadamore@83 85 public long getPosition(JCDiagnostic d, PositionKind pk) {
mcimadamore@83 86 switch (pk) {
mcimadamore@83 87 case START: return d.getIntStartPosition();
mcimadamore@83 88 case END: return d.getIntEndPosition();
mcimadamore@83 89 case LINE: return d.getLineNumber();
mcimadamore@83 90 case COLUMN: return d.getColumnNumber();
mcimadamore@83 91 case OFFSET: return d.getIntPosition();
mcimadamore@83 92 default:
mcimadamore@83 93 throw new AssertionError("Unknown diagnostic position: " + pk);
mcimadamore@83 94 }
mcimadamore@83 95 }
mcimadamore@83 96
mcimadamore@83 97 public String formatSource(JCDiagnostic d,Locale l) {
mcimadamore@83 98 assert (d.getSource() != null);
mcimadamore@83 99 return d.getSource().getName();
mcimadamore@83 100 }
mcimadamore@83 101
mcimadamore@83 102 /**
mcimadamore@83 103 * Format the arguments of a given diagnostic.
mcimadamore@83 104 *
mcimadamore@83 105 * @param d diagnostic whose arguments are to be formatted
mcimadamore@83 106 * @param l locale object to be used for i18n
mcimadamore@83 107 * @return a Collection whose elements are the formatted arguments of the diagnostic
mcimadamore@83 108 */
mcimadamore@83 109 protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
mcimadamore@83 110 ListBuffer<String> buf = new ListBuffer<String>();
mcimadamore@83 111 for (Object o : d.getArgs()) {
mcimadamore@83 112 buf.append(formatArgument(d, o, l));
mcimadamore@83 113 }
mcimadamore@83 114 return buf.toList();
mcimadamore@83 115 }
mcimadamore@83 116
mcimadamore@83 117 /**
mcimadamore@83 118 * Format a single argument of a given diagnostic.
mcimadamore@83 119 *
mcimadamore@83 120 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 121 * @param arg argument to be formatted
mcimadamore@83 122 * @param l locale object to be used for i18n
mcimadamore@83 123 * @return string representation of the diagnostic argument
mcimadamore@83 124 */
mcimadamore@83 125 protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
mcimadamore@83 126 if (arg instanceof JCDiagnostic)
mcimadamore@83 127 return format((JCDiagnostic)arg, l);
mcimadamore@83 128 else if (arg instanceof Iterable<?>) {
mcimadamore@83 129 return formatIterable(d, (Iterable<?>)arg, l);
mcimadamore@83 130 }
mcimadamore@83 131 else if (arg instanceof JavaFileObject)
mcimadamore@83 132 return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
mcimadamore@83 133 else if (arg instanceof Formattable)
mcimadamore@83 134 return ((Formattable)arg).toString(Messages.getDefaultBundle());
mcimadamore@83 135 else
mcimadamore@83 136 return String.valueOf(arg);
mcimadamore@83 137 }
mcimadamore@83 138
mcimadamore@83 139 /**
mcimadamore@83 140 * Format an iterable argument of a given diagnostic.
mcimadamore@83 141 *
mcimadamore@83 142 * @param d diagnostic whose argument is to be formatted
mcimadamore@83 143 * @param it iterable argument to be formatted
mcimadamore@83 144 * @param l locale object to be used for i18n
mcimadamore@83 145 * @return string representation of the diagnostic iterable argument
mcimadamore@83 146 */
mcimadamore@83 147 protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
mcimadamore@83 148 StringBuilder sbuf = new StringBuilder();
mcimadamore@83 149 String sep = "";
mcimadamore@83 150 for (Object o : it) {
mcimadamore@83 151 sbuf.append(sep);
mcimadamore@83 152 sbuf.append(formatArgument(d, o, l));
mcimadamore@83 153 sep = ",";
mcimadamore@83 154 }
mcimadamore@83 155 return sbuf.toString();
mcimadamore@83 156 }
mcimadamore@83 157
mcimadamore@83 158 /**
mcimadamore@83 159 * Converts a String into a locale-dependent representation accordingly to a given locale
mcimadamore@83 160 *
mcimadamore@83 161 * @param l locale object to be used for i18n
mcimadamore@83 162 * @param key locale-independent key used for looking up in a resource file
mcimadamore@83 163 * @param args localization arguments
mcimadamore@83 164 * @return a locale-dependent string
mcimadamore@83 165 */
mcimadamore@83 166 protected String localize(Locale l, String key, Object... args) {
mcimadamore@83 167 return messages.getLocalizedString(key, args);
mcimadamore@83 168 }
mcimadamore@83 169 }

mercurial