src/jdk.compiler/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
changeset 47216 71c04702a3d5
parent 34752 9c262a013456
child 47316 1129253d3bc7
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2008, 2015, 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 
       
    26 package com.sun.tools.javac.util;
       
    27 
       
    28 import java.util.Collection;
       
    29 import java.util.EnumSet;
       
    30 import java.util.Locale;
       
    31 import javax.tools.JavaFileObject;
       
    32 
       
    33 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
       
    34 import com.sun.tools.javac.api.Formattable;
       
    35 import com.sun.tools.javac.file.PathFileObject;
       
    36 import com.sun.tools.javac.tree.JCTree.*;
       
    37 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
       
    38 
       
    39 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
       
    40 
       
    41 /**
       
    42  * A raw formatter for diagnostic messages.
       
    43  * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
       
    44  * or not the source name and position are set. This formatter provides a standardized, localize-independent
       
    45  * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
       
    46  *
       
    47  * <p><b>This is NOT part of any supported API.
       
    48  * If you write code that depends on this, you do so at your own risk.
       
    49  * This code and its internal interfaces are subject to change or
       
    50  * deletion without notice.</b>
       
    51  */
       
    52 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
       
    53 
       
    54     /**
       
    55      * Create a formatter based on the supplied options.
       
    56      * @param options
       
    57      */
       
    58     public RawDiagnosticFormatter(Options options) {
       
    59         super(null, new SimpleConfiguration(options,
       
    60                 EnumSet.of(DiagnosticPart.SUMMARY,
       
    61                         DiagnosticPart.DETAILS,
       
    62                         DiagnosticPart.SUBDIAGNOSTICS)));
       
    63     }
       
    64 
       
    65     //provide common default formats
       
    66     public String formatDiagnostic(JCDiagnostic d, Locale l) {
       
    67         try {
       
    68             StringBuilder buf = new StringBuilder();
       
    69             if (d.getPosition() != Position.NOPOS) {
       
    70                 buf.append(formatSource(d, false, null));
       
    71                 buf.append(':');
       
    72                 buf.append(formatPosition(d, LINE, null));
       
    73                 buf.append(':');
       
    74                 buf.append(formatPosition(d, COLUMN, null));
       
    75                 buf.append(':');
       
    76             }
       
    77             else if (d.getSource() != null && d.getSource().getKind() == JavaFileObject.Kind.CLASS) {
       
    78                 buf.append(formatSource(d, false, null));
       
    79                 buf.append(":-:-:");
       
    80             }
       
    81             else
       
    82                 buf.append('-');
       
    83             buf.append(' ');
       
    84             buf.append(formatMessage(d, null));
       
    85             if (displaySource(d)) {
       
    86                 buf.append("\n");
       
    87                 buf.append(formatSourceLine(d, 0));
       
    88             }
       
    89             return buf.toString();
       
    90         }
       
    91         catch (Exception e) {
       
    92             //e.printStackTrace();
       
    93             return null;
       
    94         }
       
    95     }
       
    96 
       
    97     public String formatMessage(JCDiagnostic d, Locale l) {
       
    98         StringBuilder buf = new StringBuilder();
       
    99         Collection<String> args = formatArguments(d, l);
       
   100         buf.append(localize(null, d.getCode(), args.toArray()));
       
   101         if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
       
   102             List<String> subDiags = formatSubdiagnostics(d, null);
       
   103             if (subDiags.nonEmpty()) {
       
   104                 String sep = "";
       
   105                 buf.append(",{");
       
   106                 for (String sub : formatSubdiagnostics(d, null)) {
       
   107                     buf.append(sep);
       
   108                     buf.append("(");
       
   109                     buf.append(sub);
       
   110                     buf.append(")");
       
   111                     sep = ",";
       
   112                 }
       
   113                 buf.append('}');
       
   114             }
       
   115         }
       
   116         return buf.toString();
       
   117     }
       
   118 
       
   119     @Override
       
   120     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
       
   121         String s;
       
   122         if (arg instanceof Formattable) {
       
   123             s = arg.toString();
       
   124         } else if (arg instanceof JCExpression) {
       
   125             JCExpression tree = (JCExpression)arg;
       
   126             s = "@" + tree.getStartPosition();
       
   127         } else if (arg instanceof PathFileObject) {
       
   128             s = ((PathFileObject) arg).getShortName();
       
   129         } else {
       
   130             s = super.formatArgument(diag, arg, null);
       
   131         }
       
   132         return (arg instanceof JCDiagnostic) ? "(" + s + ")" : s;
       
   133     }
       
   134 
       
   135     @Override
       
   136     protected String localize(Locale l, String key, Object... args) {
       
   137         StringBuilder buf = new StringBuilder();
       
   138         buf.append(key);
       
   139         String sep = ": ";
       
   140         for (Object o : args) {
       
   141             buf.append(sep);
       
   142             buf.append(o);
       
   143             sep = ", ";
       
   144         }
       
   145         return buf.toString();
       
   146     }
       
   147 
       
   148     @Override
       
   149     public boolean isRaw() {
       
   150         return true;
       
   151     }
       
   152 }