langtools/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 22159 682da512ec17
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     1
/*
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 8843
diff changeset
     2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. 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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
942
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    23
 * questions.
942
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
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    28
import java.util.Collection;
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
    29
import java.util.EnumMap;
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    30
import java.util.EnumSet;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    31
import java.util.HashMap;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    33
import java.util.Map;
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    34
import java.util.regex.Matcher;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    35
import javax.tools.JavaFileObject;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    36
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    37
import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    38
import com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicConfiguration;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    39
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    41
import static com.sun.tools.javac.util.BasicDiagnosticFormatter.BasicConfiguration.*;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    42
import static com.sun.tools.javac.util.LayoutCharacters.*;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    43
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    44
/**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    45
 * A basic formatter for diagnostic messages.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    46
 * 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
    47
 * 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
    48
 * with the following special characters:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    49
 * <ul>
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    50
 * <li>%b: the base of the source name
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    51
 * <li>%f: the source name (full absolute path)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    52
 * <li>%l: the line number of the diagnostic, derived from the character offset
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    53
 * <li>%c: the column number of the diagnostic, derived from the character offset
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
 * <li>%o: the character offset of the diagnostic if set
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    55
 * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    56
 * <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
    57
 *        shown if the type is ERROR and if a source name is set
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    58
 * <li>%m: the text or the diagnostic, including any appropriate arguments
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    59
 * <li>%_: space delimiter, useful for formatting purposes
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
 * </ul>
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    61
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    62
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    63
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    64
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    65
 * deletion without notice.</b>
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    66
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    67
public class BasicDiagnosticFormatter extends AbstractDiagnosticFormatter {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    68
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    69
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    70
     * Create a basic formatter based on the supplied options.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 8843
diff changeset
    72
     * @param options list of command-line options
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    73
     * @param msgs JavacMessages object used for i18n
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
     */
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    75
    public BasicDiagnosticFormatter(Options options, JavacMessages msgs) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    76
        super(msgs, new BasicConfiguration(options));
942
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
     * Create a standard basic formatter
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    81
     *
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    82
     * @param msgs JavacMessages object used for i18n
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    83
     */
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    84
    public BasicDiagnosticFormatter(JavacMessages msgs) {
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
    85
        super(msgs, new BasicConfiguration());
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    86
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    87
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    88
    public String formatDiagnostic(JCDiagnostic d, Locale l) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    89
        if (l == null)
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    90
            l = messages.getCurrentLocale();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    91
        String format = selectFormat(d);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    92
        StringBuilder buf = new StringBuilder();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    93
        for (int i = 0; i < format.length(); i++) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    94
            char c = format.charAt(i);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    95
            boolean meta = false;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    96
            if (c == '%' && i < format.length() - 1) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    97
                meta = true;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    98
                c = format.charAt(++i);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    99
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   100
            buf.append(meta ? formatMeta(c, d, l) : String.valueOf(c));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   101
        }
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   102
        if (depth == 0)
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   103
            return addSourceLineIfNeeded(d, buf.toString());
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   104
        else
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   105
            return buf.toString();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   106
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   107
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   108
    public String formatMessage(JCDiagnostic d, Locale l) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   109
        int currentIndentation = 0;
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   110
        StringBuilder buf = new StringBuilder();
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   111
        Collection<String> args = formatArguments(d, l);
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   112
        String msg = localize(l, d.getCode(), args.toArray());
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   113
        String[] lines = msg.split("\n");
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   114
        if (getConfiguration().getVisible().contains(DiagnosticPart.SUMMARY)) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   115
            currentIndentation += getConfiguration().getIndentation(DiagnosticPart.SUMMARY);
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   116
            buf.append(indent(lines[0], currentIndentation)); //summary
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   117
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   118
        if (lines.length > 1 && getConfiguration().getVisible().contains(DiagnosticPart.DETAILS)) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   119
            currentIndentation += getConfiguration().getIndentation(DiagnosticPart.DETAILS);
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   120
            for (int i = 1;i < lines.length; i++) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   121
                buf.append("\n" + indent(lines[i], currentIndentation));
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   122
            }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   123
        }
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   124
        if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   125
            currentIndentation += getConfiguration().getIndentation(DiagnosticPart.SUBDIAGNOSTICS);
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   126
                for (String sub : formatSubdiagnostics(d, l)) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   127
                    buf.append("\n" + indent(sub, currentIndentation));
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   128
            }
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   129
        }
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6151
diff changeset
   130
        return buf.toString();
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   131
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   132
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   133
    protected String addSourceLineIfNeeded(JCDiagnostic d, String msg) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   134
        if (!displaySource(d))
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   135
            return msg;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   136
        else {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   137
            BasicConfiguration conf = getConfiguration();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   138
            int indentSource = conf.getIndentation(DiagnosticPart.SOURCE);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   139
            String sourceLine = "\n" + formatSourceLine(d, indentSource);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14259
diff changeset
   140
            boolean singleLine = !msg.contains("\n");
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   141
            if (singleLine || getConfiguration().getSourcePosition() == SourcePosition.BOTTOM)
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   142
                return msg + sourceLine;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   143
            else
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   144
                return msg.replaceFirst("\n", Matcher.quoteReplacement(sourceLine) + "\n");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   145
        }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   146
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   147
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   148
    protected String formatMeta(char c, JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   149
        switch (c) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   150
            case 'b':
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   151
                return formatSource(d, false, l);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   152
            case 'e':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   153
                return formatPosition(d, END, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   154
            case 'f':
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   155
                return formatSource(d, true, l);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   156
            case 'l':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   157
                return formatPosition(d, LINE, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   158
            case 'c':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   159
                return formatPosition(d, COLUMN, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   160
            case 'o':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   161
                return formatPosition(d, OFFSET, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   162
            case 'p':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   163
                return formatKind(d, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   164
            case 's':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   165
                return formatPosition(d, START, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   166
            case 't': {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   167
                boolean usePrefix;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   168
                switch (d.getType()) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   169
                case FRAGMENT:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   170
                    usePrefix = false;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   171
                    break;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   172
                case ERROR:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   173
                    usePrefix = (d.getIntPosition() == Position.NOPOS);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   174
                    break;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   175
                default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   176
                    usePrefix = true;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   177
                }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   178
                if (usePrefix)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   179
                    return formatKind(d, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   180
                else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   181
                    return "";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   182
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   183
            case 'm':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   184
                return formatMessage(d, l);
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   185
            case 'L':
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   186
                return formatLintCategory(d, l);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   187
            case '_':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   188
                return " ";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   189
            case '%':
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   190
                return "%";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   191
            default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   192
                return String.valueOf(c);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   193
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   194
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   195
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   196
    private String selectFormat(JCDiagnostic d) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   197
        DiagnosticSource source = d.getDiagnosticSource();
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   198
        String format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT);
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents: 3380
diff changeset
   199
        if (source != null && source != DiagnosticSource.NO_SOURCE) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   200
            if (d.getIntPosition() != Position.NOPOS) {
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   201
                format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_POS_FORMAT);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   202
            } else if (source.getFile() != null &&
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   203
                       source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   204
                format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   205
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   206
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   207
        return format;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   208
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   209
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   210
    @Override
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   211
    public BasicConfiguration getConfiguration() {
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   212
        //the following cast is always safe - see init
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   213
        return (BasicConfiguration)super.getConfiguration();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   214
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   215
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   216
    static public class BasicConfiguration extends SimpleConfiguration {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   217
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   218
        protected Map<DiagnosticPart, Integer> indentationLevels;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   219
        protected Map<BasicFormatKind, String> availableFormats;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   220
        protected SourcePosition sourcePosition;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   221
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   222
        @SuppressWarnings("fallthrough")
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   223
        public BasicConfiguration(Options options) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   224
            super(options, EnumSet.of(DiagnosticPart.SUMMARY,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   225
                            DiagnosticPart.DETAILS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   226
                            DiagnosticPart.SUBDIAGNOSTICS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   227
                            DiagnosticPart.SOURCE));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   228
            initFormat();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   229
            initIndentation();
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   230
            if (options.isSet("oldDiags"))
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   231
                initOldFormat();
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   232
            String fmt = options.get("diagsFormat");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   233
            if (fmt != null) {
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   234
                if (fmt.equals("OLD"))
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   235
                    initOldFormat();
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   236
                else
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   237
                    initFormats(fmt);
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   238
            }
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   239
            String srcPos = null;
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   240
            if ((((srcPos = options.get("sourcePosition")) != null)) &&
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   241
                    srcPos.equals("bottom"))
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   242
                    setSourcePosition(SourcePosition.BOTTOM);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   243
            else
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   244
                setSourcePosition(SourcePosition.AFTER_SUMMARY);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   245
            String indent = options.get("diagsIndentation");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   246
            if (indent != null) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   247
                String[] levels = indent.split("\\|");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   248
                try {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   249
                    switch (levels.length) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   250
                        case 5:
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   251
                            setIndentation(DiagnosticPart.JLS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   252
                                    Integer.parseInt(levels[4]));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   253
                        case 4:
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   254
                            setIndentation(DiagnosticPart.SUBDIAGNOSTICS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   255
                                    Integer.parseInt(levels[3]));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   256
                        case 3:
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   257
                            setIndentation(DiagnosticPart.SOURCE,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   258
                                    Integer.parseInt(levels[2]));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   259
                        case 2:
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   260
                            setIndentation(DiagnosticPart.DETAILS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   261
                                    Integer.parseInt(levels[1]));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   262
                        default:
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   263
                            setIndentation(DiagnosticPart.SUMMARY,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   264
                                    Integer.parseInt(levels[0]));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   265
                    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   266
                }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   267
                catch (NumberFormatException ex) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   268
                    initIndentation();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   269
                }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   270
            }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   271
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   272
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   273
        public BasicConfiguration() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   274
            super(EnumSet.of(DiagnosticPart.SUMMARY,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   275
                  DiagnosticPart.DETAILS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   276
                  DiagnosticPart.SUBDIAGNOSTICS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   277
                  DiagnosticPart.SOURCE));
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   278
            initFormat();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   279
            initIndentation();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   280
        }
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   281
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   282
        private void initFormat() {
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   283
            initFormats("%f:%l:%_%p%L%m", "%p%L%m", "%f:%_%p%L%m");
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   284
        }
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   285
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   286
        private void initOldFormat() {
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   287
            initFormats("%f:%l:%_%t%L%m", "%p%L%m", "%f:%_%t%L%m");
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   288
        }
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   289
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   290
        private void initFormats(String pos, String nopos, String clazz) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   291
            availableFormats = new EnumMap<>(BasicFormatKind.class);
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   292
            setFormat(BasicFormatKind.DEFAULT_POS_FORMAT,    pos);
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   293
            setFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT, nopos);
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   294
            setFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT,  clazz);
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   295
        }
8843
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   296
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   297
        @SuppressWarnings("fallthrough")
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   298
        private void initFormats(String fmt) {
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   299
            String[] formats = fmt.split("\\|");
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   300
            switch (formats.length) {
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   301
                case 3:
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   302
                    setFormat(BasicFormatKind.DEFAULT_CLASS_FORMAT, formats[2]);
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   303
                case 2:
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   304
                    setFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT, formats[1]);
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   305
                default:
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   306
                    setFormat(BasicFormatKind.DEFAULT_POS_FORMAT, formats[0]);
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   307
            }
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   308
        }
2602bd5c9c5f 7010608: the string 'error' should appear in error messages
jjg
parents: 7681
diff changeset
   309
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   310
        private void initIndentation() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   311
            indentationLevels = new HashMap<>();
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   312
            setIndentation(DiagnosticPart.SUMMARY, 0);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   313
            setIndentation(DiagnosticPart.DETAILS, DetailsInc);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   314
            setIndentation(DiagnosticPart.SUBDIAGNOSTICS, DiagInc);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   315
            setIndentation(DiagnosticPart.SOURCE, 0);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   316
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   317
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   318
        /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   319
         * Get the amount of spaces for a given indentation kind
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   320
         * @param diagPart the diagnostic part for which the indentation is
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   321
         * to be retrieved
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   322
         * @return the amount of spaces used for the specified indentation kind
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   323
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   324
        public int getIndentation(DiagnosticPart diagPart) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   325
            return indentationLevels.get(diagPart);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   326
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   327
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   328
        /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   329
         * Set the indentation level for various element of a given diagnostic -
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   330
         * this might lead to more readable diagnostics
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   331
         *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 8843
diff changeset
   332
         * @param diagPart
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   333
         * @param nSpaces amount of spaces for the specified diagnostic part
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   334
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   335
        public void setIndentation(DiagnosticPart diagPart, int nSpaces) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   336
            indentationLevels.put(diagPart, nSpaces);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   337
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   338
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   339
        /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   340
         * Set the source line positioning used by this formatter
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   341
         *
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   342
         * @param sourcePos a positioning value for source line
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   343
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   344
        public void setSourcePosition(SourcePosition sourcePos) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   345
            sourcePosition = sourcePos;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   346
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   347
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   348
        /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   349
         * Get the source line positioning used by this formatter
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   350
         *
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   351
         * @return the positioning value used by this formatter
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   352
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   353
        public SourcePosition getSourcePosition() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   354
            return sourcePosition;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   355
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   356
        //where
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   357
        /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   358
         * A source positioning value controls the position (within a given
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   359
         * diagnostic message) in which the source line the diagnostic refers to
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   360
         * should be displayed (if applicable)
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   361
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   362
        public enum SourcePosition {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   363
            /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   364
             * Source line is displayed after the diagnostic message
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   365
             */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   366
            BOTTOM,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   367
            /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   368
             * Source line is displayed after the first line of the diagnostic
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   369
             * message
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   370
             */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   371
            AFTER_SUMMARY
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   372
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   373
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   374
        /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   375
         * Set a metachar string for a specific format
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   376
         *
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   377
         * @param kind the format kind to be set
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   378
         * @param s the metachar string specifying the format
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   379
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   380
        public void setFormat(BasicFormatKind kind, String s) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   381
            availableFormats.put(kind, s);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   382
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   383
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   384
        /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   385
         * Get a metachar string for a specific format
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   386
         *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 8843
diff changeset
   387
         * @param kind the format kind for which to get the metachar string
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   388
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   389
        public String getFormat(BasicFormatKind kind) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   390
            return availableFormats.get(kind);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   391
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   392
        //where
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   393
        /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   394
         * This enum contains all the kinds of formatting patterns supported
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   395
         * by a basic diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   396
         */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   397
        public enum BasicFormatKind {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   398
            /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   399
            * A format string to be used for diagnostics with a given position.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   400
            */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   401
            DEFAULT_POS_FORMAT,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   402
            /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   403
            * A format string to be used for diagnostics without a given position.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   404
            */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   405
            DEFAULT_NO_POS_FORMAT,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   406
            /**
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   407
            * A format string to be used for diagnostics regarding classfiles
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   408
            */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   409
            DEFAULT_CLASS_FORMAT
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1870
diff changeset
   410
        }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   411
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   412
}