Mon, 11 Apr 2011 16:31:22 -0700
Merge
1 /*
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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 supported API.
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;
49 int warningCount = 0;
51 JavacMessager(Context context, JavacProcessingEnvironment processingEnv) {
52 log = Log.instance(context);
53 this.processingEnv = processingEnv;
54 }
56 // processingEnv.getElementUtils()
58 public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
59 printMessage(kind, msg, null, null, null);
60 }
62 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
63 Element e) {
64 printMessage(kind, msg, e, null, null);
65 }
67 /**
68 * Prints a message of the specified kind at the location of the
69 * annotation mirror of the annotated element.
70 *
71 * @param kind the kind of message
72 * @param msg the message, or an empty string if none
73 * @param e the annotated element
74 * @param a the annotation to use as a position hint
75 */
76 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
77 Element e, AnnotationMirror a) {
78 printMessage(kind, msg, e, a, null);
79 }
81 /**
82 * Prints a message of the specified kind at the location of the
83 * annotation value inside the annotation mirror of the annotated
84 * element.
85 *
86 * @param kind the kind of message
87 * @param msg the message, or an empty string if none
88 * @param e the annotated element
89 * @param a the annotation containing the annotaiton value
90 * @param v the annotation value to use as a position hint
91 */
92 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
93 Element e, AnnotationMirror a, AnnotationValue v) {
94 JavaFileObject oldSource = null;
95 JavaFileObject newSource = null;
96 JCDiagnostic.DiagnosticPosition pos = null;
97 JavacElements elemUtils = processingEnv.getElementUtils();
98 Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v);
99 if (treeTop != null) {
100 newSource = treeTop.snd.sourcefile;
101 if (newSource != null) {
102 oldSource = log.useSource(newSource);
103 pos = treeTop.fst.pos();
104 }
105 }
106 try {
107 switch (kind) {
108 case ERROR:
109 errorCount++;
110 boolean prev = log.multipleErrors;
111 log.multipleErrors = true;
112 try {
113 log.error(pos, "proc.messager", msg.toString());
114 } finally {
115 log.multipleErrors = prev;
116 }
117 break;
119 case WARNING:
120 warningCount++;
121 log.warning(pos, "proc.messager", msg.toString());
122 break;
124 case MANDATORY_WARNING:
125 warningCount++;
126 log.mandatoryWarning(pos, "proc.messager", msg.toString());
127 break;
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 }
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 }
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 }
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 }
165 public boolean errorRaised() {
166 return errorCount > 0;
167 }
169 public int errorCount() {
170 return errorCount;
171 }
173 public int warningCount() {
174 return warningCount;
175 }
177 public void newRound(Context context) {
178 log = Log.instance(context);
179 errorCount = 0;
180 }
182 public String toString() {
183 return "javac Messager";
184 }
185 }