src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacMessager.java
author jlahoda
Fri, 15 Feb 2019 12:09:53 +0100
changeset 53773 454d54d8af1c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round Summary: When an annotation processor reports and error, defer reporting recoverable errors from the erroneous round to the last round, to avoid reporting errors that were resolved in the erroneous round. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 26266
diff changeset
     2
 * Copyright (c) 2005, 2015, 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: 1264
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: 1264
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: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
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.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.model.JavacElements;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
    29
import com.sun.tools.javac.resources.CompilerProperties.Errors;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
    30
import com.sun.tools.javac.resources.CompilerProperties.Notes;
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
    31
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    33
import com.sun.tools.javac.util.DefinedBy.Api;
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 26266
diff changeset
    34
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.*;
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 47216
diff changeset
    37
import java.util.Set;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.Diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * An implementation of the Messager built on top of log.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    46
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class JavacMessager implements Messager {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    JavacProcessingEnvironment processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    int errorCount = 0;
6153
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
    55
    int warningCount = 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    JavacMessager(Context context, JavacProcessingEnvironment processingEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        this.processingEnv = processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    // processingEnv.getElementUtils()
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    64
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        printMessage(kind, msg, null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    69
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                      Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        printMessage(kind, msg, e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * Prints a message of the specified kind at the location of the
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * annotation mirror of the annotated element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * @param kind the kind of message
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * @param e    the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * @param a    the annotation to use as a position hint
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    84
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                      Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        printMessage(kind, msg, e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * Prints a message of the specified kind at the location of the
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * annotation value inside the annotation mirror of the annotated
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * @param kind the kind of message
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     * @param e    the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * @param a    the annotation containing the annotaiton value
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * @param v    the annotation value to use as a position hint
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   101
    @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                      Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        JavaFileObject oldSource = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        JavaFileObject newSource = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        JCDiagnostic.DiagnosticPosition pos = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        JavacElements elemUtils = processingEnv.getElementUtils();
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        if (treeTop != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            newSource = treeTop.snd.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            if (newSource != null) {
12714
73f1f7097c5e 7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents: 7681
diff changeset
   112
                // save the old version and reinstate it later
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                oldSource = log.useSource(newSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                pos = treeTop.fst.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                errorCount++;
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 47216
diff changeset
   121
                log.error(DiagnosticFlag.API, pos, Errors.ProcMessager(msg.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            case WARNING:
6153
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   125
                warningCount++;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
   126
                log.warning(pos, Warnings.ProcMessager(msg.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            case MANDATORY_WARNING:
6153
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   130
                warningCount++;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
   131
                log.mandatoryWarning(pos, Warnings.ProcMessager(msg.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            default:
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 28455
diff changeset
   135
                log.note(pos, Notes.ProcMessager(msg.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        } finally {
12714
73f1f7097c5e 7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents: 7681
diff changeset
   139
            // reinstate the saved version, only if it was saved earlier
73f1f7097c5e 7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents: 7681
diff changeset
   140
            if (newSource != null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                log.useSource(oldSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * Prints an error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * Equivalent to {@code printError(null, msg)}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    public void printError(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        printMessage(Diagnostic.Kind.ERROR, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * Prints a warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Equivalent to {@code printWarning(null, msg)}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public void printWarning(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        printMessage(Diagnostic.Kind.WARNING, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * Prints a notice.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    public void printNotice(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        printMessage(Diagnostic.Kind.NOTE, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    public boolean errorRaised() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        return errorCount > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    public int errorCount() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        return errorCount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
6153
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   179
    public int warningCount() {
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   180
        return warningCount;
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   181
    }
277c719ff46e 6403456: -Werror should work with annotation processing
jjg
parents: 5847
diff changeset
   182
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 12714
diff changeset
   183
    public void newRound() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        errorCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        return "javac Messager";
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
}