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