|
1 /* |
|
2 * Copyright (c) 2005, 2012, 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 */ |
|
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.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.*; |
|
36 |
|
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; |
|
50 |
|
51 JavacMessager(Context context, JavacProcessingEnvironment processingEnv) { |
|
52 log = Log.instance(context); |
|
53 this.processingEnv = processingEnv; |
|
54 } |
|
55 |
|
56 // processingEnv.getElementUtils() |
|
57 |
|
58 public void printMessage(Diagnostic.Kind kind, CharSequence msg) { |
|
59 printMessage(kind, msg, null, null, null); |
|
60 } |
|
61 |
|
62 public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
|
63 Element e) { |
|
64 printMessage(kind, msg, e, null, null); |
|
65 } |
|
66 |
|
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 } |
|
80 |
|
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 // save the old version and reinstate it later |
|
103 oldSource = log.useSource(newSource); |
|
104 pos = treeTop.fst.pos(); |
|
105 } |
|
106 } |
|
107 try { |
|
108 switch (kind) { |
|
109 case ERROR: |
|
110 errorCount++; |
|
111 boolean prev = log.multipleErrors; |
|
112 log.multipleErrors = true; |
|
113 try { |
|
114 log.error(pos, "proc.messager", msg.toString()); |
|
115 } finally { |
|
116 log.multipleErrors = prev; |
|
117 } |
|
118 break; |
|
119 |
|
120 case WARNING: |
|
121 warningCount++; |
|
122 log.warning(pos, "proc.messager", msg.toString()); |
|
123 break; |
|
124 |
|
125 case MANDATORY_WARNING: |
|
126 warningCount++; |
|
127 log.mandatoryWarning(pos, "proc.messager", msg.toString()); |
|
128 break; |
|
129 |
|
130 default: |
|
131 log.note(pos, "proc.messager", msg.toString()); |
|
132 break; |
|
133 } |
|
134 } finally { |
|
135 // reinstate the saved version, only if it was saved earlier |
|
136 if (newSource != null) |
|
137 log.useSource(oldSource); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Prints an error message. |
|
143 * Equivalent to {@code printError(null, msg)}. |
|
144 * @param msg the message, or an empty string if none |
|
145 */ |
|
146 public void printError(String msg) { |
|
147 printMessage(Diagnostic.Kind.ERROR, msg); |
|
148 } |
|
149 |
|
150 /** |
|
151 * Prints a warning message. |
|
152 * Equivalent to {@code printWarning(null, msg)}. |
|
153 * @param msg the message, or an empty string if none |
|
154 */ |
|
155 public void printWarning(String msg) { |
|
156 printMessage(Diagnostic.Kind.WARNING, msg); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Prints a notice. |
|
161 * @param msg the message, or an empty string if none |
|
162 */ |
|
163 public void printNotice(String msg) { |
|
164 printMessage(Diagnostic.Kind.NOTE, msg); |
|
165 } |
|
166 |
|
167 public boolean errorRaised() { |
|
168 return errorCount > 0; |
|
169 } |
|
170 |
|
171 public int errorCount() { |
|
172 return errorCount; |
|
173 } |
|
174 |
|
175 public int warningCount() { |
|
176 return warningCount; |
|
177 } |
|
178 |
|
179 public void newRound(Context context) { |
|
180 log = Log.instance(context); |
|
181 errorCount = 0; |
|
182 } |
|
183 |
|
184 public String toString() { |
|
185 return "javac Messager"; |
|
186 } |
|
187 } |