author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 6153 | 277c719ff46e |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 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 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
10 | 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 |
||
50 |
JavacMessager(Context context, JavacProcessingEnvironment processingEnv) { |
|
51 |
log = Log.instance(context); |
|
52 |
this.processingEnv = processingEnv; |
|
53 |
} |
|
54 |
||
55 |
// processingEnv.getElementUtils() |
|
56 |
||
57 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg) { |
|
58 |
printMessage(kind, msg, null, null, null); |
|
59 |
} |
|
60 |
||
61 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
|
62 |
Element e) { |
|
63 |
printMessage(kind, msg, e, null, null); |
|
64 |
} |
|
65 |
||
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 |
} |
|
79 |
||
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; |
|
117 |
||
118 |
case WARNING: |
|
119 |
log.warning(pos, "proc.messager", msg.toString()); |
|
120 |
break; |
|
121 |
||
122 |
case MANDATORY_WARNING: |
|
123 |
log.mandatoryWarning(pos, "proc.messager", msg.toString()); |
|
124 |
break; |
|
125 |
||
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 |
} |
|
135 |
||
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 |
} |
|
144 |
||
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 |
} |
|
153 |
||
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 |
} |
|
161 |
||
162 |
public boolean errorRaised() { |
|
163 |
return errorCount > 0; |
|
164 |
} |
|
165 |
||
166 |
public int errorCount() { |
|
167 |
return errorCount; |
|
168 |
} |
|
169 |
||
170 |
public void newRound(Context context) { |
|
171 |
log = Log.instance(context); |
|
172 |
errorCount = 0; |
|
173 |
} |
|
174 |
||
175 |
public String toString() { |
|
176 |
return "javac Messager"; |
|
177 |
} |
|
178 |
} |