langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java
author jjg
Wed, 10 Aug 2016 15:47:46 -0700
changeset 40308 274367a99f98
parent 40232 4995ab1a4558
child 40504 0a01f6710c84
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers 8160489: Multiple -Xpatch lines ignored by javac 8156998: javac should support new option -XinheritRuntimeEnvironment Reviewed-by: jlahoda, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
     2
 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
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: 5001
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
    29
import java.util.Arrays;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    30
import java.util.EnumMap;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    31
import java.util.EnumSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    33
import java.util.Map;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    34
import java.util.Queue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    36
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 167
diff changeset
    39
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    40
import com.sun.tools.javac.api.DiagnosticFormatter;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    41
import com.sun.tools.javac.main.Main;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    42
import com.sun.tools.javac.main.Option;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11316
diff changeset
    43
import com.sun.tools.javac.tree.EndPosTable;
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
    44
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    48
import static com.sun.tools.javac.main.Option.*;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    49
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
/** A class for error logs. Reports errors and warnings, and
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *  keeps track of error numbers and positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    53
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    54
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 */
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
    58
public class Log extends AbstractLog {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /** The context key for the log. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    60
    public static final Context.Key<Log> logKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    62
    /** The context key for the standard output PrintWriter. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    63
    public static final Context.Key<PrintWriter> outKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    65
    /** The context key for the diagnostic PrintWriter. */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    66
    public static final Context.Key<PrintWriter> errKey = new Context.Key<>();
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    67
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    68
    /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    69
    public enum PrefixKind {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    70
        JAVAC("javac."),
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    71
        COMPILER_MISC("compiler.misc.");
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    72
        PrefixKind(String v) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    73
            value = v;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    74
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    75
        public String key(String k) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    76
            return value + k;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    77
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    78
        final String value;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    79
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    80
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    81
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    82
     * DiagnosticHandler's provide the initial handling for diagnostics.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    83
     * When a diagnostic handler is created and has been initialized, it
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    84
     * should install itself as the current diagnostic handler. When a
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    85
     * client has finished using a handler, the client should call
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    86
     * {@code log.removeDiagnosticHandler();}
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    87
     *
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    88
     * Note that javax.tools.DiagnosticListener (if set) is called later in the
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    89
     * diagnostic pipeline.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    90
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    91
    public static abstract class DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    92
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    93
         * The previously installed diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    94
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    95
        protected DiagnosticHandler prev;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    96
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    97
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    98
         * Install this diagnostic handler as the current one,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    99
         * recording the previous one.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   100
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   101
        protected void install(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   102
            prev = log.diagnosticHandler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   103
            log.diagnosticHandler = this;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   104
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   105
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   106
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   107
         * Handle a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   108
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   109
        public abstract void report(JCDiagnostic diag);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   110
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   111
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   112
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   113
     * A DiagnosticHandler that discards all diagnostics.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   114
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   115
    public static class DiscardDiagnosticHandler extends DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   116
        public DiscardDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   117
            install(log);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   118
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   119
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   120
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   121
        public void report(JCDiagnostic diag) { }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   122
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   123
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   124
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   125
     * A DiagnosticHandler that can defer some or all diagnostics,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   126
     * by buffering them for later examination and/or reporting.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   127
     * If a diagnostic is not deferred, or is subsequently reported
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   128
     * with reportAllDiagnostics(), it will be reported to the previously
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   129
     * active diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   130
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   131
    public static class DeferredDiagnosticHandler extends DiagnosticHandler {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 17582
diff changeset
   132
        private Queue<JCDiagnostic> deferred = new ListBuffer<>();
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   133
        private final Filter<JCDiagnostic> filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   134
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   135
        public DeferredDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   136
            this(log, null);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   137
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   138
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   139
        public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   140
            this.filter = filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   141
            install(log);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   142
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   143
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   144
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   145
        public void report(JCDiagnostic diag) {
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   146
            if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) &&
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   147
                (filter == null || filter.accepts(diag))) {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   148
                deferred.add(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   149
            } else {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   150
                prev.report(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   151
            }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   152
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   153
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   154
        public Queue<JCDiagnostic> getDiagnostics() {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   155
            return deferred;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   156
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   157
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   158
        /** Report all deferred diagnostics. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   159
        public void reportDeferredDiagnostics() {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   160
            reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   161
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   162
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   163
        /** Report selected deferred diagnostics. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   164
        public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   165
            JCDiagnostic d;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   166
            while ((d = deferred.poll()) != null) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   167
                if (kinds.contains(d.getKind()))
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   168
                    prev.report(d);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   169
            }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   170
            deferred = null; // prevent accidental ongoing use
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   171
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   172
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   173
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   174
    public enum WriterKind { NOTICE, WARNING, ERROR, STDOUT, STDERR }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   176
    private final Map<WriterKind, PrintWriter> writers;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** The maximum number of errors/warnings that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   180
    protected int MaxErrors;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   181
    protected int MaxWarnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    /** Switch: prompt user on each error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    public boolean promptOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    /** Switch: emit warning messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    public boolean emitWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
3660
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   191
    /** Switch: suppress note messages.
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   192
     */
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   193
    public boolean suppressNotes;
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   194
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    /** Print stack trace on errors?
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    public boolean dumpOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * Diagnostic listener, if provided through programmatic
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * interface to javac (JSR 199).
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    protected DiagnosticListener<? super JavaFileObject> diagListener;
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   204
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   206
     * Formatter for diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     */
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 867
diff changeset
   208
    private DiagnosticFormatter<JCDiagnostic> diagFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   210
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   211
     * Keys for expected diagnostics.
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   212
     */
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   213
    public Set<String> expectDiagKeys;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   214
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   215
    /**
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   216
     * Set to true if a compressed diagnostic is reported
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   217
     */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   218
    public boolean compressedOutput;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   219
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   220
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   221
     * JavacMessages object used for localization.
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   222
     */
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   223
    private JavacMessages messages;
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   224
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   225
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   226
     * Handler for initial dispatch of diagnostics.
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   227
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   228
    private DiagnosticHandler diagnosticHandler;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   229
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   230
    /** Get the Log instance for this context. */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   231
    public static Log instance(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   232
        Log instance = context.get(logKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   233
        if (instance == null)
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   234
            instance = new Log(context);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   235
        return instance;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   236
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   237
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   238
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   239
     * Register a Context.Factory to create a Log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   240
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   241
    public static void preRegister(Context context, PrintWriter w) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   242
        context.put(Log.class, (Context.Factory<Log>) (c -> new Log(c, w)));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   243
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   244
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   245
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   246
     * Construct a log with default settings.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   247
     * If no streams are set in the context, the log will be initialized to use
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   248
     * System.out for normal output, and System.err for all diagnostic output.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   249
     * If one stream is set in the context, with either Log.outKey or Log.errKey,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   250
     * it will be used for all output.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   251
     * Otherwise, the log will be initialized to use both streams found in the context.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   252
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   253
    protected Log(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   254
        this(context, initWriters(context));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   255
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   256
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   257
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   258
     * Initialize a map of writers based on values found in the context
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   259
     * @param context the context in which to find writers to use
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   260
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   261
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   262
    private static Map<WriterKind, PrintWriter> initWriters(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   263
        PrintWriter out = context.get(outKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   264
        PrintWriter err = context.get(errKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   265
        if (out == null && err == null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   266
            out = new PrintWriter(System.out, true);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   267
            err = new PrintWriter(System.err, true);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   268
            return initWriters(out, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   269
        } else if (out == null || err == null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   270
            PrintWriter pw = (out != null) ? out : err;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   271
            return initWriters(pw, pw);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   272
        } else {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   273
            return initWriters(out, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   274
        }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   275
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   276
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   277
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   278
     * Construct a log with all output sent to a single output stream.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   279
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   280
    protected Log(Context context, PrintWriter writer) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   281
        this(context, initWriters(writer, writer));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   282
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   283
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   284
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   285
     * Construct a log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   286
     * The log will be initialized to use stdOut for normal output, and stdErr
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   287
     * for all diagnostic output.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   289
    protected Log(Context context, PrintWriter out, PrintWriter err) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   290
        this(context, initWriters(out, err));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   291
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   292
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   293
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   294
     * Initialize a writer map for a stream for normal output, and a stream for diagnostics.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   295
     * @param out a stream to be used for normal output
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   296
     * @param err a stream to be used for diagnostic messages, such as errors, warnings, etc
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   297
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   298
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   299
    private static Map<WriterKind, PrintWriter> initWriters(PrintWriter out, PrintWriter err) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   300
        Map<WriterKind, PrintWriter> writers = new EnumMap<>(WriterKind.class);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   301
        writers.put(WriterKind.ERROR, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   302
        writers.put(WriterKind.WARNING, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   303
        writers.put(WriterKind.NOTICE, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   304
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   305
        writers.put(WriterKind.STDOUT, out);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   306
        writers.put(WriterKind.STDERR, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   307
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   308
        return writers;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   309
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   310
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   311
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   312
     * Construct a log with given I/O redirections.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   313
     * @deprecated
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   314
     * This constructor is provided to support the supported but now-deprecated javadoc entry point
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   315
     *      com.sun.tools.javadoc.Main.execute(String programName,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   316
     *          PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   317
     *          String defaultDocletClassName, String... args)
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   318
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   319
    @Deprecated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
    protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   321
        this(context, initWriters(errWriter, warnWriter, noticeWriter));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   322
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   323
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   324
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   325
     * Initialize a writer map with different streams for different types of diagnostics.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   326
     * @param errWriter a stream for writing error messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   327
     * @param warnWriter a stream for writing warning messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   328
     * @param noticeWriter a stream for writing notice messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   329
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   330
     * @deprecated This method exists to support a supported but now deprecated javadoc entry point.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   331
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   332
    @Deprecated
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   333
    private static Map<WriterKind, PrintWriter>  initWriters(PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   334
        Map<WriterKind, PrintWriter> writers = new EnumMap<>(WriterKind.class);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   335
        writers.put(WriterKind.ERROR, errWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   336
        writers.put(WriterKind.WARNING, warnWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   337
        writers.put(WriterKind.NOTICE, noticeWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   338
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   339
        writers.put(WriterKind.STDOUT, noticeWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   340
        writers.put(WriterKind.STDERR, errWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   341
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   342
        return writers;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   343
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   344
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   345
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   346
     * Creates a log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   347
     * @param context the context in which the log should be registered
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   348
     * @param writers a map of writers that can be accessed by the kind of writer required
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   349
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   350
    private Log(Context context, Map<WriterKind, PrintWriter> writers) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   351
        super(JCDiagnostic.Factory.instance(context));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        context.put(logKey, this);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   353
        this.writers = writers;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        @SuppressWarnings("unchecked") // FIXME
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   356
        DiagnosticListener<? super JavaFileObject> dl =
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            context.get(DiagnosticListener.class);
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   358
        this.diagListener = dl;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   359
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   360
        diagnosticHandler = new DefaultDiagnosticHandler();
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   361
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   362
        messages = JavacMessages.instance(context);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   363
        messages.add(Main.javacBundleName);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   364
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   365
        final Options options = Options.instance(context);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   366
        initOptions(options);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   367
        options.addListener(new Runnable() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   368
            @Override
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   369
            public void run() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   370
                initOptions(options);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   371
            }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   372
        });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    // where
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   375
        private void initOptions(Options options) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   376
            this.dumpOnError = options.isSet(DOE);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   377
            this.promptOnError = options.isSet(PROMPT);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   378
            this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   379
            this.suppressNotes = options.isSet("suppressNotes");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   380
            this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   381
            this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   382
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   383
            boolean rawDiagnostics = options.isSet("rawDiagnostics");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   384
            this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   385
                                                  new BasicDiagnosticFormatter(options, messages);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   386
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   387
            String ek = options.get("expectKeys");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   388
            if (ek != null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   389
                expectDiagKeys = new HashSet<>(Arrays.asList(ek.split(", *")));
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   390
        }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   391
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   392
        private int getIntOption(Options options, Option option, int defaultValue) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   393
            String s = options.get(option);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            try {
4699
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   395
                if (s != null) {
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   396
                    int n = Integer.parseInt(s);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   397
                    return (n <= 0 ? Integer.MAX_VALUE : n);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   398
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            } catch (NumberFormatException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                // silently ignore ill-formed numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            return defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   405
        /** Default value for -Xmaxerrs.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   406
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   407
        protected int getDefaultMaxErrors() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   408
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   409
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   410
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   411
        /** Default value for -Xmaxwarns.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   412
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   413
        protected int getDefaultMaxWarnings() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   414
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   415
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   416
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    /** The number of errors encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
    public int nerrors = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    /** The number of warnings encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    public int nwarnings = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    /** A set of all errors generated so far. This is used to avoid printing an
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     *  error message more than once. For each error, a pair consisting of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
     *  source file name and source code position of the error is added to the set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     */
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents: 29291
diff changeset
   429
    protected Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    public boolean hasDiagnosticListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        return diagListener != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   435
    public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28455
diff changeset
   436
        Assert.checkNonNull(name);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   437
        getSource(name).setEndPosTable(endPosTable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   440
    /** Return current sourcefile.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     */
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   442
    public JavaFileObject currentSourceFile() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        return source == null ? null : source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   446
    /** Get the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   447
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   448
    public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   449
        return diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   450
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   451
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   452
    /** Set the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   453
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   454
    public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   455
        this.diagFormatter = diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   456
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   457
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   458
    public PrintWriter getWriter(WriterKind kind) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   459
        return writers.get(kind);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   460
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   461
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   462
    public void setWriter(WriterKind kind, PrintWriter pw) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28455
diff changeset
   463
        Assert.checkNonNull(pw);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   464
        writers.put(kind, pw);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   465
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   466
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   467
    public void setWriters(PrintWriter pw) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   468
        Assert.checkNonNull(pw);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   469
        for (WriterKind k: WriterKind.values())
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   470
            writers.put(k, pw);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   471
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   472
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   473
    /**
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   474
     * Replace the specified diagnostic handler with the
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   475
     * handler that was current at the time this handler was created.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   476
     * The given handler must be the currently installed handler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   477
     * it must be specified explicitly for clarity and consistency checking.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   478
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   479
    public void popDiagnosticHandler(DiagnosticHandler h) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   480
        Assert.check(diagnosticHandler == h);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   481
        diagnosticHandler = h.prev;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   482
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   483
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    /** Flush the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    public void flush() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   487
        for (PrintWriter pw: writers.values()) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   488
            pw.flush();
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   489
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   492
    public void flush(WriterKind kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   493
        getWriter(kind).flush();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   494
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   495
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    /** Returns true if an error needs to be reported for a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
     * source name and pos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    protected boolean shouldReport(JavaFileObject file, int pos) {
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   500
        if (file == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   503
        Pair<JavaFileObject,Integer> coords = new Pair<>(file, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        boolean shouldReport = !recorded.contains(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        if (shouldReport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            recorded.add(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        return shouldReport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    /** Prompt user after an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    public void prompt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        if (promptOnError) {
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   514
            System.err.println(localize("resume.abort"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                    switch (System.in.read()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                    case 'a': case 'A':
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                        System.exit(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                    case 'r': case 'R':
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                    case 'x': case 'X':
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                        throw new AssertionError("user abort");
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                    default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            } catch (IOException e) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    /** Print the faulty source code line and point to the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
     *  @param pos   Buffer index of the error position, must be on current line
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
    private void printErrLine(int pos, PrintWriter writer) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   536
        String line = (source == null ? null : source.getLine(pos));
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   537
        if (line == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            return;
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   539
        int col = source.getColumnNumber(pos, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   541
        printRawLines(writer, line);
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   542
        for (int i = 0; i < col - 1; i++) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   543
            writer.print((line.charAt(i) == '\t') ? "\t" : " ");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        writer.println("^");
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   549
    public void printNewline() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   550
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   551
        noticeWriter.println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   552
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   553
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   554
    public void printNewline(WriterKind wk) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   555
        getWriter(wk).println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   556
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   557
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   558
    public void printLines(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   559
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   560
        printRawLines(noticeWriter, localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   561
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   562
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   563
    public void printLines(PrefixKind pk, String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   564
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   565
        printRawLines(noticeWriter, localize(pk, key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   566
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   567
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   568
    public void printLines(WriterKind wk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   569
        printRawLines(getWriter(wk), localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   570
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   571
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   572
    public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   573
        printRawLines(getWriter(wk), localize(pk, key, args));
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   574
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   575
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   576
    /** Print the text of a message, translating newlines appropriately
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   577
     *  for the platform.
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   578
     */
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   579
    public void printRawLines(String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   580
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   581
        printRawLines(noticeWriter, msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   582
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   583
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   584
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   585
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   586
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   587
    public void printRawLines(WriterKind kind, String msg) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   588
        printRawLines(getWriter(kind), msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   589
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   590
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   591
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   592
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   593
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   594
    public static void printRawLines(PrintWriter writer, String msg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        int nl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        while ((nl = msg.indexOf('\n')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            writer.println(msg.substring(0, nl));
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            msg = msg.substring(nl+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        if (msg.length() != 0) writer.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   603
    /**
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   604
     * Print the localized text of a "verbose" message to the
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   605
     * noticeWriter stream.
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   606
     */
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   607
    public void printVerbose(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   608
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   609
        printRawLines(noticeWriter, localize("verbose." + key, args));
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   610
    }
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   611
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   612
    @Override
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   613
    protected void directError(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   614
        PrintWriter errWriter = writers.get(WriterKind.ERROR);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   615
        printRawLines(errWriter, localize(key, args));
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   616
        errWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
    /** Report a warning that cannot be suppressed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
28334
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents: 25874
diff changeset
   625
        writeDiagnostic(diags.warning(null, source, pos, key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   629
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   630
     * Primary method to report a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   631
     * @param diagnostic
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   632
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   633
    @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   634
    public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   635
        diagnosticHandler.report(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   636
     }
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   637
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     * Common diagnostic handling.
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
     * The diagnostic is counted, and depending on the options and how many diagnostics have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
     * reported so far, the diagnostic may be handed off to writeDiagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   643
    private class DefaultDiagnosticHandler extends DiagnosticHandler {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   644
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   645
        public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   646
            if (expectDiagKeys != null)
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   647
                expectDiagKeys.remove(diagnostic.getCode());
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   648
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   649
            switch (diagnostic.getType()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   650
            case FRAGMENT:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   651
                throw new IllegalArgumentException();
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   652
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   653
            case NOTE:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   654
                // Print out notes only when we are permitted to report warnings
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   655
                // Notes are only generated at the end of a compilation, so should be small
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   656
                // in number.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   657
                if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   658
                    writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   659
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   660
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   662
            case WARNING:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   663
                if (emitWarnings || diagnostic.isMandatory()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   664
                    if (nwarnings < MaxWarnings) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   665
                        writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   666
                        nwarnings++;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   667
                    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   668
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   669
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   671
            case ERROR:
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   672
                if (nerrors < MaxErrors &&
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   673
                    (diagnostic.isFlagSet(DiagnosticFlag.MULTIPLE) ||
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   674
                     shouldReport(diagnostic.getSource(), diagnostic.getIntPosition()))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                    writeDiagnostic(diagnostic);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   676
                    nerrors++;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
                }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   678
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   680
            if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   681
                compressedOutput = true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   682
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
     * Write out a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
    protected void writeDiagnostic(JCDiagnostic diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        if (diagListener != null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   691
            diagListener.report(diag);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   692
            return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        PrintWriter writer = getWriterForDiagnosticType(diag.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   697
        printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            switch (diag.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
                prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        if (dumpOnError)
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            new RuntimeException().printStackTrace(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
        switch (dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
        case NOTE:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   720
            return writers.get(WriterKind.NOTICE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        case WARNING:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   723
            return writers.get(WriterKind.WARNING);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
        case ERROR:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   726
            return writers.get(WriterKind.ERROR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
            throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    /** Find a localized string in the resource bundle.
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   734
     *  Because this method is static, it ignores the locale.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   735
     *  Use localize(key, args) when possible.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     *  @param key    The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
     *  @param args   Fields to substitute into the string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
    public static String getLocalizedString(String key, Object ... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   740
        return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   743
    /** Find a localized string in the resource bundle.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   744
     *  @param key    The key for the localized string.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   745
     *  @param args   Fields to substitute into the string.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   746
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   747
    public String localize(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   748
        return localize(PrefixKind.COMPILER_MISC, key, args);
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   749
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   750
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   751
    public String localize(JCDiagnostic.DiagnosticInfo diagInfo) {
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   752
        if (useRawMessages) {
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   753
            return diagInfo.key();
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   754
        } else {
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   755
            return messages.getLocalizedString(diagInfo.key(), diagInfo.args);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   756
        }
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   757
    }
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   758
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   759
    /** Find a localized string in the resource bundle.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   760
     *  @param key    The key for the localized string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   761
     *  @param args   Fields to substitute into the string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   762
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   763
    public String localize(PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   764
        if (useRawMessages)
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   765
            return pk.key(key);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   766
        else
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   767
            return messages.getLocalizedString(pk.key(key), args);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   768
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   769
    // where
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   770
        // backdoor hook for testing, should transition to use -XDrawDiagnostics
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   771
        private static boolean useRawMessages = false;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   772
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
 * raw error messages without internationalization; used for experimentation
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
 * and quick prototyping
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   778
    /** print an error or warning message:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   779
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   780
    private void printRawDiag(PrintWriter pw, String prefix, int pos, String msg) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   781
        if (source == null || pos == Position.NOPOS) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   782
            printRawLines(pw, prefix + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
        } else {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   784
            int line = source.getLineNumber(pos);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   785
            JavaFileObject file = source.getFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
            if (file != null)
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   787
                printRawLines(pw,
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3660
diff changeset
   788
                           file.getName() + ":" +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
                           line + ": " + msg);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   790
            printErrLine(pos, pw);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        }
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   792
        pw.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   795
    /** report an error:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   796
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
    public void rawError(int pos, String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   798
        PrintWriter errWriter = writers.get(WriterKind.ERROR);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   799
        if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   800
            printRawDiag(errWriter, "error: ", pos, msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
            prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
            nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   807
    /** report a warning:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   808
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
    public void rawWarning(int pos, String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   810
        PrintWriter warnWriter = writers.get(WriterKind.ERROR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
        if (nwarnings < MaxWarnings && emitWarnings) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   812
            printRawDiag(warnWriter, "warning: ", pos, msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        nwarnings++;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   816
        warnWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    public static String format(String fmt, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
        return String.format((java.util.Locale)null, fmt, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
}