author | jjg |
Wed, 20 Jun 2012 13:23:26 -0700 | |
changeset 13077 | 16fb753bb5dc |
parent 11055 | ec1418effa77 |
child 15385 | ee1eebe7e210 |
permissions | -rw-r--r-- |
4870 | 1 |
/* |
13077 | 2 |
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
4870 | 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5520 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
4870 | 22 |
*/ |
23 |
||
24 |
import java.awt.BorderLayout; |
|
25 |
import java.awt.Color; |
|
26 |
import java.awt.Dimension; |
|
27 |
import java.awt.EventQueue; |
|
28 |
import java.awt.Font; |
|
29 |
import java.awt.GridBagConstraints; |
|
30 |
import java.awt.GridBagLayout; |
|
31 |
import java.awt.Rectangle; |
|
32 |
import java.awt.event.ActionEvent; |
|
33 |
import java.awt.event.ActionListener; |
|
34 |
import java.awt.event.MouseAdapter; |
|
35 |
import java.awt.event.MouseEvent; |
|
36 |
import java.io.File; |
|
37 |
import java.io.IOException; |
|
38 |
import java.io.PrintStream; |
|
39 |
import java.io.PrintWriter; |
|
40 |
import java.io.StringWriter; |
|
41 |
import java.lang.reflect.Field; |
|
42 |
import java.lang.reflect.Modifier; |
|
43 |
import java.nio.charset.Charset; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Collections; |
|
46 |
import java.util.HashMap; |
|
47 |
import java.util.HashSet; |
|
48 |
import java.util.Iterator; |
|
49 |
import java.util.List; |
|
50 |
import java.util.Map; |
|
51 |
import java.util.Set; |
|
52 |
import javax.swing.DefaultComboBoxModel; |
|
53 |
import javax.swing.JComboBox; |
|
54 |
import javax.swing.JComponent; |
|
55 |
import javax.swing.JFrame; |
|
56 |
import javax.swing.JLabel; |
|
57 |
import javax.swing.JPanel; |
|
58 |
import javax.swing.JScrollPane; |
|
59 |
import javax.swing.JTextArea; |
|
60 |
import javax.swing.JTextField; |
|
61 |
import javax.swing.SwingUtilities; |
|
62 |
import javax.swing.event.CaretEvent; |
|
63 |
import javax.swing.event.CaretListener; |
|
64 |
import javax.swing.text.BadLocationException; |
|
65 |
import javax.swing.text.DefaultHighlighter; |
|
66 |
import javax.swing.text.Highlighter; |
|
67 |
import javax.tools.Diagnostic; |
|
68 |
import javax.tools.DiagnosticListener; |
|
69 |
import javax.tools.JavaFileObject; |
|
70 |
import javax.tools.StandardJavaFileManager; |
|
71 |
||
72 |
import com.sun.source.tree.CompilationUnitTree; |
|
73 |
import com.sun.source.util.JavacTask; |
|
74 |
import com.sun.tools.javac.api.JavacTool; |
|
75 |
import com.sun.tools.javac.code.Flags; |
|
13077 | 76 |
import com.sun.tools.javac.tree.EndPosTable; |
4870 | 77 |
import com.sun.tools.javac.tree.JCTree; |
78 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
|
79 |
import com.sun.tools.javac.tree.JCTree.JCNewClass; |
|
80 |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
|
81 |
import com.sun.tools.javac.tree.TreeInfo; |
|
82 |
import com.sun.tools.javac.tree.TreeScanner; |
|
83 |
||
10950 | 84 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
4870 | 85 |
import static com.sun.tools.javac.util.Position.NOPOS; |
86 |
||
87 |
/** |
|
88 |
* Utility and test program to check validity of tree positions for tree nodes. |
|
89 |
* The program can be run standalone, or as a jtreg test. In standalone mode, |
|
90 |
* errors can be displayed in a gui viewer. For info on command line args, |
|
91 |
* run program with no args. |
|
92 |
* |
|
93 |
* <p> |
|
94 |
* jtreg: Note that by using the -r switch in the test description below, this test |
|
95 |
* will process all java files in the langtools/test directory, thus implicitly |
|
96 |
* covering any new language features that may be tested in this test suite. |
|
97 |
*/ |
|
98 |
||
99 |
/* |
|
100 |
* @test |
|
101 |
* @bug 6919889 |
|
102 |
* @summary assorted position errors in compiler syntax trees |
|
4933 | 103 |
* @run main TreePosTest -q -r -ef ./tools/javac/typeAnnotations -ef ./tools/javap/typeAnnotations -et ANNOTATED_TYPE . |
4870 | 104 |
*/ |
105 |
public class TreePosTest { |
|
106 |
/** |
|
107 |
* Main entry point. |
|
108 |
* If test.src is set, program runs in jtreg mode, and will throw an Error |
|
109 |
* if any errors arise, otherwise System.exit will be used, unless the gui |
|
110 |
* viewer is being used. In jtreg mode, the default base directory for file |
|
111 |
* args is the value of ${test.src}. In jtreg mode, the -r option can be |
|
112 |
* given to change the default base directory to the root test directory. |
|
113 |
*/ |
|
114 |
public static void main(String... args) { |
|
115 |
String testSrc = System.getProperty("test.src"); |
|
116 |
File baseDir = (testSrc == null) ? null : new File(testSrc); |
|
117 |
boolean ok = new TreePosTest().run(baseDir, args); |
|
118 |
if (!ok) { |
|
119 |
if (testSrc != null) // jtreg mode |
|
120 |
throw new Error("failed"); |
|
121 |
else |
|
122 |
System.exit(1); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Run the program. A base directory can be provided for file arguments. |
|
128 |
* In jtreg mode, the -r option can be given to change the default base |
|
129 |
* directory to the test root directory. For other options, see usage(). |
|
130 |
* @param baseDir base directory for any file arguments. |
|
131 |
* @param args command line args |
|
132 |
* @return true if successful or in gui mode |
|
133 |
*/ |
|
134 |
boolean run(File baseDir, String... args) { |
|
135 |
if (args.length == 0) { |
|
136 |
usage(System.out); |
|
137 |
return true; |
|
138 |
} |
|
139 |
||
140 |
List<File> files = new ArrayList<File>(); |
|
141 |
for (int i = 0; i < args.length; i++) { |
|
142 |
String arg = args[i]; |
|
143 |
if (arg.equals("-encoding") && i + 1 < args.length) |
|
144 |
encoding = args[++i]; |
|
145 |
else if (arg.equals("-gui")) |
|
146 |
gui = true; |
|
147 |
else if (arg.equals("-q")) |
|
148 |
quiet = true; |
|
149 |
else if (arg.equals("-v")) |
|
150 |
verbose = true; |
|
151 |
else if (arg.equals("-t") && i + 1 < args.length) |
|
152 |
tags.add(args[++i]); |
|
153 |
else if (arg.equals("-ef") && i + 1 < args.length) |
|
154 |
excludeFiles.add(new File(baseDir, args[++i])); |
|
4933 | 155 |
else if (arg.equals("-et") && i + 1 < args.length) |
156 |
excludeTags.add(args[++i]); |
|
4870 | 157 |
else if (arg.equals("-r")) { |
158 |
if (excludeFiles.size() > 0) |
|
159 |
throw new Error("-r must be used before -ef"); |
|
160 |
File d = baseDir; |
|
161 |
while (!new File(d, "TEST.ROOT").exists()) { |
|
162 |
d = d.getParentFile(); |
|
163 |
if (d == null) |
|
164 |
throw new Error("cannot find TEST.ROOT"); |
|
165 |
} |
|
166 |
baseDir = d; |
|
167 |
} |
|
168 |
else if (arg.startsWith("-")) |
|
169 |
throw new Error("unknown option: " + arg); |
|
170 |
else { |
|
171 |
while (i < args.length) |
|
172 |
files.add(new File(baseDir, args[i++])); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
for (File file: files) { |
|
177 |
if (file.exists()) |
|
178 |
test(file); |
|
179 |
else |
|
180 |
error("File not found: " + file); |
|
181 |
} |
|
182 |
||
183 |
if (fileCount != 1) |
|
184 |
System.err.println(fileCount + " files read"); |
|
185 |
if (errors > 0) |
|
186 |
System.err.println(errors + " errors"); |
|
187 |
||
188 |
return (gui || errors == 0); |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Print command line help. |
|
193 |
* @param out output stream |
|
194 |
*/ |
|
195 |
void usage(PrintStream out) { |
|
196 |
out.println("Usage:"); |
|
197 |
out.println(" java TreePosTest options... files..."); |
|
198 |
out.println(""); |
|
199 |
out.println("where options include:"); |
|
200 |
out.println("-gui Display returns in a GUI viewer"); |
|
201 |
out.println("-q Quiet: don't report on inapplicable files"); |
|
202 |
out.println("-v Verbose: report on files as they are being read"); |
|
203 |
out.println("-t tag Limit checks to tree nodes with this tag"); |
|
204 |
out.println(" Can be repeated if desired"); |
|
205 |
out.println("-ef file Exclude file or directory"); |
|
4933 | 206 |
out.println("-et tag Exclude tree nodes with given tag name"); |
4870 | 207 |
out.println(""); |
208 |
out.println("files may be directories or files"); |
|
209 |
out.println("directories will be scanned recursively"); |
|
210 |
out.println("non java files, or java files which cannot be parsed, will be ignored"); |
|
211 |
out.println(""); |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Test a file. If the file is a directory, it will be recursively scanned |
|
216 |
* for java files. |
|
217 |
* @param file the file or directory to test |
|
218 |
*/ |
|
219 |
void test(File file) { |
|
220 |
if (excludeFiles.contains(file)) { |
|
221 |
if (!quiet) |
|
222 |
error("File " + file + " excluded"); |
|
223 |
return; |
|
224 |
} |
|
225 |
||
226 |
if (file.isDirectory()) { |
|
227 |
for (File f: file.listFiles()) { |
|
228 |
test(f); |
|
229 |
} |
|
230 |
return; |
|
231 |
} |
|
232 |
||
233 |
if (file.isFile() && file.getName().endsWith(".java")) { |
|
234 |
try { |
|
235 |
if (verbose) |
|
236 |
System.err.println(file); |
|
237 |
fileCount++; |
|
238 |
PosTester p = new PosTester(); |
|
239 |
p.test(read(file)); |
|
240 |
} catch (ParseException e) { |
|
241 |
if (!quiet) { |
|
242 |
error("Error parsing " + file + "\n" + e.getMessage()); |
|
243 |
} |
|
244 |
} catch (IOException e) { |
|
245 |
error("Error reading " + file + ": " + e); |
|
246 |
} |
|
247 |
return; |
|
248 |
} |
|
249 |
||
250 |
if (!quiet) |
|
251 |
error("File " + file + " ignored"); |
|
252 |
} |
|
253 |
||
7846
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
254 |
// See CR: 6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
255 |
StringWriter sw = new StringWriter(); |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
256 |
PrintWriter pw = new PrintWriter(sw); |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
257 |
Reporter r = new Reporter(pw); |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
258 |
JavacTool tool = JavacTool.create(); |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
259 |
StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null); |
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
260 |
|
4870 | 261 |
/** |
262 |
* Read a file. |
|
263 |
* @param file the file to be read |
|
264 |
* @return the tree for the content of the file |
|
265 |
* @throws IOException if any IO errors occur |
|
266 |
* @throws TreePosTest.ParseException if any errors occur while parsing the file |
|
267 |
*/ |
|
268 |
JCCompilationUnit read(File file) throws IOException, ParseException { |
|
269 |
JavacTool tool = JavacTool.create(); |
|
7846
f72ce57eb9c1
6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents:
7072
diff
changeset
|
270 |
r.errors = 0; |
4870 | 271 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file); |
272 |
JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files); |
|
273 |
Iterable<? extends CompilationUnitTree> trees = task.parse(); |
|
274 |
pw.flush(); |
|
275 |
if (r.errors > 0) |
|
276 |
throw new ParseException(sw.toString()); |
|
277 |
Iterator<? extends CompilationUnitTree> iter = trees.iterator(); |
|
278 |
if (!iter.hasNext()) |
|
279 |
throw new Error("no trees found"); |
|
280 |
JCCompilationUnit t = (JCCompilationUnit) iter.next(); |
|
281 |
if (iter.hasNext()) |
|
282 |
throw new Error("too many trees found"); |
|
283 |
return t; |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Report an error. When the program is complete, the program will either |
|
288 |
* exit or throw an Error if any errors have been reported. |
|
289 |
* @param msg the error message |
|
290 |
*/ |
|
291 |
void error(String msg) { |
|
292 |
System.err.println(msg); |
|
293 |
errors++; |
|
294 |
} |
|
295 |
||
10950 | 296 |
/** |
297 |
* Names for tree tags. |
|
298 |
*/ |
|
299 |
private static String getTagName(JCTree.Tag tag) { |
|
300 |
String name = tag.name(); |
|
301 |
return (name == null) ? "??" : name; |
|
302 |
} |
|
303 |
||
4870 | 304 |
/** Number of files that have been analyzed. */ |
305 |
int fileCount; |
|
306 |
/** Number of errors reported. */ |
|
307 |
int errors; |
|
308 |
/** Flag: don't report irrelevant files. */ |
|
309 |
boolean quiet; |
|
310 |
/** Flag: report files as they are processed. */ |
|
311 |
boolean verbose; |
|
312 |
/** Flag: show errors in GUI viewer. */ |
|
313 |
boolean gui; |
|
314 |
/** Option: encoding for test files. */ |
|
315 |
String encoding; |
|
316 |
/** The GUI viewer for errors. */ |
|
317 |
Viewer viewer; |
|
318 |
/** The set of tags for tree nodes to be analyzed; if empty, all tree nodes |
|
319 |
* are analyzed. */ |
|
320 |
Set<String> tags = new HashSet<String>(); |
|
321 |
/** Set of files and directories to be excluded from analysis. */ |
|
322 |
Set<File> excludeFiles = new HashSet<File>(); |
|
4933 | 323 |
/** Set of tag names to be excluded from analysis. */ |
324 |
Set<String> excludeTags = new HashSet<String>(); |
|
4870 | 325 |
|
326 |
/** |
|
327 |
* Main class for testing assertions concerning tree positions for tree nodes. |
|
328 |
*/ |
|
329 |
private class PosTester extends TreeScanner { |
|
330 |
void test(JCCompilationUnit tree) { |
|
331 |
sourcefile = tree.sourcefile; |
|
332 |
endPosTable = tree.endPositions; |
|
333 |
encl = new Info(); |
|
334 |
tree.accept(this); |
|
335 |
} |
|
336 |
||
337 |
@Override |
|
338 |
public void scan(JCTree tree) { |
|
339 |
if (tree == null) |
|
340 |
return; |
|
341 |
||
342 |
Info self = new Info(tree, endPosTable); |
|
4933 | 343 |
if (check(encl, self)) { |
4870 | 344 |
// Modifiers nodes are present throughout the tree even where |
345 |
// there is no corresponding source text. |
|
346 |
// Redundant semicolons in a class definition can cause empty |
|
347 |
// initializer blocks with no positions. |
|
10950 | 348 |
if ((self.tag == MODIFIERS || self.tag == BLOCK) |
4870 | 349 |
&& self.pos == NOPOS) { |
350 |
// If pos is NOPOS, so should be the start and end positions |
|
351 |
check("start == NOPOS", encl, self, self.start == NOPOS); |
|
352 |
check("end == NOPOS", encl, self, self.end == NOPOS); |
|
353 |
} else { |
|
354 |
// For this node, start , pos, and endpos should be all defined |
|
355 |
check("start != NOPOS", encl, self, self.start != NOPOS); |
|
356 |
check("pos != NOPOS", encl, self, self.pos != NOPOS); |
|
357 |
check("end != NOPOS", encl, self, self.end != NOPOS); |
|
358 |
// The following should normally be ordered |
|
359 |
// encl.start <= start <= pos <= end <= encl.end |
|
360 |
// In addition, the position of the enclosing node should be |
|
361 |
// within this node. |
|
362 |
// The primary exceptions are for array type nodes, because of the |
|
363 |
// need to support legacy syntax: |
|
364 |
// e.g. int a[]; int[] b[]; int f()[] { return null; } |
|
365 |
// and because of inconsistent nesting of left and right of |
|
366 |
// array declarations: |
|
367 |
// e.g. int[][] a = new int[2][]; |
|
368 |
check("encl.start <= start", encl, self, encl.start <= self.start); |
|
369 |
check("start <= pos", encl, self, self.start <= self.pos); |
|
10950 | 370 |
if (!(self.tag == TYPEARRAY |
371 |
&& (encl.tag == VARDEF || |
|
372 |
encl.tag == METHODDEF || |
|
373 |
encl.tag == TYPEARRAY))) { |
|
4870 | 374 |
check("encl.pos <= start || end <= encl.pos", |
375 |
encl, self, encl.pos <= self.start || self.end <= encl.pos); |
|
376 |
} |
|
377 |
check("pos <= end", encl, self, self.pos <= self.end); |
|
10950 | 378 |
if (!(self.tag == TYPEARRAY && encl.tag == TYPEARRAY)) { |
4870 | 379 |
check("end <= encl.end", encl, self, self.end <= encl.end); |
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
Info prevEncl = encl; |
|
385 |
encl = self; |
|
386 |
tree.accept(this); |
|
387 |
encl = prevEncl; |
|
388 |
} |
|
389 |
||
390 |
@Override |
|
391 |
public void visitVarDef(JCVariableDecl tree) { |
|
392 |
// enum member declarations are desugared in the parser and have |
|
393 |
// ill-defined semantics for tree positions, so for now, we |
|
394 |
// skip the synthesized bits and just check parts which came from |
|
395 |
// the original source text |
|
396 |
if ((tree.mods.flags & Flags.ENUM) != 0) { |
|
397 |
scan(tree.mods); |
|
398 |
if (tree.init != null) { |
|
10950 | 399 |
if (tree.init.hasTag(NEWCLASS)) { |
4870 | 400 |
JCNewClass init = (JCNewClass) tree.init; |
401 |
if (init.args != null && init.args.nonEmpty()) { |
|
402 |
scan(init.args); |
|
403 |
} |
|
404 |
if (init.def != null && init.def.defs != null) { |
|
405 |
scan(init.def.defs); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
} else |
|
410 |
super.visitVarDef(tree); |
|
411 |
} |
|
412 |
||
4933 | 413 |
boolean check(Info encl, Info self) { |
414 |
if (excludeTags.size() > 0) { |
|
10950 | 415 |
if (encl != null && excludeTags.contains(getTagName(encl.tag)) |
416 |
|| excludeTags.contains(getTagName(self.tag))) |
|
4933 | 417 |
return false; |
418 |
} |
|
10950 | 419 |
return tags.size() == 0 || tags.contains(getTagName(self.tag)); |
4870 | 420 |
} |
421 |
||
422 |
void check(String label, Info encl, Info self, boolean ok) { |
|
423 |
if (!ok) { |
|
424 |
if (gui) { |
|
425 |
if (viewer == null) |
|
426 |
viewer = new Viewer(); |
|
427 |
viewer.addEntry(sourcefile, label, encl, self); |
|
428 |
} |
|
429 |
||
430 |
String s = self.tree.toString(); |
|
431 |
String msg = sourcefile.getName() + ": " + label + ": " + |
|
432 |
"encl:" + encl + " this:" + self + "\n" + |
|
433 |
s.substring(0, Math.min(80, s.length())).replaceAll("[\r\n]+", " "); |
|
434 |
error(msg); |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
JavaFileObject sourcefile; |
|
11055 | 439 |
EndPosTable endPosTable; |
4870 | 440 |
Info encl; |
441 |
||
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Utility class providing easy access to position and other info for a tree node. |
|
446 |
*/ |
|
447 |
private class Info { |
|
448 |
Info() { |
|
449 |
tree = null; |
|
10950 | 450 |
tag = ERRONEOUS; |
4870 | 451 |
start = 0; |
452 |
pos = 0; |
|
453 |
end = Integer.MAX_VALUE; |
|
454 |
} |
|
455 |
||
11055 | 456 |
Info(JCTree tree, EndPosTable endPosTable) { |
4870 | 457 |
this.tree = tree; |
458 |
tag = tree.getTag(); |
|
459 |
start = TreeInfo.getStartPos(tree); |
|
460 |
pos = tree.pos; |
|
461 |
end = TreeInfo.getEndPos(tree, endPosTable); |
|
462 |
} |
|
463 |
||
464 |
@Override |
|
465 |
public String toString() { |
|
10950 | 466 |
return getTagName(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]"; |
4870 | 467 |
} |
468 |
||
469 |
final JCTree tree; |
|
10950 | 470 |
final JCTree.Tag tag; |
4870 | 471 |
final int start; |
472 |
final int pos; |
|
473 |
final int end; |
|
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* Thrown when errors are found parsing a java file. |
|
478 |
*/ |
|
479 |
private static class ParseException extends Exception { |
|
480 |
ParseException(String msg) { |
|
481 |
super(msg); |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
/** |
|
486 |
* DiagnosticListener to report diagnostics and count any errors that occur. |
|
487 |
*/ |
|
488 |
private static class Reporter implements DiagnosticListener<JavaFileObject> { |
|
489 |
Reporter(PrintWriter out) { |
|
490 |
this.out = out; |
|
491 |
} |
|
492 |
||
493 |
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
494 |
out.println(diagnostic); |
|
495 |
switch (diagnostic.getKind()) { |
|
496 |
case ERROR: |
|
497 |
errors++; |
|
498 |
} |
|
499 |
} |
|
500 |
int errors; |
|
501 |
PrintWriter out; |
|
502 |
} |
|
503 |
||
504 |
/** |
|
505 |
* GUI viewer for issues found by TreePosTester. The viewer provides a drop |
|
506 |
* down list for selecting error conditions, a header area providing details |
|
507 |
* about an error, and a text area with the ranges of text highlighted as |
|
508 |
* appropriate. |
|
509 |
*/ |
|
510 |
private class Viewer extends JFrame { |
|
511 |
/** |
|
512 |
* Create a viewer. |
|
513 |
*/ |
|
514 |
Viewer() { |
|
515 |
initGUI(); |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* Add another entry to the list of errors. |
|
520 |
* @param file The file containing the error |
|
521 |
* @param check The condition that was being tested, and which failed |
|
522 |
* @param encl the enclosing tree node |
|
523 |
* @param self the tree node containing the error |
|
524 |
*/ |
|
525 |
void addEntry(JavaFileObject file, String check, Info encl, Info self) { |
|
526 |
Entry e = new Entry(file, check, encl, self); |
|
527 |
DefaultComboBoxModel m = (DefaultComboBoxModel) entries.getModel(); |
|
528 |
m.addElement(e); |
|
529 |
if (m.getSize() == 1) |
|
530 |
entries.setSelectedItem(e); |
|
531 |
} |
|
532 |
||
533 |
/** |
|
534 |
* Initialize the GUI window. |
|
535 |
*/ |
|
536 |
private void initGUI() { |
|
537 |
JPanel head = new JPanel(new GridBagLayout()); |
|
538 |
GridBagConstraints lc = new GridBagConstraints(); |
|
539 |
GridBagConstraints fc = new GridBagConstraints(); |
|
540 |
fc.anchor = GridBagConstraints.WEST; |
|
541 |
fc.fill = GridBagConstraints.HORIZONTAL; |
|
542 |
fc.gridwidth = GridBagConstraints.REMAINDER; |
|
543 |
||
544 |
entries = new JComboBox(); |
|
545 |
entries.addActionListener(new ActionListener() { |
|
546 |
public void actionPerformed(ActionEvent e) { |
|
547 |
showEntry((Entry) entries.getSelectedItem()); |
|
548 |
} |
|
549 |
}); |
|
550 |
fc.insets.bottom = 10; |
|
551 |
head.add(entries, fc); |
|
552 |
fc.insets.bottom = 0; |
|
553 |
head.add(new JLabel("check:"), lc); |
|
554 |
head.add(checkField = createTextField(80), fc); |
|
555 |
fc.fill = GridBagConstraints.NONE; |
|
556 |
head.add(setBackground(new JLabel("encl:"), enclColor), lc); |
|
557 |
head.add(enclPanel = new InfoPanel(), fc); |
|
558 |
head.add(setBackground(new JLabel("self:"), selfColor), lc); |
|
559 |
head.add(selfPanel = new InfoPanel(), fc); |
|
560 |
add(head, BorderLayout.NORTH); |
|
561 |
||
562 |
body = new JTextArea(); |
|
563 |
body.setFont(Font.decode(Font.MONOSPACED)); |
|
564 |
body.addCaretListener(new CaretListener() { |
|
565 |
public void caretUpdate(CaretEvent e) { |
|
566 |
int dot = e.getDot(); |
|
567 |
int mark = e.getMark(); |
|
568 |
if (dot == mark) |
|
569 |
statusText.setText("dot: " + dot); |
|
570 |
else |
|
571 |
statusText.setText("dot: " + dot + ", mark:" + mark); |
|
572 |
} |
|
573 |
}); |
|
574 |
JScrollPane p = new JScrollPane(body, |
|
575 |
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
|
576 |
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
|
577 |
p.setPreferredSize(new Dimension(640, 480)); |
|
578 |
add(p, BorderLayout.CENTER); |
|
579 |
||
580 |
statusText = createTextField(80); |
|
581 |
add(statusText, BorderLayout.SOUTH); |
|
582 |
||
583 |
pack(); |
|
584 |
setLocationRelativeTo(null); // centered on screen |
|
585 |
setVisible(true); |
|
586 |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
587 |
} |
|
588 |
||
589 |
/** Show an entry that has been selected. */ |
|
590 |
private void showEntry(Entry e) { |
|
591 |
try { |
|
592 |
// update simple fields |
|
593 |
setTitle(e.file.getName()); |
|
594 |
checkField.setText(e.check); |
|
595 |
enclPanel.setInfo(e.encl); |
|
596 |
selfPanel.setInfo(e.self); |
|
597 |
// show file text with highlights |
|
598 |
body.setText(e.file.getCharContent(true).toString()); |
|
599 |
Highlighter highlighter = body.getHighlighter(); |
|
600 |
highlighter.removeAllHighlights(); |
|
601 |
addHighlight(highlighter, e.encl, enclColor); |
|
602 |
addHighlight(highlighter, e.self, selfColor); |
|
603 |
scroll(body, getMinPos(enclPanel.info, selfPanel.info)); |
|
604 |
} catch (IOException ex) { |
|
605 |
body.setText("Cannot read " + e.file.getName() + ": " + e); |
|
606 |
} |
|
607 |
} |
|
608 |
||
609 |
/** Create a test field. */ |
|
610 |
private JTextField createTextField(int width) { |
|
611 |
JTextField f = new JTextField(width); |
|
612 |
f.setEditable(false); |
|
613 |
f.setBorder(null); |
|
614 |
return f; |
|
615 |
} |
|
616 |
||
617 |
/** Add a highlighted region based on the positions in an Info object. */ |
|
618 |
private void addHighlight(Highlighter h, Info info, Color c) { |
|
619 |
int start = info.start; |
|
620 |
int end = info.end; |
|
621 |
if (start == -1 && end == -1) |
|
622 |
return; |
|
623 |
if (start == -1) |
|
624 |
start = end; |
|
625 |
if (end == -1) |
|
626 |
end = start; |
|
627 |
try { |
|
628 |
h.addHighlight(info.start, info.end, |
|
629 |
new DefaultHighlighter.DefaultHighlightPainter(c)); |
|
630 |
if (info.pos != -1) { |
|
631 |
Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40% |
|
632 |
h.addHighlight(info.pos, info.pos + 1, |
|
633 |
new DefaultHighlighter.DefaultHighlightPainter(c2)); |
|
634 |
} |
|
635 |
} catch (BadLocationException e) { |
|
636 |
e.printStackTrace(); |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
/** Get the minimum valid position in a set of info objects. */ |
|
641 |
private int getMinPos(Info... values) { |
|
642 |
int i = Integer.MAX_VALUE; |
|
643 |
for (Info info: values) { |
|
644 |
if (info.start >= 0) i = Math.min(i, info.start); |
|
645 |
if (info.pos >= 0) i = Math.min(i, info.pos); |
|
646 |
if (info.end >= 0) i = Math.min(i, info.end); |
|
647 |
} |
|
648 |
return (i == Integer.MAX_VALUE) ? 0 : i; |
|
649 |
} |
|
650 |
||
651 |
/** Set the background on a component. */ |
|
652 |
private JComponent setBackground(JComponent comp, Color c) { |
|
653 |
comp.setOpaque(true); |
|
654 |
comp.setBackground(c); |
|
655 |
return comp; |
|
656 |
} |
|
657 |
||
658 |
/** Scroll a text area to display a given position near the middle of the visible area. */ |
|
659 |
private void scroll(final JTextArea t, final int pos) { |
|
660 |
// Using invokeLater appears to give text a chance to sort itself out |
|
661 |
// before the scroll happens; otherwise scrollRectToVisible doesn't work. |
|
662 |
// Maybe there's a better way to sync with the text... |
|
663 |
EventQueue.invokeLater(new Runnable() { |
|
664 |
public void run() { |
|
665 |
try { |
|
666 |
Rectangle r = t.modelToView(pos); |
|
667 |
JScrollPane p = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, t); |
|
668 |
r.y = Math.max(0, r.y - p.getHeight() * 2 / 5); |
|
669 |
r.height += p.getHeight() * 4 / 5; |
|
670 |
t.scrollRectToVisible(r); |
|
671 |
} catch (BadLocationException ignore) { |
|
672 |
} |
|
673 |
} |
|
674 |
}); |
|
675 |
} |
|
676 |
||
677 |
private JComboBox entries; |
|
678 |
private JTextField checkField; |
|
679 |
private InfoPanel enclPanel; |
|
680 |
private InfoPanel selfPanel; |
|
681 |
private JTextArea body; |
|
682 |
private JTextField statusText; |
|
683 |
||
684 |
private Color selfColor = new Color(0.f, 1.f, 0.f, 0.2f); // 20% green |
|
685 |
private Color enclColor = new Color(1.f, 0.f, 0.f, 0.2f); // 20% red |
|
686 |
||
687 |
/** Panel to display an Info object. */ |
|
688 |
private class InfoPanel extends JPanel { |
|
689 |
InfoPanel() { |
|
690 |
add(tagName = createTextField(20)); |
|
691 |
add(new JLabel("start:")); |
|
692 |
add(addListener(start = createTextField(6))); |
|
693 |
add(new JLabel("pos:")); |
|
694 |
add(addListener(pos = createTextField(6))); |
|
695 |
add(new JLabel("end:")); |
|
696 |
add(addListener(end = createTextField(6))); |
|
697 |
} |
|
698 |
||
699 |
void setInfo(Info info) { |
|
700 |
this.info = info; |
|
10950 | 701 |
tagName.setText(getTagName(info.tag)); |
4870 | 702 |
start.setText(String.valueOf(info.start)); |
703 |
pos.setText(String.valueOf(info.pos)); |
|
704 |
end.setText(String.valueOf(info.end)); |
|
705 |
} |
|
706 |
||
707 |
JTextField addListener(final JTextField f) { |
|
708 |
f.addMouseListener(new MouseAdapter() { |
|
709 |
@Override |
|
710 |
public void mouseClicked(MouseEvent e) { |
|
711 |
body.setCaretPosition(Integer.valueOf(f.getText())); |
|
712 |
body.getCaret().setVisible(true); |
|
713 |
} |
|
714 |
}); |
|
715 |
return f; |
|
716 |
} |
|
717 |
||
718 |
Info info; |
|
719 |
JTextField tagName; |
|
720 |
JTextField start; |
|
721 |
JTextField pos; |
|
722 |
JTextField end; |
|
723 |
} |
|
724 |
||
725 |
/** Object to record information about an error to be displayed. */ |
|
726 |
private class Entry { |
|
727 |
Entry(JavaFileObject file, String check, Info encl, Info self) { |
|
728 |
this.file = file; |
|
729 |
this.check = check; |
|
730 |
this.encl = encl; |
|
731 |
this.self= self; |
|
732 |
} |
|
733 |
||
734 |
@Override |
|
735 |
public String toString() { |
|
736 |
return file.getName() + " " + check + " " + getMinPos(encl, self); |
|
737 |
} |
|
738 |
||
739 |
final JavaFileObject file; |
|
740 |
final String check; |
|
741 |
final Info encl; |
|
742 |
final Info self; |
|
743 |
} |
|
744 |
} |
|
745 |
} |
|
746 |