langtools/src/share/classes/com/sun/tools/javac/util/RawDiagnosticFormatter.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 7624 c31b0ea95b37
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
     2
 * Copyright (c) 2008, 2009, 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: 3995
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: 3995
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: 3995
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    23
 * questions.
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    24
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    25
package com.sun.tools.javac.util;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    26
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    27
import java.util.Collection;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    28
import java.util.EnumSet;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    29
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    30
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    31
import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.*;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.api.Formattable;
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3380
diff changeset
    33
import com.sun.tools.javac.file.BaseFileObject;
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    34
import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    35
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    36
import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    37
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    38
/**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    39
 * A raw formatter for diagnostic messages.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
 * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    41
 * or not the source name and position are set. This formatter provides a standardized, localize-independent
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    42
 * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 2984
diff changeset
    43
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 * <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
    45
 * 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
    46
 * 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
    47
 * deletion without notice.</b>
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    48
 */
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    49
public final class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    50
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    51
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    52
     * Create a formatter based on the supplied options.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    53
     * @param msgs
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
     */
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    55
    public RawDiagnosticFormatter(Options options) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    56
        super(null, new SimpleConfiguration(options,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    57
                EnumSet.of(DiagnosticPart.SUMMARY,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    58
                        DiagnosticPart.DETAILS,
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    59
                        DiagnosticPart.SUBDIAGNOSTICS)));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    61
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    62
    //provide common default formats
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    63
    public String formatDiagnostic(JCDiagnostic d, Locale l) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    64
        try {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    65
            StringBuffer buf = new StringBuffer();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    66
            if (d.getPosition() != Position.NOPOS) {
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
    67
                buf.append(formatSource(d, false, null));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    68
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    69
                buf.append(formatPosition(d, LINE, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    70
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
                buf.append(formatPosition(d, COLUMN, null));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    72
                buf.append(':');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    73
            }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
            else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    75
                buf.append('-');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    76
            buf.append(' ');
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    77
            buf.append(formatMessage(d, null));
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    78
            if (displaySource(d))
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    79
                buf.append("\n" + formatSourceLine(d, 0));
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    80
            return buf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    81
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    82
        catch (Exception e) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    83
            e.printStackTrace();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    84
            return null;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    85
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    86
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    87
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    88
    public String formatMessage(JCDiagnostic d, Locale l) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    89
        StringBuilder buf = new StringBuilder();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    90
        Collection<String> args = formatArguments(d, l);
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    91
        buf.append(localize(null, d.getCode(), args.toArray()));
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    92
        if (d.isMultiline() && getConfiguration().getVisible().contains(DiagnosticPart.SUBDIAGNOSTICS)) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    93
            List<String> subDiags = formatSubdiagnostics(d, null);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    94
            if (subDiags.nonEmpty()) {
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
    95
                String sep = "";
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    96
                buf.append(",{");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    97
                for (String sub : formatSubdiagnostics(d, null)) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    98
                    buf.append(sep);
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
    99
                    buf.append("(" + sub + ")");
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   100
                    sep = ",";
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   101
                }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   102
                buf.append('}');
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   103
            }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   104
        }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   105
        return buf.toString();
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   106
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1591
diff changeset
   107
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   108
    @Override
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   109
    protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   110
        String s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   111
        if (arg instanceof Formattable)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   112
            s = arg.toString();
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3380
diff changeset
   113
        else if (arg instanceof BaseFileObject)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3380
diff changeset
   114
            s = ((BaseFileObject) arg).getShortName();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   115
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   116
            s = super.formatArgument(diag, arg, null);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   117
        if (arg instanceof JCDiagnostic)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   118
            return "(" + s + ")";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   119
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   120
            return s;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   121
    }
2221
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   122
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   123
    @Override
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   124
    protected String localize(Locale l, String key, Object... args) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   125
        StringBuilder buf = new StringBuilder();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   126
        buf.append(key);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   127
        String sep = ": ";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   128
        for (Object o : args) {
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   129
            buf.append(sep);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   130
            buf.append(o);
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   131
            sep = ", ";
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   132
        }
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   133
        return buf.toString();
cd6557bcaa0a 6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents: 2085
diff changeset
   134
    }
2984
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   135
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   136
    @Override
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   137
    public boolean isRaw() {
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   138
        return true;
e15ff3a34054 6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents: 2223
diff changeset
   139
    }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   140
}