langtools/src/share/classes/com/sun/tools/javac/processing/JavacMessager.java
author jjg
Fri, 29 Aug 2008 11:10:12 -0700
changeset 1206 3a05355982a9
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6597471: unused imports in javax.tools.JavaCompiler 6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java Reviewed-by: mcimadamore Contributed-by: davide.angelocola@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.tools.Diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * An implementation of the Messager built on top of log.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class JavacMessager implements Messager {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    JavacProcessingEnvironment processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    int errorCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    JavacMessager(Context context, JavacProcessingEnvironment processingEnv) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        this.processingEnv = processingEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    // processingEnv.getElementUtils()
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        printMessage(kind, msg, null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
                      Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        printMessage(kind, msg, e, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * Prints a message of the specified kind at the location of the
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * annotation mirror of the annotated element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @param kind the kind of message
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @param e    the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param a    the annotation to use as a position hint
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                      Element e, AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        printMessage(kind, msg, e, a, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * Prints a message of the specified kind at the location of the
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * annotation value inside the annotation mirror of the annotated
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * @param kind the kind of message
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * @param e    the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * @param a    the annotation containing the annotaiton value
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * @param v    the annotation value to use as a position hint
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    public void printMessage(Diagnostic.Kind kind, CharSequence msg,
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                      Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        JavaFileObject oldSource = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        JavaFileObject newSource = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        JCDiagnostic.DiagnosticPosition pos = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        JavacElements elemUtils = processingEnv.getElementUtils();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if (treeTop != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            newSource = treeTop.snd.sourcefile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            if (newSource != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                oldSource = log.useSource(newSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                pos = treeTop.fst.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                errorCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                boolean prev = log.multipleErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                log.multipleErrors = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                    log.error(pos, "proc.messager", msg.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                    log.multipleErrors = prev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                log.warning(pos, "proc.messager", msg.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            case MANDATORY_WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                log.mandatoryWarning(pos, "proc.messager", msg.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                log.note(pos, "proc.messager", msg.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            if (oldSource != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                log.useSource(oldSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * Prints an error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * Equivalent to {@code printError(null, msg)}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    public void printError(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        printMessage(Diagnostic.Kind.ERROR, msg);
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 a warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * Equivalent to {@code printWarning(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 printWarning(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        printMessage(Diagnostic.Kind.WARNING, 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 notice.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * @param msg  the message, or an empty string if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    public void printNotice(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        printMessage(Diagnostic.Kind.NOTE, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public boolean errorRaised() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return errorCount > 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public int errorCount() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        return errorCount;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public void newRound(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        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 String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        return "javac Messager";
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
}