| 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 | */ |
| 25 | |
| 26 | package com.sun.tools.javac.processing; |
| 27 | |
| 28 | import com.sun.tools.javac.model.JavacElements; |
| 29 | import com.sun.tools.javac.util.*; |
| 30 | import com.sun.tools.javac.comp.*; |
| 31 | import com.sun.tools.javac.tree.JCTree; |
| 32 | import com.sun.tools.javac.tree.JCTree.*; |
| 33 | import com.sun.tools.javac.util.Position; |
| 34 | import javax.lang.model.element.*; |
| 35 | import javax.tools.JavaFileObject; |
| 36 | import javax.tools.Diagnostic; |
| 37 | import javax.annotation.processing.*; |
| 38 | import java.util.*; |
| 39 | |
| 40 | /** |
| 41 | * An implementation of the Messager built on top of log. |
| 42 | * |
| 43 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
| 44 | * If you write code that depends on this, you do so at your own risk. |
| 45 | * This code and its internal interfaces are subject to change or |
| 46 | * deletion without notice.</b> |
| 47 | */ |
| 48 | public class JavacMessager implements Messager { |
| 49 | Log log; |
| 50 | JavacProcessingEnvironment processingEnv; |
| 51 | int errorCount = 0; |
| 52 | |
| 53 | JavacMessager(Context context, JavacProcessingEnvironment processingEnv) { |
| 54 | log = Log.instance(context); |
| 55 | this.processingEnv = processingEnv; |
| 56 | } |
| 57 | |
| 58 | // processingEnv.getElementUtils() |
| 59 | |
| 60 | public void printMessage(Diagnostic.Kind kind, CharSequence msg) { |
| 61 | printMessage(kind, msg, null, null, null); |
| 62 | } |
| 63 | |
| 64 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
| 65 | Element e) { |
| 66 | printMessage(kind, msg, e, null, null); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Prints a message of the specified kind at the location of the |
| 71 | * annotation mirror of the annotated element. |
| 72 | * |
| 73 | * @param kind the kind of message |
| 74 | * @param msg the message, or an empty string if none |
| 75 | * @param e the annotated element |
| 76 | * @param a the annotation to use as a position hint |
| 77 | */ |
| 78 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
| 79 | Element e, AnnotationMirror a) { |
| 80 | printMessage(kind, msg, e, a, null); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Prints a message of the specified kind at the location of the |
| 85 | * annotation value inside the annotation mirror of the annotated |
| 86 | * element. |
| 87 | * |
| 88 | * @param kind the kind of message |
| 89 | * @param msg the message, or an empty string if none |
| 90 | * @param e the annotated element |
| 91 | * @param a the annotation containing the annotaiton value |
| 92 | * @param v the annotation value to use as a position hint |
| 93 | */ |
| 94 | public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
| 95 | Element e, AnnotationMirror a, AnnotationValue v) { |
| 96 | JavaFileObject oldSource = null; |
| 97 | JavaFileObject newSource = null; |
| 98 | JCDiagnostic.DiagnosticPosition pos = null; |
| 99 | JavacElements elemUtils = processingEnv.getElementUtils(); |
| 100 | Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v); |
| 101 | if (treeTop != null) { |
| 102 | newSource = treeTop.snd.sourcefile; |
| 103 | if (newSource != null) { |
| 104 | oldSource = log.useSource(newSource); |
| 105 | pos = treeTop.fst.pos(); |
| 106 | } |
| 107 | } |
| 108 | try { |
| 109 | switch (kind) { |
| 110 | case ERROR: |
| 111 | errorCount++; |
| 112 | boolean prev = log.multipleErrors; |
| 113 | log.multipleErrors = true; |
| 114 | try { |
| 115 | log.error(pos, "proc.messager", msg.toString()); |
| 116 | } finally { |
| 117 | log.multipleErrors = prev; |
| 118 | } |
| 119 | break; |
| 120 | |
| 121 | case WARNING: |
| 122 | log.warning(pos, "proc.messager", msg.toString()); |
| 123 | break; |
| 124 | |
| 125 | case MANDATORY_WARNING: |
| 126 | log.mandatoryWarning(pos, "proc.messager", msg.toString()); |
| 127 | break; |
| 128 | |
| 129 | default: |
| 130 | log.note(pos, "proc.messager", msg.toString()); |
| 131 | break; |
| 132 | } |
| 133 | } finally { |
| 134 | if (oldSource != null) |
| 135 | log.useSource(oldSource); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Prints an error message. |
| 141 | * Equivalent to {@code printError(null, msg)}. |
| 142 | * @param msg the message, or an empty string if none |
| 143 | */ |
| 144 | public void printError(String msg) { |
| 145 | printMessage(Diagnostic.Kind.ERROR, msg); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Prints a warning message. |
| 150 | * Equivalent to {@code printWarning(null, msg)}. |
| 151 | * @param msg the message, or an empty string if none |
| 152 | */ |
| 153 | public void printWarning(String msg) { |
| 154 | printMessage(Diagnostic.Kind.WARNING, msg); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Prints a notice. |
| 159 | * @param msg the message, or an empty string if none |
| 160 | */ |
| 161 | public void printNotice(String msg) { |
| 162 | printMessage(Diagnostic.Kind.NOTE, msg); |
| 163 | } |
| 164 | |
| 165 | public boolean errorRaised() { |
| 166 | return errorCount > 0; |
| 167 | } |
| 168 | |
| 169 | public int errorCount() { |
| 170 | return errorCount; |
| 171 | } |
| 172 | |
| 173 | public void newRound(Context context) { |
| 174 | log = Log.instance(context); |
| 175 | errorCount = 0; |
| 176 | } |
| 177 | |
| 178 | public String toString() { |
| 179 | return "javac Messager"; |
| 180 | } |
| 181 | } |