langtools/src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
author jjg
Tue, 16 Sep 2008 18:35:18 -0700
changeset 1260 a772ba9ba43d
parent 1109 853d8c191eac
child 1471 57506cdfb7b4
permissions -rw-r--r--
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes Reviewed-by: darcy, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     1
/*
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
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
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    27
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    28
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    29
import com.sun.tools.javac.api.Formattable;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    30
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    31
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
/**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    33
 * A raw formatter for diagnostic messages.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    34
 * 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
    35
 * or not the source name and position are set. This formatter provides a standardized, localize-independent
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    36
 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    37
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    38
public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    39
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    41
     * Create a formatter based on the supplied options.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    42
     * @param msgs
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    43
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    44
    public RawDiagnosticFormatter(Messages msgs) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    45
        super(null);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    46
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    47
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    48
    //provide common default formats
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    49
    public String format(JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    50
        try {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    51
            StringBuffer buf = new StringBuffer();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    52
            if (d.getPosition() != Position.NOPOS) {
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
    53
                buf.append(formatSource(d, false, null));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    55
                buf.append(formatPosition(d, LINE, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    56
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    57
                buf.append(formatPosition(d, COLUMN, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    58
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    59
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
            else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    61
                buf.append('-');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    62
            buf.append(' ');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    63
            buf.append(formatMessage(d, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    64
            return buf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    65
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    66
        catch (Exception e) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    67
            e.printStackTrace();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    68
            return null;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    69
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    70
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    72
    @Override
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    73
    protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
        String s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    75
        if (arg instanceof Formattable)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    76
            s = arg.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    77
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    78
            s = super.formatArgument(diag, arg, null);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    79
        if (arg instanceof JCDiagnostic)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    80
            return "(" + s + ")";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    81
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    82
            return s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    83
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    84
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    85
    @Override
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    86
    protected String localize(Locale l, String s, Object... args) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    87
        StringBuffer buf = new StringBuffer();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    88
        buf.append(s);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    89
        String sep = ": ";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    90
        for (Object o : args) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    91
            buf.append(sep);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    92
            buf.append(o);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    93
            sep = ", ";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    94
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    95
        return buf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    96
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    97
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    98
    public boolean displaySource(JCDiagnostic d) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    99
        return false;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   100
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   101
}