src/jdk.compiler/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
changeset 47316 1129253d3bc7
parent 47216 71c04702a3d5
child 51563 de411d537aae
equal deleted inserted replaced
47315:31f541df4187 47316:1129253d3bc7
    32 
    32 
    33 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
    33 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
    34 import com.sun.tools.javac.api.Formattable;
    34 import com.sun.tools.javac.api.Formattable;
    35 import com.sun.tools.javac.file.PathFileObject;
    35 import com.sun.tools.javac.file.PathFileObject;
    36 import com.sun.tools.javac.tree.JCTree.*;
    36 import com.sun.tools.javac.tree.JCTree.*;
    37 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
       
    38 
    37 
    39 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
    38 import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
    40 
    39 
    41 /**
    40 /**
    42  * A raw formatter for diagnostic messages.
    41  * A raw formatter for diagnostic messages.
    50  * deletion without notice.</b>
    49  * deletion without notice.</b>
    51  */
    50  */
    52 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
    51 public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
    53 
    52 
    54     /**
    53     /**
       
    54      * The raw diagnostic helper.
       
    55      */
       
    56     RawDiagnosticPosHelper rawDiagnosticPosHelper;
       
    57 
       
    58     /**
       
    59      * Helper class to generate stable positions for AST nodes occurring in diagnostic arguments.
       
    60      * If the AST node appears in the same line number as the main diagnostic, the line is information is omitted.
       
    61      * Otherwise both line and column information is included, using the format @{code line:col}".
       
    62      *
       
    63      * Note: since subdiagnostics can be created without a diagnostic source, a position helper
       
    64      * should always refer to the toplevel diagnostic source.
       
    65      */
       
    66     static class RawDiagnosticPosHelper {
       
    67         private final JCDiagnostic diag;
       
    68 
       
    69         RawDiagnosticPosHelper(JCDiagnostic diag) {
       
    70             this.diag = diag;
       
    71         }
       
    72 
       
    73         String getPosition(JCExpression exp) {
       
    74             DiagnosticSource diagSource = diag.getDiagnosticSource();
       
    75             long diagLine = diag.getLineNumber();
       
    76             long expLine = diagSource.getLineNumber(exp.pos);
       
    77             long expCol = diagSource.getColumnNumber(exp.pos, false);
       
    78             return (expLine == diagLine) ?
       
    79                     String.valueOf(expCol) :
       
    80                     expLine + ":" + expCol;
       
    81         }
       
    82     }
       
    83 
       
    84     /**
    55      * Create a formatter based on the supplied options.
    85      * Create a formatter based on the supplied options.
    56      * @param options
    86      * @param options
    57      */
    87      */
    58     public RawDiagnosticFormatter(Options options) {
    88     public RawDiagnosticFormatter(Options options) {
    59         super(null, new SimpleConfiguration(options,
    89         super(null, new SimpleConfiguration(options,
    63     }
    93     }
    64 
    94 
    65     //provide common default formats
    95     //provide common default formats
    66     public String formatDiagnostic(JCDiagnostic d, Locale l) {
    96     public String formatDiagnostic(JCDiagnostic d, Locale l) {
    67         try {
    97         try {
       
    98             rawDiagnosticPosHelper = new RawDiagnosticPosHelper(d);
    68             StringBuilder buf = new StringBuilder();
    99             StringBuilder buf = new StringBuilder();
    69             if (d.getPosition() != Position.NOPOS) {
   100             if (d.getPosition() != Position.NOPOS) {
    70                 buf.append(formatSource(d, false, null));
   101                 buf.append(formatSource(d, false, null));
    71                 buf.append(':');
   102                 buf.append(':');
    72                 buf.append(formatPosition(d, LINE, null));
   103                 buf.append(formatPosition(d, LINE, null));
    87                 buf.append(formatSourceLine(d, 0));
   118                 buf.append(formatSourceLine(d, 0));
    88             }
   119             }
    89             return buf.toString();
   120             return buf.toString();
    90         }
   121         }
    91         catch (Exception e) {
   122         catch (Exception e) {
    92             //e.printStackTrace();
       
    93             return null;
   123             return null;
       
   124         } finally {
       
   125             rawDiagnosticPosHelper = null;
    94         }
   126         }
    95     }
   127     }
    96 
   128 
    97     public String formatMessage(JCDiagnostic d, Locale l) {
   129     public String formatMessage(JCDiagnostic d, Locale l) {
    98         StringBuilder buf = new StringBuilder();
   130         StringBuilder buf = new StringBuilder();
   120     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
   152     protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
   121         String s;
   153         String s;
   122         if (arg instanceof Formattable) {
   154         if (arg instanceof Formattable) {
   123             s = arg.toString();
   155             s = arg.toString();
   124         } else if (arg instanceof JCExpression) {
   156         } else if (arg instanceof JCExpression) {
   125             JCExpression tree = (JCExpression)arg;
   157             Assert.checkNonNull(rawDiagnosticPosHelper);
   126             s = "@" + tree.getStartPosition();
   158             s = "@" + rawDiagnosticPosHelper.getPosition((JCExpression)arg);
   127         } else if (arg instanceof PathFileObject) {
   159         } else if (arg instanceof PathFileObject) {
   128             s = ((PathFileObject) arg).getShortName();
   160             s = ((PathFileObject) arg).getShortName();
   129         } else {
   161         } else {
   130             s = super.formatArgument(diag, arg, null);
   162             s = super.formatArgument(diag, arg, null);
   131         }
   163         }