langtools/src/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java
author mcimadamore
Wed, 12 Nov 2008 14:17:03 +0000
changeset 1591 e5a618442f5f
parent 1472 1e09e143438c
child 2085 4792e12a8ca2
permissions -rw-r--r--
6768932: Add support for multiline diagnostics Summary: Added basic support for multiline/tabular diagnostics 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 2008 Sun Microsystems, Inc.  All Rights Reserved.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     4
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    10
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    16
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    20
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    23
 * have any questions.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    24
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    25
package com.sun.tools.javac.util;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    26
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    27
import java.util.Collection;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    28
import java.util.Locale;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    29
import javax.tools.JavaFileObject;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    30
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.javac.api.DiagnosticFormatter;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.api.Formattable;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.api.DiagnosticFormatter.PositionKind;
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.file.JavacFileManager;
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    35
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    36
import static com.sun.tools.javac.util.LayoutCharacters.*;
942
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
 * This abstract class provides a basic implementation of the functionalities that should be provided
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    40
 * by any formatter used by javac. Among the main features provided by AbstractDiagnosticFormatter are:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    41
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    42
 * <ul>
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    43
 *  <li> Provides a standard implementation of the visitor-like methods defined in the interface DiagnisticFormatter.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    44
 *  Those implementations are specifically targeting JCDiagnostic objects.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    45
 *  <li> Provides basic support for i18n and a method for executing all locale-dependent conversions
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    46
 *  <li> Provides the formatting logic for rendering the arguments of a JCDiagnostic object.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    47
 * <ul>
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    48
 *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    49
 */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    50
public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter<JCDiagnostic> {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    51
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    52
    /**
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    53
     * JavacMessages object used by this formatter for i18n
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    54
     */
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    55
    protected JavacMessages messages;
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    56
    protected boolean showSource;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    57
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    58
    /**
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
    59
     * Initialize an AbstractDiagnosticFormatter by setting its JavacMessages object
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    60
     * @param messages
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    61
     */
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    62
    protected AbstractDiagnosticFormatter(JavacMessages messages, Options options, boolean showSource) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    63
        this.messages = messages;
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    64
        this.showSource = options.get("showSource") == null ? showSource :
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    65
                          options.get("showSource").equals("true");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    66
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    67
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    68
    protected AbstractDiagnosticFormatter(JavacMessages messages, boolean showSource) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    69
        this.messages = messages;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
    70
        this.showSource = showSource;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    71
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    72
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    73
    public String formatMessage(JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    74
        //this code should rely on the locale settings but it's not! See RFE 6443132
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    75
        StringBuilder buf = new StringBuilder();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    76
        Collection<String> args = formatArguments(d, l);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    77
        buf.append(localize(l, d.getCode(), args.toArray()));
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    78
        if (d.isMultiline()) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    79
            buf.append(formatSubdiagnostics(d, l));
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    80
        }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
    81
        return buf.toString();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    82
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    83
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    84
    public String formatKind(JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    85
        switch (d.getType()) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    86
            case FRAGMENT: return "";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    87
            case NOTE:     return localize(l, "compiler.note.note");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    88
            case WARNING:  return localize(l, "compiler.warn.warning");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    89
            case ERROR:    return localize(l, "compiler.err.error");
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    90
            default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    91
                throw new AssertionError("Unknown diagnostic type: " + d.getType());
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    92
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    93
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    94
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    95
    public String formatPosition(JCDiagnostic d, PositionKind pk,Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    96
        assert (d.getPosition() != Position.NOPOS);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    97
        return String.valueOf(getPosition(d, pk));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    98
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
    99
    //WHERE
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   100
    public long getPosition(JCDiagnostic d, PositionKind pk) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   101
        switch (pk) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   102
            case START: return d.getIntStartPosition();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   103
            case END: return d.getIntEndPosition();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   104
            case LINE: return d.getLineNumber();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   105
            case COLUMN: return d.getColumnNumber();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   106
            case OFFSET: return d.getIntPosition();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   107
            default:
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   108
                throw new AssertionError("Unknown diagnostic position: " + pk);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   109
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   110
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   111
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   112
    public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   113
        assert (d.getSource() != null);
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   114
        return fullname ? d.getSourceName() : d.getSource().getName();
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   115
    }
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
     * Format the arguments of a given diagnostic.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   119
     *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   120
     * @param d diagnostic whose arguments are to be formatted
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   121
     * @param l locale object to be used for i18n
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   122
     * @return a Collection whose elements are the formatted arguments of the diagnostic
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   123
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   124
    protected Collection<String> formatArguments(JCDiagnostic d, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   125
        ListBuffer<String> buf = new ListBuffer<String>();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   126
        for (Object o : d.getArgs()) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   127
           buf.append(formatArgument(d, o, l));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   128
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   129
        return buf.toList();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   130
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   131
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   132
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   133
     * Format a single argument of a given diagnostic.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   134
     *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   135
     * @param d diagnostic whose argument is to be formatted
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   136
     * @param arg argument to be formatted
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   137
     * @param l locale object to be used for i18n
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   138
     * @return string representation of the diagnostic argument
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   139
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   140
    protected String formatArgument(JCDiagnostic d, Object arg, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   141
        if (arg instanceof JCDiagnostic)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   142
            return format((JCDiagnostic)arg, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   143
        else if (arg instanceof Iterable<?>) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   144
            return formatIterable(d, (Iterable<?>)arg, l);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   145
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   146
        else if (arg instanceof JavaFileObject)
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   147
            return JavacFileManager.getJavacBaseFileName((JavaFileObject)arg);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   148
        else if (arg instanceof Formattable)
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   149
            return ((Formattable)arg).toString(l, messages);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   150
        else
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   151
            return String.valueOf(arg);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   152
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   153
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   154
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   155
     * Format an iterable argument of a given diagnostic.
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   156
     *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   157
     * @param d diagnostic whose argument is to be formatted
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   158
     * @param it iterable argument to be formatted
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   159
     * @param l locale object to be used for i18n
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   160
     * @return string representation of the diagnostic iterable argument
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   161
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   162
    protected String formatIterable(JCDiagnostic d, Iterable<?> it, Locale l) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   163
        StringBuilder sbuf = new StringBuilder();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   164
        String sep = "";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   165
        for (Object o : it) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   166
            sbuf.append(sep);
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   167
            sbuf.append(formatArgument(d, o, l));
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   168
            sep = ",";
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   169
        }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   170
        return sbuf.toString();
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   171
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   172
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   173
    /**
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   174
     * Format all the subdiagnostics attached to a given diagnostic
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   175
     *
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   176
     * @param d diagnostic whose subdiagnostics are to be formatted
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   177
     * @param l locale object to be used for i18n
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   178
     * @return string representation of the subdiagnostics
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   179
     */
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   180
    protected String formatSubdiagnostics(JCDiagnostic d, Locale l) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   181
        StringBuilder buf = new StringBuilder();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   182
        for (JCDiagnostic d2 : d.getSubdiagnostics()) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   183
            buf.append('\n');
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   184
            String subdiagMsg = format(d2, l);
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   185
            buf.append(indent(subdiagMsg, DiagInc));
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   186
        }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   187
        return buf.toString();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   188
    }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   189
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   190
    /** Format the faulty source code line and point to the error.
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   191
     *  @param d The diagnostic for which the error line should be printed
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   192
     */
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   193
    protected String formatSourceLine(JCDiagnostic d) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   194
        StringBuilder buf = new StringBuilder();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   195
        DiagnosticSource source = d.getDiagnosticSource();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   196
        int pos = d.getIntPosition();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   197
        if (d.getIntPosition() != Position.NOPOS) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   198
            String line = (source == null ? null : source.getLine(pos));
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   199
            if (line == null)
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   200
                return "";
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   201
            buf.append(line+"\n");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   202
            int col = source.getColumnNumber(pos, false);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   203
            for (int i = 0; i < col - 1; i++)  {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   204
                buf.append((line.charAt(i) == '\t') ? "\t" : " ");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   205
            }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   206
            buf.append("^");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   207
         }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   208
         return buf.toString();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   209
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   210
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   211
    /**
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   212
     * Converts a String into a locale-dependent representation accordingly to a given locale
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   213
     *
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   214
     * @param l locale object to be used for i18n
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   215
     * @param key locale-independent key used for looking up in a resource file
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   216
     * @param args localization arguments
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   217
     * @return a locale-dependent string
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   218
     */
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   219
    protected String localize(Locale l, String key, Object... args) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   220
        return messages.getLocalizedString(l, key, args);
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   221
    }
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   222
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   223
    public boolean displaySource(JCDiagnostic d) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   224
        return showSource && d.getType() != FRAGMENT;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   225
    }
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   226
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   227
    /**
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   228
     * Creates a string with a given amount of empty spaces. Useful for
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   229
     * indenting the text of a diagnostic message.
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   230
     *
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   231
     * @param nSpaces the amount of spaces to be added to the result string
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   232
     * @return the indentation string
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   233
     */
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   234
    protected String indentString(int nSpaces) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   235
        String spaces = "                        ";
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   236
        if (nSpaces <= spaces.length())
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   237
            return spaces.substring(0, nSpaces);
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   238
        else {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   239
            StringBuilder buf = new StringBuilder();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   240
            for (int i = 0 ; i < nSpaces ; i++)
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   241
                buf.append(" ");
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   242
            return buf.toString();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   243
        }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   244
    }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   245
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   246
    /**
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   247
     * Indent a string by prepending a given amount of empty spaces to each line
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   248
     * of the string
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   249
     *
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   250
     * @param s the string to be indented
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   251
     * @param nSpaces the amount of spaces that should be prepended to each line
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   252
     * of the string
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   253
     * @return an indented string
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   254
     */
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   255
    protected String indent(String s, int nSpaces) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   256
        String indent = indentString(nSpaces);
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   257
        StringBuilder buf = new StringBuilder();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   258
        String nl = "";
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   259
        for (String line : s.split("\n")) {
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   260
            buf.append(nl);
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   261
            buf.append(indent + line);
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   262
            nl = "\n";
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   263
        }
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   264
        return buf.toString();
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   265
    }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents:
diff changeset
   266
}