langtools/src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
author tbell
Mon, 09 Mar 2009 23:53:41 -0700
changeset 2223 95e3c21b2919
parent 2212 1d3dc0e0ba0c
parent 2221 cd6557bcaa0a
child 2984 e15ff3a34054
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     1
/*
2212
1d3dc0e0ba0c 6814575: Update copyright year
xdono
parents: 2085
diff changeset
     2
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     4
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    10
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    16
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    20
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    23
 * have any questions.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    24
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    25
package com.sun.tools.javac.util;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    26
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    27
import java.util.Collection;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    28
import java.util.EnumSet;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    29
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    30
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    31
import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.api.Formattable;
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    33
import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    34
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    35
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    36
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    37
/**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    38
 * A raw formatter for diagnostic messages.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    39
 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
 * or not the source name and position are set. This formatter provides a standardized, localize-independent
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    41
 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    42
 */
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    43
public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    44
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    45
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    46
     * Create a formatter based on the supplied options.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    47
     * @param msgs
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    48
     */
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    49
    public RawDiagnosticFormatter(Options options) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    50
        super(null, new SimpleConfiguration(options,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    51
                EnumSet.of(DiagnosticPart.SUMMARY,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    52
                        DiagnosticPart.DETAILS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    53
                        DiagnosticPart.SUBDIAGNOSTICS)));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    55
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    56
    //provide common default formats
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    57
    public String formatDiagnostic(JCDiagnostic d, Locale l) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    58
        try {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    59
            StringBuffer buf = new StringBuffer();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
            if (d.getPosition() != Position.NOPOS) {
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
    61
                buf.append(formatSource(d, false, null));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    62
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    63
                buf.append(formatPosition(d, LINE, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    64
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    65
                buf.append(formatPosition(d, COLUMN, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    66
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    67
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    68
            else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    69
                buf.append('-');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    70
            buf.append(' ');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
            buf.append(formatMessage(d, null));
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    72
            if (displaySource(d))
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    73
                buf.append("\n" + formatSourceLine(d, 0));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
            return buf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    75
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    76
        catch (Exception e) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    77
            e.printStackTrace();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    78
            return null;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    79
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    80
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    81
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    82
    public String formatMessage(JCDiagnostic d, Locale l) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    83
        StringBuilder buf = new StringBuilder();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    84
        Collection<String> args = formatArguments(d, l);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    85
        buf.append(localize(null, d.getCode(), args.toArray()));
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    86
        if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    87
            List<String> subDiags = formatSubdiagnostics(d, null);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    88
            if (subDiags.nonEmpty()) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    89
                String sep = "";
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    90
                buf.append(",{");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    91
                for (String sub : formatSubdiagnostics(d, null)) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    92
                    buf.append(sep);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    93
                    buf.append("(" + sub + ")");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    94
                    sep = ",";
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    95
                }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    96
                buf.append('}');
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    97
            }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    98
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    99
        return buf.toString();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   100
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   101
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   102
    @Override
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   103
    protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   104
        String s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   105
        if (arg instanceof Formattable)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   106
            s = arg.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   107
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   108
            s = super.formatArgument(diag, arg, null);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   109
        if (arg instanceof JCDiagnostic)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   110
            return "(" + s + ")";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   111
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   112
            return s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   113
    }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   114
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   115
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   116
    protected String localize(Locale l, String key, Object... args) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   117
        StringBuilder buf = new StringBuilder();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   118
        buf.append(key);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   119
        String sep = ": ";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   120
        for (Object o : args) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   121
            buf.append(sep);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   122
            buf.append(o);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   123
            sep = ", ";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   124
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   125
        return buf.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   126
    }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   127
}