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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

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

mercurial