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

Thu, 09 Oct 2008 16:19:13 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:19:13 +0100
changeset 137
e4eaddca54b7
parent 136
8eafba4f61be
child 168
4cdaaf4c5dca
permissions
-rw-r--r--

6731573: diagnostic output should optionally include source line
Summary: Added an -XD option to optionally prints out source lines in error messages
Reviewed-by: jjg

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

mercurial