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

Tue, 11 Aug 2009 01:13:14 +0100

author
mcimadamore
date
Tue, 11 Aug 2009 01:13:14 +0100
changeset 359
8227961c64d3
parent 333
7c2d6da61646
child 415
49359d0e6a9c
permissions
-rw-r--r--

6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg

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

mercurial