langtools/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java
author mcimadamore
Thu, 09 Oct 2008 16:19:13 +0100
changeset 1472 1e09e143438c
parent 1471 57506cdfb7b4
child 1870 57a1138dffc8
permissions -rw-r--r--
6731573: diagnostic output should optionally include source line Summary: Added an -XD option to optionally prints out source lines in error messages Reviewed-by: jjg
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 2005-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
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.util;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    27
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    28
import java.util.HashMap;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    29
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    30
import java.util.Map;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    31
import javax.tools.JavaFileObject;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    33
import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicFormatKind.*;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    34
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    35
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    36
/**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    37
 * A basic formatter for diagnostic messages.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    38
 * The basic formatter will format a diagnostic according to one of three format patterns, depending on whether
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    39
 * or not the source name and position are set. The formatter supports a printf-like string for patterns
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
 * with the following special characters:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    41
 * <ul>
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    42
 * <li>%b: the base of the source name
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    43
 * <li>%f: the source name (full absolute path)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    44
 * <li>%l: the line number of the diagnostic, derived from the character offset
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    45
 * <li>%c: the column number of the diagnostic, derived from the character offset
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    46
 * <li>%o: the character offset of the diagnostic if set
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    47
 * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    48
 * <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    49
 *        shown if the type is ERROR and if a source name is set
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    50
 * <li>%m: the text or the diagnostic, including any appropriate arguments
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    51
 * <li>%_: space delimiter, useful for formatting purposes
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    52
 * </ul>
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    53
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    55
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    56
    protected Map<BasicFormatKind, String> availableFormats;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    57
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    58
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    59
     * Create a basic formatter based on the supplied options.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
     *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    61
     * @param opts list of command-line options
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    62
     * @param msgs JavacMessages object used for i18n
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    63
     */
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    64
    BasicDiagnosticFormatter(Options opts, JavacMessages msgs) {
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    65
        super(msgs, opts, true);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    66
        initAvailableFormats();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    67
        String fmt = opts.get("diags");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    68
        if (fmt != null) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    69
            String[] formats = fmt.split("\\|");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    70
            switch (formats.length) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
                case 3:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    72
                    availableFormats.put(DEFAULT_CLASS_FORMAT, formats[2]);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    73
                case 2:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
                    availableFormats.put(DEFAULT_NO_POS_FORMAT, formats[1]);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    75
                default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    76
                    availableFormats.put(DEFAULT_POS_FORMAT, formats[0]);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    77
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    78
        }
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
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    82
     * Create a standard basic formatter
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    83
     *
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    84
     * @param msgs JavacMessages object used for i18n
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    85
     */
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    86
    public BasicDiagnosticFormatter(JavacMessages msgs) {
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    87
        super(msgs, true);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    88
        initAvailableFormats();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    89
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    90
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    91
    public void initAvailableFormats() {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    92
        availableFormats = new HashMap<BasicFormatKind, String>();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    93
        availableFormats.put(DEFAULT_POS_FORMAT, "%f:%l:%_%t%m");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    94
        availableFormats.put(DEFAULT_NO_POS_FORMAT, "%p%m");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    95
        availableFormats.put(DEFAULT_CLASS_FORMAT, "%f:%_%t%m");
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 String format(JCDiagnostic d, Locale l) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    99
        if (l == null)
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   100
            l = messages.getCurrentLocale();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   101
        String format = selectFormat(d);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   102
        StringBuilder buf = new StringBuilder();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   103
        for (int i = 0; i < format.length(); i++) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   104
            char c = format.charAt(i);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   105
            boolean meta = false;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   106
            if (c == '%' && i < format.length() - 1) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   107
                meta = true;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   108
                c = format.charAt(++i);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   109
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   110
            buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   111
        }
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   112
        if (displaySource(d)) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   113
            buf.append("\n" + formatSourceLine(d));
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   114
        }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   115
        return buf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   116
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   117
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   118
    protected String formatMeta(char c, JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   119
        switch (c) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   120
            case 'b':
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   121
                return formatSource(d, false, l);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   122
            case 'e':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   123
                return formatPosition(d, END, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   124
            case 'f':
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   125
                return formatSource(d, true, l);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   126
            case 'l':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   127
                return formatPosition(d, LINE, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   128
            case 'c':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   129
                return formatPosition(d, COLUMN, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   130
            case 'o':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   131
                return formatPosition(d, OFFSET, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   132
            case 'p':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   133
                return formatKind(d, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   134
            case 's':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   135
                return formatPosition(d, START, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   136
            case 't': {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   137
                boolean usePrefix;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   138
                switch (d.getType()) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   139
                case FRAGMENT:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   140
                    usePrefix = false;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   141
                    break;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   142
                case ERROR:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   143
                    usePrefix = (d.getIntPosition() == Position.NOPOS);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   144
                    break;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   145
                default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   146
                    usePrefix = true;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   147
                }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   148
                if (usePrefix)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   149
                    return formatKind(d, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   150
                else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   151
                    return "";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   152
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   153
            case 'm':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   154
                return formatMessage(d, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   155
            case '_':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   156
                return " ";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   157
            case '%':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   158
                return "%";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   159
            default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   160
                return String.valueOf(c);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   161
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   162
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   163
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   164
    private String selectFormat(JCDiagnostic d) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   165
        DiagnosticSource source = d.getDiagnosticSource();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   166
        String format = availableFormats.get(DEFAULT_NO_POS_FORMAT);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   167
        if (source != null) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   168
            if (d.getIntPosition() != Position.NOPOS) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   169
                format = availableFormats.get(DEFAULT_POS_FORMAT);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   170
            } else if (source.getFile() != null &&
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   171
                       source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   172
                format = availableFormats.get(DEFAULT_CLASS_FORMAT);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   173
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   174
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   175
        return format;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   176
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   177
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   178
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   179
     * This enum contains all the kinds of formatting patterns supported
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   180
     * by a basic diagnostic formatter.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   181
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   182
    public enum BasicFormatKind {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   183
        /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   184
        * A format string to be used for diagnostics with a given position.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   185
        */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   186
        DEFAULT_POS_FORMAT,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   187
        /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   188
        * A format string to be used for diagnostics without a given position.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   189
        */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   190
        DEFAULT_NO_POS_FORMAT,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   191
        /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   192
        * A format string to be used for diagnostics regarding classfiles
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   193
        */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   194
        DEFAULT_CLASS_FORMAT;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   195
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   196
}