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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 612
d1bd93028447
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 package com.sun.tools.javac.util;
    27 import java.util.Arrays;
    28 import java.util.Collection;
    29 import java.util.EnumSet;
    30 import java.util.HashMap;
    31 import java.util.Locale;
    32 import java.util.Map;
    33 import java.util.Set;
    34 import javax.tools.JavaFileObject;
    36 import com.sun.tools.javac.api.DiagnosticFormatter;
    37 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
    38 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.MultilineLimit;
    39 import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
    40 import com.sun.tools.javac.api.Formattable;
    41 import com.sun.tools.javac.code.Printer;
    42 import com.sun.tools.javac.code.Symbol;
    43 import com.sun.tools.javac.code.Type;
    44 import com.sun.tools.javac.code.Type.CapturedType;
    46 import com.sun.tools.javac.file.BaseFileObject;
    47 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
    49 /**
    50  * This abstract class provides a basic implementation of the functionalities that should be provided
    51  * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
    52  *
    53  * <ul>
    54  *  <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
    55  *  Those implementations are specifically targeting JCDiagnostic objects.
    56  *  <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
    57  *  <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
    58  * <ul>
    59  *
    60  * <p><b>This is NOT part of any supported API.
    61  * If you write code that depends on this, you do so at your own risk.
    62  * This code and its internal interfaces are subject to change or
    63  * deletion without notice.</b>
    64  */
    65 public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
    67     /**
    68      * JavacMessages object used by this formatter for i18n.
    69      */
    70     protected JavacMessages messages;
    72     /**
    73      * Configuration object used by this formatter
    74      */
    75     private SimpleConfiguration config;
    77     /**
    78      * Current depth level of the disgnostic being formatted
    79      * (!= 0 for subdiagnostics)
    80      */
    81     protected int depth = 0;
    83     /**
    84      * All captured types that have been encountered during diagnostic formatting.
    85      * This info is used by the FormatterPrinter in order to print friendly unique
    86      * ids for captured types
    87      */
    88     private List<Type> allCaptured = List.nil();
    90     /**
    91      * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object.
    92      * @param messages
    93      */
    94     protected AbstractDiagnosticFormatter(JavacMessages messages, SimpleConfiguration config) {
    95         this.messages = messages;
    96         this.config = config;
    97     }
    99     public String formatKind(JCDiagnostic d, Locale l) {
   100         switch (d.getType()) {
   101             case FRAGMENT: return "";
   102             case NOTE:     return localize(l, "compiler.note.note");
   103             case WARNING:  return localize(l, "compiler.warn.warning");
   104             case ERROR:    return localize(l, "compiler.err.error");
   105             default:
   106                 throw new AssertionError("Unknown diagnostic type: " + d.getType());
   107         }
   108     }
   110     @Override
   111     public String format(JCDiagnostic d, Locale locale) {
   112         allCaptured = List.nil();
   113         return formatDiagnostic(d, locale);
   114     }
   116     protected abstract String formatDiagnostic(JCDiagnostic d, Locale locale);
   118     public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
   119         assert (d.getPosition() != Position.NOPOS);
   120         return String.valueOf(getPosition(d, pk));
   121     }
   122     //where
   123     private long getPosition(JCDiagnostic d, PositionKind pk) {
   124         switch (pk) {
   125             case START: return d.getIntStartPosition();
   126             case END: return d.getIntEndPosition();
   127             case LINE: return d.getLineNumber();
   128             case COLUMN: return d.getColumnNumber();
   129             case OFFSET: return d.getIntPosition();
   130             default:
   131                 throw new AssertionError("Unknown diagnostic position: " + pk);
   132         }
   133     }
   135     public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
   136         JavaFileObject fo = d.getSource();
   137         if (fo == null)
   138             throw new IllegalArgumentException(); // d should have source set
   139         if (fullname)
   140             return fo.getName();
   141         else if (fo instanceof BaseFileObject)
   142             return ((BaseFileObject) fo).getShortName();
   143         else
   144             return BaseFileObject.getSimpleName(fo);
   145     }
   147     /**
   148      * Format the arguments of a given diagnostic.
   149      *
   150      * @param d diagnostic whose arguments are to be formatted
   151      * @param l locale object to be used for i18n
   152      * @return a Collection whose elements are the formatted arguments of the diagnostic
   153      */
   154     protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
   155         ListBuffer<String> buf = new ListBuffer<String>();
   156         for (Object o : d.getArgs()) {
   157            buf.append(formatArgument(d, o, l));
   158         }
   159         return buf.toList();
   160     }
   162     /**
   163      * Format a single argument of a given diagnostic.
   164      *
   165      * @param d diagnostic whose argument is to be formatted
   166      * @param arg argument to be formatted
   167      * @param l locale object to be used for i18n
   168      * @return string representation of the diagnostic argument
   169      */
   170     protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
   171         if (arg instanceof JCDiagnostic) {
   172             String s = null;
   173             depth++;
   174             try {
   175                 s = formatMessage((JCDiagnostic)arg, l);
   176             }
   177             finally {
   178                 depth--;
   179             }
   180             return s;
   181         }
   182         else if (arg instanceof Iterable<?>) {
   183             return formatIterable(d, (Iterable<?>)arg, l);
   184         }
   185         else if (arg instanceof Type) {
   186             return printer.visit((Type)arg, l);
   187         }
   188         else if (arg instanceof Symbol) {
   189             return printer.visit((Symbol)arg, l);
   190         }
   191         else if (arg instanceof JavaFileObject) {
   192             return ((JavaFileObject)arg).getName();
   193         }
   194         else if (arg instanceof Formattable) {
   195             return ((Formattable)arg).toString(l, messages);
   196         }
   197         else {
   198             return String.valueOf(arg);
   199         }
   200     }
   202     /**
   203      * Format an iterable argument of a given diagnostic.
   204      *
   205      * @param d diagnostic whose argument is to be formatted
   206      * @param it iterable argument to be formatted
   207      * @param l locale object to be used for i18n
   208      * @return string representation of the diagnostic iterable argument
   209      */
   210     protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
   211         StringBuilder sbuf = new StringBuilder();
   212         String sep = "";
   213         for (Object o : it) {
   214             sbuf.append(sep);
   215             sbuf.append(formatArgument(d, o, l));
   216             sep = ",";
   217         }
   218         return sbuf.toString();
   219     }
   221     /**
   222      * Format all the subdiagnostics attached to a given diagnostic.
   223      *
   224      * @param d diagnostic whose subdiagnostics are to be formatted
   225      * @param l locale object to be used for i18n
   226      * @return list of all string representations of the subdiagnostics
   227      */
   228     protected List<String> formatSubdiagnostics(JCDiagnostic d, Locale l) {
   229         List<String> subdiagnostics = List.nil();
   230         int maxDepth = config.getMultilineLimit(MultilineLimit.DEPTH);
   231         if (maxDepth == -1 || depth < maxDepth) {
   232             depth++;
   233             try {
   234                 int maxCount = config.getMultilineLimit(MultilineLimit.LENGTH);
   235                 int count = 0;
   236                 for (JCDiagnostic d2 : d.getSubdiagnostics()) {
   237                     if (maxCount == -1 || count < maxCount) {
   238                         subdiagnostics = subdiagnostics.append(formatSubdiagnostic(d, d2, l));
   239                         count++;
   240                     }
   241                     else
   242                         break;
   243                 }
   244             }
   245             finally {
   246                 depth--;
   247             }
   248         }
   249         return subdiagnostics;
   250     }
   252     /**
   253      * Format a subdiagnostics attached to a given diagnostic.
   254      *
   255      * @param parent multiline diagnostic whose subdiagnostics is to be formatted
   256      * @param sub subdiagnostic to be formatted
   257      * @param l locale object to be used for i18n
   258      * @return string representation of the subdiagnostics
   259      */
   260     protected String formatSubdiagnostic(JCDiagnostic parent, JCDiagnostic sub, Locale l) {
   261         return formatMessage(sub, l);
   262     }
   264     /** Format the faulty source code line and point to the error.
   265      *  @param d The diagnostic for which the error line should be printed
   266      */
   267     protected String formatSourceLine(JCDiagnostic d, int nSpaces) {
   268         StringBuilder buf = new StringBuilder();
   269         DiagnosticSource source = d.getDiagnosticSource();
   270         int pos = d.getIntPosition();
   271         if (d.getIntPosition() == Position.NOPOS)
   272             throw new AssertionError();
   273         String line = (source == null ? null : source.getLine(pos));
   274         if (line == null)
   275             return "";
   276         buf.append(indent(line, nSpaces));
   277         int col = source.getColumnNumber(pos, false);
   278         if (config.isCaretEnabled()) {
   279             buf.append("\n");
   280             for (int i = 0; i < col - 1; i++)  {
   281                 buf.append((line.charAt(i) == '\t') ? "\t" : " ");
   282             }
   283             buf.append(indent("^", nSpaces));
   284         }
   285         return buf.toString();
   286     }
   288     /**
   289      * Converts a String into a locale-dependent representation accordingly to a given locale.
   290      *
   291      * @param l locale object to be used for i18n
   292      * @param key locale-independent key used for looking up in a resource file
   293      * @param args localization arguments
   294      * @return a locale-dependent string
   295      */
   296     protected String localize(Locale l, String key, Object... args) {
   297         return messages.getLocalizedString(l, key, args);
   298     }
   300     public boolean displaySource(JCDiagnostic d) {
   301         return config.getVisible().contains(DiagnosticPart.SOURCE) &&
   302                 d.getType() != FRAGMENT &&
   303                 d.getIntPosition() != Position.NOPOS;
   304     }
   306     public boolean isRaw() {
   307         return false;
   308     }
   310     /**
   311      * Creates a string with a given amount of empty spaces. Useful for
   312      * indenting the text of a diagnostic message.
   313      *
   314      * @param nSpaces the amount of spaces to be added to the result string
   315      * @return the indentation string
   316      */
   317     protected String indentString(int nSpaces) {
   318         String spaces = "                        ";
   319         if (nSpaces <= spaces.length())
   320             return spaces.substring(0, nSpaces);
   321         else {
   322             StringBuilder buf = new StringBuilder();
   323             for (int i = 0 ; i < nSpaces ; i++)
   324                 buf.append(" ");
   325             return buf.toString();
   326         }
   327     }
   329     /**
   330      * Indent a string by prepending a given amount of empty spaces to each line
   331      * of the string.
   332      *
   333      * @param s the string to be indented
   334      * @param nSpaces the amount of spaces that should be prepended to each line
   335      * of the string
   336      * @return an indented string
   337      */
   338     protected String indent(String s, int nSpaces) {
   339         String indent = indentString(nSpaces);
   340         StringBuilder buf = new StringBuilder();
   341         String nl = "";
   342         for (String line : s.split("\n")) {
   343             buf.append(nl);
   344             buf.append(indent + line);
   345             nl = "\n";
   346         }
   347         return buf.toString();
   348     }
   350     public SimpleConfiguration getConfiguration() {
   351         return config;
   352     }
   354     static public class SimpleConfiguration implements Configuration {
   356         protected Map<MultilineLimit, Integer> multilineLimits;
   357         protected EnumSet<DiagnosticPart> visibleParts;
   358         protected boolean caretEnabled;
   360         public SimpleConfiguration(Set<DiagnosticPart> parts) {
   361             multilineLimits = new HashMap<MultilineLimit, Integer>();
   362             setVisible(parts);
   363             setMultilineLimit(MultilineLimit.DEPTH, -1);
   364             setMultilineLimit(MultilineLimit.LENGTH, -1);
   365             setCaretEnabled(true);
   366         }
   368         @SuppressWarnings("fallthrough")
   369         public SimpleConfiguration(Options options, Set<DiagnosticPart> parts) {
   370             this(parts);
   371             String showSource = null;
   372             if ((showSource = options.get("showSource")) != null) {
   373                 if (showSource.equals("true"))
   374                     setVisiblePart(DiagnosticPart.SOURCE, true);
   375                 else if (showSource.equals("false"))
   376                     setVisiblePart(DiagnosticPart.SOURCE, false);
   377             }
   378             String diagOpts = options.get("diags");
   379             if (diagOpts != null) {//override -XDshowSource
   380                 Collection<String> args = Arrays.asList(diagOpts.split(","));
   381                 if (args.contains("short")) {
   382                     setVisiblePart(DiagnosticPart.DETAILS, false);
   383                     setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
   384                 }
   385                 if (args.contains("source"))
   386                     setVisiblePart(DiagnosticPart.SOURCE, true);
   387                 if (args.contains("-source"))
   388                     setVisiblePart(DiagnosticPart.SOURCE, false);
   389             }
   390             String multiPolicy = null;
   391             if ((multiPolicy = options.get("multilinePolicy")) != null) {
   392                 if (multiPolicy.equals("disabled"))
   393                     setVisiblePart(DiagnosticPart.SUBDIAGNOSTICS, false);
   394                 else if (multiPolicy.startsWith("limit:")) {
   395                     String limitString = multiPolicy.substring("limit:".length());
   396                     String[] limits = limitString.split(":");
   397                     try {
   398                         switch (limits.length) {
   399                             case 2: {
   400                                 if (!limits[1].equals("*"))
   401                                     setMultilineLimit(MultilineLimit.DEPTH, Integer.parseInt(limits[1]));
   402                             }
   403                             case 1: {
   404                                 if (!limits[0].equals("*"))
   405                                     setMultilineLimit(MultilineLimit.LENGTH, Integer.parseInt(limits[0]));
   406                             }
   407                         }
   408                     }
   409                     catch(NumberFormatException ex) {
   410                         setMultilineLimit(MultilineLimit.DEPTH, -1);
   411                         setMultilineLimit(MultilineLimit.LENGTH, -1);
   412                     }
   413                 }
   414             }
   415             String showCaret = null;
   416             if (((showCaret = options.get("showCaret")) != null) &&
   417                 showCaret.equals("false"))
   418                     setCaretEnabled(false);
   419             else
   420                 setCaretEnabled(true);
   421         }
   423         public int getMultilineLimit(MultilineLimit limit) {
   424             return multilineLimits.get(limit);
   425         }
   427         public EnumSet<DiagnosticPart> getVisible() {
   428             return EnumSet.copyOf(visibleParts);
   429         }
   431         public void setMultilineLimit(MultilineLimit limit, int value) {
   432             multilineLimits.put(limit, value < -1 ? -1 : value);
   433         }
   436         public void setVisible(Set<DiagnosticPart> diagParts) {
   437             visibleParts = EnumSet.copyOf(diagParts);
   438         }
   440         public void setVisiblePart(DiagnosticPart diagParts, boolean enabled) {
   441             if (enabled)
   442                 visibleParts.add(diagParts);
   443             else
   444                 visibleParts.remove(diagParts);
   445         }
   447         /**
   448          * Shows a '^' sign under the source line displayed by the formatter
   449          * (if applicable).
   450          *
   451          * @param caretEnabled if true enables caret
   452          */
   453         public void setCaretEnabled(boolean caretEnabled) {
   454             this.caretEnabled = caretEnabled;
   455         }
   457         /**
   458          * Tells whether the caret display is active or not.
   459          *
   460          * @param caretEnabled if true the caret is enabled
   461          */
   462         public boolean isCaretEnabled() {
   463             return caretEnabled;
   464         }
   465     }
   467     public Printer getPrinter() {
   468         return printer;
   469     }
   471     public void setPrinter(Printer printer) {
   472         this.printer = printer;
   473     }
   475     /**
   476      * An enhanced printer for formatting types/symbols used by
   477      * AbstractDiagnosticFormatter. Provides alternate numbering of captured
   478      * types (they are numbered starting from 1 on each new diagnostic, instead
   479      * of relying on the underlying hashcode() method which generates unstable
   480      * output). Also detects cycles in wildcard messages (e.g. if the wildcard
   481      * type referred by a given captured type C contains C itself) which might
   482      * lead to infinite loops.
   483      */
   484     protected Printer printer = new Printer() {
   485         @Override
   486         protected String localize(Locale locale, String key, Object... args) {
   487             return AbstractDiagnosticFormatter.this.localize(locale, key, args);
   488         }
   489         @Override
   490         protected String capturedVarId(CapturedType t, Locale locale) {
   491             return "" + (allCaptured.indexOf(t) + 1);
   492         }
   493         @Override
   494         public String visitCapturedType(CapturedType t, Locale locale) {
   495             if (!allCaptured.contains(t)) {
   496                 allCaptured = allCaptured.append(t);
   497             }
   498             return super.visitCapturedType(t, locale);
   499         }
   500     };
   501 }

mercurial