author | jlahoda |
Thu, 15 Jun 2017 13:44:42 +0200 | |
changeset 45504 | ea7475564d07 |
parent 28455 | 41245007c074 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
26266
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, 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; |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
29 |
import com.sun.tools.javac.resources.CompilerProperties.Errors; |
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
30 |
import com.sun.tools.javac.resources.CompilerProperties.Notes; |
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
31 |
import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
10 | 32 |
import com.sun.tools.javac.util.*; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
33 |
import com.sun.tools.javac.util.DefinedBy.Api; |
28455
41245007c074
8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents:
26266
diff
changeset
|
34 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
10 | 35 |
import com.sun.tools.javac.tree.JCTree; |
36 |
import com.sun.tools.javac.tree.JCTree.*; |
|
37 |
import javax.lang.model.element.*; |
|
38 |
import javax.tools.JavaFileObject; |
|
39 |
import javax.tools.Diagnostic; |
|
40 |
import javax.annotation.processing.*; |
|
41 |
||
42 |
/** |
|
43 |
* An implementation of the Messager built on top of log. |
|
44 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
45 |
* <p><b>This is NOT part of any supported API. |
10 | 46 |
* If you write code that depends on this, you do so at your own risk. |
47 |
* This code and its internal interfaces are subject to change or |
|
48 |
* deletion without notice.</b> |
|
49 |
*/ |
|
50 |
public class JavacMessager implements Messager { |
|
51 |
Log log; |
|
52 |
JavacProcessingEnvironment processingEnv; |
|
53 |
int errorCount = 0; |
|
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
54 |
int warningCount = 0; |
10 | 55 |
|
56 |
JavacMessager(Context context, JavacProcessingEnvironment processingEnv) { |
|
57 |
log = Log.instance(context); |
|
58 |
this.processingEnv = processingEnv; |
|
59 |
} |
|
60 |
||
61 |
// processingEnv.getElementUtils() |
|
62 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
63 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 64 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg) { |
65 |
printMessage(kind, msg, null, null, null); |
|
66 |
} |
|
67 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
68 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 69 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
70 |
Element e) { |
|
71 |
printMessage(kind, msg, e, null, null); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Prints a message of the specified kind at the location of the |
|
76 |
* annotation mirror of the annotated element. |
|
77 |
* |
|
78 |
* @param kind the kind of message |
|
79 |
* @param msg the message, or an empty string if none |
|
80 |
* @param e the annotated element |
|
81 |
* @param a the annotation to use as a position hint |
|
82 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
83 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 84 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
85 |
Element e, AnnotationMirror a) { |
|
86 |
printMessage(kind, msg, e, a, null); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Prints a message of the specified kind at the location of the |
|
91 |
* annotation value inside the annotation mirror of the annotated |
|
92 |
* element. |
|
93 |
* |
|
94 |
* @param kind the kind of message |
|
95 |
* @param msg the message, or an empty string if none |
|
96 |
* @param e the annotated element |
|
97 |
* @param a the annotation containing the annotaiton value |
|
98 |
* @param v the annotation value to use as a position hint |
|
99 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
100 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 101 |
public void printMessage(Diagnostic.Kind kind, CharSequence msg, |
102 |
Element e, AnnotationMirror a, AnnotationValue v) { |
|
103 |
JavaFileObject oldSource = null; |
|
104 |
JavaFileObject newSource = null; |
|
105 |
JCDiagnostic.DiagnosticPosition pos = null; |
|
106 |
JavacElements elemUtils = processingEnv.getElementUtils(); |
|
107 |
Pair<JCTree, JCCompilationUnit> treeTop = elemUtils.getTreeAndTopLevel(e, a, v); |
|
108 |
if (treeTop != null) { |
|
109 |
newSource = treeTop.snd.sourcefile; |
|
110 |
if (newSource != null) { |
|
12714
73f1f7097c5e
7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents:
7681
diff
changeset
|
111 |
// save the old version and reinstate it later |
10 | 112 |
oldSource = log.useSource(newSource); |
113 |
pos = treeTop.fst.pos(); |
|
114 |
} |
|
115 |
} |
|
116 |
try { |
|
117 |
switch (kind) { |
|
118 |
case ERROR: |
|
119 |
errorCount++; |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
120 |
log.error(DiagnosticFlag.MULTIPLE, pos, Errors.ProcMessager(msg.toString())); |
10 | 121 |
break; |
122 |
||
123 |
case WARNING: |
|
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
124 |
warningCount++; |
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
125 |
log.warning(pos, Warnings.ProcMessager(msg.toString())); |
10 | 126 |
break; |
127 |
||
128 |
case MANDATORY_WARNING: |
|
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
129 |
warningCount++; |
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
130 |
log.mandatoryWarning(pos, Warnings.ProcMessager(msg.toString())); |
10 | 131 |
break; |
132 |
||
133 |
default: |
|
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
28455
diff
changeset
|
134 |
log.note(pos, Notes.ProcMessager(msg.toString())); |
10 | 135 |
break; |
136 |
} |
|
137 |
} finally { |
|
12714
73f1f7097c5e
7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents:
7681
diff
changeset
|
138 |
// reinstate the saved version, only if it was saved earlier |
73f1f7097c5e
7166010: (javac) JavacMessager incorrectly restores log source file
ksrini
parents:
7681
diff
changeset
|
139 |
if (newSource != null) |
10 | 140 |
log.useSource(oldSource); |
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Prints an error message. |
|
146 |
* Equivalent to {@code printError(null, msg)}. |
|
147 |
* @param msg the message, or an empty string if none |
|
148 |
*/ |
|
149 |
public void printError(String msg) { |
|
150 |
printMessage(Diagnostic.Kind.ERROR, msg); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Prints a warning message. |
|
155 |
* Equivalent to {@code printWarning(null, msg)}. |
|
156 |
* @param msg the message, or an empty string if none |
|
157 |
*/ |
|
158 |
public void printWarning(String msg) { |
|
159 |
printMessage(Diagnostic.Kind.WARNING, msg); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Prints a notice. |
|
164 |
* @param msg the message, or an empty string if none |
|
165 |
*/ |
|
166 |
public void printNotice(String msg) { |
|
167 |
printMessage(Diagnostic.Kind.NOTE, msg); |
|
168 |
} |
|
169 |
||
170 |
public boolean errorRaised() { |
|
171 |
return errorCount > 0; |
|
172 |
} |
|
173 |
||
174 |
public int errorCount() { |
|
175 |
return errorCount; |
|
176 |
} |
|
177 |
||
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
178 |
public int warningCount() { |
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
179 |
return warningCount; |
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
180 |
} |
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
5847
diff
changeset
|
181 |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
12714
diff
changeset
|
182 |
public void newRound() { |
10 | 183 |
errorCount = 0; |
184 |
} |
|
185 |
||
186 |
public String toString() { |
|
187 |
return "javac Messager"; |
|
188 |
} |
|
189 |
} |