src/share/classes/com/sun/tools/javac/processing/JavacMessager.java

Tue, 16 Sep 2008 18:35:18 -0700

author
jjg
date
Tue, 16 Sep 2008 18:35:18 -0700
changeset 113
eff38cc97183
parent 104
5e89c4ca637c
child 117
24a47c3062fe
permissions
-rw-r--r--

6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
Reviewed-by: darcy, mcimadamore

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.processing;
    28 import com.sun.tools.javac.model.JavacElements;
    29 import com.sun.tools.javac.util.*;
    30 import com.sun.tools.javac.tree.JCTree;
    31 import com.sun.tools.javac.tree.JCTree.*;
    32 import javax.lang.model.element.*;
    33 import javax.tools.JavaFileObject;
    34 import javax.tools.Diagnostic;
    35 import javax.annotation.processing.*;
    37 /**
    38  * An implementation of the Messager built on top of log.
    39  *
    40  * <p><b>This is NOT part of any API supported by Sun Microsystems.
    41  * If you write code that depends on this, you do so at your own risk.
    42  * This code and its internal interfaces are subject to change or
    43  * deletion without notice.</b>
    44  */
    45 public class JavacMessager implements Messager {
    46     Log log;
    47     JavacProcessingEnvironment processingEnv;
    48     int errorCount = 0;
    50     JavacMessager(Context context, JavacProcessingEnvironment processingEnv) {
    51         log = Log.instance(context);
    52         this.processingEnv = processingEnv;
    53     }
    55     // processingEnv.getElementUtils()
    57     public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
    58         printMessage(kind, msg, null, null, null);
    59     }
    61     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
    62                       Element e) {
    63         printMessage(kind, msg, e, null, null);
    64     }
    66     /**
    67      * Prints a message of the specified kind at the location of the
    68      * annotation mirror of the annotated element.
    69      *
    70      * @param kind the kind of message
    71      * @param msg  the message, or an empty string if none
    72      * @param e    the annotated element
    73      * @param a    the annotation to use as a position hint
    74      */
    75     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
    76                       Element e, AnnotationMirror a) {
    77         printMessage(kind, msg, e, a, null);
    78     }
    80     /**
    81      * Prints a message of the specified kind at the location of the
    82      * annotation value inside the annotation mirror of the annotated
    83      * element.
    84      *
    85      * @param kind the kind of message
    86      * @param msg  the message, or an empty string if none
    87      * @param e    the annotated element
    88      * @param a    the annotation containing the annotaiton value
    89      * @param v    the annotation value to use as a position hint
    90      */
    91     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
    92                       Element e, AnnotationMirror a, AnnotationValue v) {
    93         JavaFileObject oldSource = null;
    94         JavaFileObject newSource = null;
    95         JCDiagnostic.DiagnosticPosition pos = null;
    96         JavacElements elemUtils = processingEnv.getElementUtils();
    97         Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
    98         if (treeTop != null) {
    99             newSource = treeTop.snd.sourcefile;
   100             if (newSource != null) {
   101                 oldSource = log.useSource(newSource);
   102                 pos = treeTop.fst.pos();
   103             }
   104         }
   105         try {
   106             switch (kind) {
   107             case ERROR:
   108                 errorCount++;
   109                 boolean prev = log.multipleErrors;
   110                 log.multipleErrors = true;
   111                 try {
   112                     log.error(pos, "proc.messager", msg.toString());
   113                 } finally {
   114                     log.multipleErrors = prev;
   115                 }
   116                 break;
   118             case WARNING:
   119                 log.warning(pos, "proc.messager", msg.toString());
   120                 break;
   122             case MANDATORY_WARNING:
   123                 log.mandatoryWarning(pos, "proc.messager", msg.toString());
   124                 break;
   126             default:
   127                 log.note(pos, "proc.messager", msg.toString());
   128                 break;
   129             }
   130         } finally {
   131             if (oldSource != null)
   132                 log.useSource(oldSource);
   133         }
   134     }
   136     /**
   137      * Prints an error message.
   138      * Equivalent to {@code printError(null, msg)}.
   139      * @param msg  the message, or an empty string if none
   140      */
   141     public void printError(String msg) {
   142         printMessage(Diagnostic.Kind.ERROR, msg);
   143     }
   145     /**
   146      * Prints a warning message.
   147      * Equivalent to {@code printWarning(null, msg)}.
   148      * @param msg  the message, or an empty string if none
   149      */
   150     public void printWarning(String msg) {
   151         printMessage(Diagnostic.Kind.WARNING, msg);
   152     }
   154     /**
   155      * Prints a notice.
   156      * @param msg  the message, or an empty string if none
   157      */
   158     public void printNotice(String msg) {
   159         printMessage(Diagnostic.Kind.NOTE, msg);
   160     }
   162     public boolean errorRaised() {
   163         return errorCount > 0;
   164     }
   166     public int errorCount() {
   167         return errorCount;
   168     }
   170     public void newRound(Context context) {
   171         log = Log.instance(context);
   172         errorCount = 0;
   173     }
   175     public String toString() {
   176         return "javac Messager";
   177     }
   178 }

mercurial