mcimadamore@676: /* jjg@1127: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. mcimadamore@676: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@676: * mcimadamore@676: * This code is free software; you can redistribute it and/or modify it mcimadamore@676: * under the terms of the GNU General Public License version 2 only, as mcimadamore@676: * published by the Free Software Foundation. mcimadamore@676: * mcimadamore@676: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@676: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@676: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@676: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@676: * accompanied this code). mcimadamore@676: * mcimadamore@676: * You should have received a copy of the GNU General Public License version mcimadamore@676: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@676: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@676: * mcimadamore@676: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@676: * or visit www.oracle.com if you need additional information or have any mcimadamore@676: * questions. mcimadamore@676: */ mcimadamore@676: mcimadamore@676: import com.sun.source.util.TaskEvent; mcimadamore@676: import java.awt.BorderLayout; mcimadamore@676: import java.awt.Color; mcimadamore@676: import java.awt.Dimension; mcimadamore@676: import java.awt.EventQueue; mcimadamore@676: import java.awt.Font; mcimadamore@676: import java.awt.GridBagConstraints; mcimadamore@676: import java.awt.GridBagLayout; mcimadamore@676: import java.awt.Rectangle; mcimadamore@676: import java.awt.event.ActionEvent; mcimadamore@676: import java.awt.event.ActionListener; mcimadamore@676: import java.awt.event.MouseAdapter; mcimadamore@676: import java.awt.event.MouseEvent; mcimadamore@676: import javax.swing.DefaultComboBoxModel; mcimadamore@676: import javax.swing.JComboBox; mcimadamore@676: import javax.swing.JComponent; mcimadamore@676: import javax.swing.JFrame; mcimadamore@676: import javax.swing.JLabel; mcimadamore@676: import javax.swing.JPanel; mcimadamore@676: import javax.swing.JScrollPane; mcimadamore@676: import javax.swing.JTextArea; mcimadamore@676: import javax.swing.JTextField; mcimadamore@676: import javax.swing.SwingUtilities; mcimadamore@676: import javax.swing.event.CaretEvent; mcimadamore@676: import javax.swing.event.CaretListener; mcimadamore@676: import javax.swing.text.BadLocationException; mcimadamore@676: import javax.swing.text.DefaultHighlighter; mcimadamore@676: import javax.swing.text.Highlighter; mcimadamore@676: import java.io.File; mcimadamore@676: import java.io.IOException; mcimadamore@676: import java.io.PrintStream; mcimadamore@676: import java.io.PrintWriter; mcimadamore@676: import java.io.StringWriter; mcimadamore@676: import java.lang.reflect.Field; mcimadamore@676: import java.util.ArrayList; mcimadamore@676: import java.util.List; mcimadamore@676: import javax.tools.Diagnostic; mcimadamore@676: import javax.tools.DiagnosticListener; mcimadamore@676: import javax.tools.JavaFileObject; mcimadamore@676: import javax.tools.StandardJavaFileManager; mcimadamore@676: mcimadamore@676: import com.sun.source.tree.CompilationUnitTree; mcimadamore@676: import com.sun.source.util.JavacTask; mcimadamore@676: import com.sun.source.util.TaskListener; mcimadamore@676: import com.sun.tools.javac.api.JavacTool; mcimadamore@676: import com.sun.tools.javac.code.Symbol; mcimadamore@676: import com.sun.tools.javac.code.Type; ksrini@1138: import com.sun.tools.javac.parser.EndPosTable; mcimadamore@676: import com.sun.tools.javac.tree.JCTree; mcimadamore@676: import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; mcimadamore@676: import com.sun.tools.javac.tree.JCTree.JCImport; mcimadamore@676: import com.sun.tools.javac.tree.TreeInfo; mcimadamore@676: import com.sun.tools.javac.tree.TreeScanner; mcimadamore@676: import com.sun.tools.javac.util.Pair; mcimadamore@676: mcimadamore@676: import java.util.Arrays; mcimadamore@676: import java.util.HashSet; mcimadamore@676: import java.util.Set; mcimadamore@676: import javax.lang.model.element.Element; mcimadamore@676: jjg@1127: import static com.sun.tools.javac.tree.JCTree.Tag.*; jjg@1127: mcimadamore@676: /** mcimadamore@676: * Utility and test program to check validity of tree positions for tree nodes. mcimadamore@676: * The program can be run standalone, or as a jtreg test. In standalone mode, mcimadamore@676: * errors can be displayed in a gui viewer. For info on command line args, mcimadamore@676: * run program with no args. mcimadamore@676: * mcimadamore@676: *

mcimadamore@676: * jtreg: Note that by using the -r switch in the test description below, this test mcimadamore@676: * will process all java files in the langtools/test directory, thus implicitly mcimadamore@676: * covering any new language features that may be tested in this test suite. mcimadamore@676: */ mcimadamore@676: mcimadamore@676: /* mcimadamore@676: * @test mcimadamore@676: * @bug 6970584 mcimadamore@676: * @summary assorted position errors in compiler syntax trees mcimadamore@676: * @run main CheckAttributedTree -q -r -et ERRONEOUS . mcimadamore@676: */ mcimadamore@676: public class CheckAttributedTree { mcimadamore@676: /** mcimadamore@676: * Main entry point. mcimadamore@676: * If test.src is set, program runs in jtreg mode, and will throw an Error mcimadamore@676: * if any errors arise, otherwise System.exit will be used, unless the gui mcimadamore@676: * viewer is being used. In jtreg mode, the default base directory for file mcimadamore@676: * args is the value of ${test.src}. In jtreg mode, the -r option can be mcimadamore@676: * given to change the default base directory to the root test directory. mcimadamore@676: */ mcimadamore@676: public static void main(String... args) { mcimadamore@676: String testSrc = System.getProperty("test.src"); mcimadamore@676: File baseDir = (testSrc == null) ? null : new File(testSrc); mcimadamore@676: boolean ok = new CheckAttributedTree().run(baseDir, args); mcimadamore@676: if (!ok) { mcimadamore@676: if (testSrc != null) // jtreg mode mcimadamore@676: throw new Error("failed"); mcimadamore@676: else mcimadamore@676: System.exit(1); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Run the program. A base directory can be provided for file arguments. mcimadamore@676: * In jtreg mode, the -r option can be given to change the default base mcimadamore@676: * directory to the test root directory. For other options, see usage(). mcimadamore@676: * @param baseDir base directory for any file arguments. mcimadamore@676: * @param args command line args mcimadamore@676: * @return true if successful or in gui mode mcimadamore@676: */ mcimadamore@676: boolean run(File baseDir, String... args) { mcimadamore@676: if (args.length == 0) { mcimadamore@676: usage(System.out); mcimadamore@676: return true; mcimadamore@676: } mcimadamore@676: mcimadamore@676: List files = new ArrayList(); mcimadamore@676: for (int i = 0; i < args.length; i++) { mcimadamore@676: String arg = args[i]; mcimadamore@676: if (arg.equals("-encoding") && i + 1 < args.length) mcimadamore@676: encoding = args[++i]; mcimadamore@676: else if (arg.equals("-gui")) mcimadamore@676: gui = true; mcimadamore@676: else if (arg.equals("-q")) mcimadamore@676: quiet = true; mcimadamore@676: else if (arg.equals("-v")) mcimadamore@676: verbose = true; mcimadamore@676: else if (arg.equals("-t") && i + 1 < args.length) mcimadamore@676: tags.add(args[++i]); mcimadamore@676: else if (arg.equals("-ef") && i + 1 < args.length) mcimadamore@676: excludeFiles.add(new File(baseDir, args[++i])); mcimadamore@676: else if (arg.equals("-et") && i + 1 < args.length) mcimadamore@676: excludeTags.add(args[++i]); mcimadamore@676: else if (arg.equals("-r")) { mcimadamore@676: if (excludeFiles.size() > 0) mcimadamore@676: throw new Error("-r must be used before -ef"); mcimadamore@676: File d = baseDir; mcimadamore@676: while (!new File(d, "TEST.ROOT").exists()) { mcimadamore@676: if (d == null) mcimadamore@676: throw new Error("cannot find TEST.ROOT"); mcimadamore@676: d = d.getParentFile(); mcimadamore@676: } mcimadamore@676: baseDir = d; mcimadamore@676: } mcimadamore@676: else if (arg.startsWith("-")) mcimadamore@676: throw new Error("unknown option: " + arg); mcimadamore@676: else { mcimadamore@676: while (i < args.length) mcimadamore@676: files.add(new File(baseDir, args[i++])); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: for (File file: files) { mcimadamore@676: if (file.exists()) mcimadamore@676: test(file); mcimadamore@676: else mcimadamore@676: error("File not found: " + file); mcimadamore@676: } mcimadamore@676: mcimadamore@676: if (fileCount != 1) mcimadamore@676: System.err.println(fileCount + " files read"); mcimadamore@676: if (errors > 0) mcimadamore@676: System.err.println(errors + " errors"); mcimadamore@676: mcimadamore@676: return (gui || errors == 0); mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Print command line help. mcimadamore@676: * @param out output stream mcimadamore@676: */ mcimadamore@676: void usage(PrintStream out) { mcimadamore@676: out.println("Usage:"); mcimadamore@676: out.println(" java CheckAttributedTree options... files..."); mcimadamore@676: out.println(""); mcimadamore@676: out.println("where options include:"); mcimadamore@676: out.println("-q Quiet: don't report on inapplicable files"); mcimadamore@676: out.println("-gui Display returns in a GUI viewer"); mcimadamore@676: out.println("-v Verbose: report on files as they are being read"); mcimadamore@676: out.println("-t tag Limit checks to tree nodes with this tag"); mcimadamore@676: out.println(" Can be repeated if desired"); mcimadamore@676: out.println("-ef file Exclude file or directory"); mcimadamore@676: out.println("-et tag Exclude tree nodes with given tag name"); mcimadamore@676: out.println(""); mcimadamore@676: out.println("files may be directories or files"); mcimadamore@676: out.println("directories will be scanned recursively"); mcimadamore@676: out.println("non java files, or java files which cannot be parsed, will be ignored"); mcimadamore@676: out.println(""); mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Test a file. If the file is a directory, it will be recursively scanned mcimadamore@676: * for java files. mcimadamore@676: * @param file the file or directory to test mcimadamore@676: */ mcimadamore@676: void test(File file) { mcimadamore@676: if (excludeFiles.contains(file)) { mcimadamore@676: if (!quiet) mcimadamore@676: error("File " + file + " excluded"); mcimadamore@676: return; mcimadamore@676: } mcimadamore@676: mcimadamore@676: if (file.isDirectory()) { mcimadamore@676: for (File f: file.listFiles()) { mcimadamore@676: test(f); mcimadamore@676: } mcimadamore@676: return; mcimadamore@676: } mcimadamore@676: mcimadamore@676: if (file.isFile() && file.getName().endsWith(".java")) { mcimadamore@676: try { mcimadamore@676: if (verbose) mcimadamore@676: System.err.println(file); mcimadamore@676: fileCount++; mcimadamore@676: NPETester p = new NPETester(); mcimadamore@676: p.test(read(file)); mcimadamore@676: } catch (AttributionException e) { mcimadamore@676: if (!quiet) { mcimadamore@676: error("Error attributing " + file + "\n" + e.getMessage()); mcimadamore@676: } mcimadamore@676: } catch (IOException e) { mcimadamore@676: error("Error reading " + file + ": " + e); mcimadamore@676: } mcimadamore@676: return; mcimadamore@676: } mcimadamore@676: mcimadamore@676: if (!quiet) mcimadamore@676: error("File " + file + " ignored"); mcimadamore@676: } mcimadamore@676: jjh@808: // See CR: 6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout jjh@808: StringWriter sw = new StringWriter(); jjh@808: PrintWriter pw = new PrintWriter(sw); jjh@808: Reporter r = new Reporter(pw); jjh@808: JavacTool tool = JavacTool.create(); jjh@808: StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null); jjh@808: mcimadamore@676: /** mcimadamore@676: * Read a file. mcimadamore@676: * @param file the file to be read mcimadamore@676: * @return the tree for the content of the file mcimadamore@676: * @throws IOException if any IO errors occur mcimadamore@676: * @throws TreePosTest.ParseException if any errors occur while parsing the file mcimadamore@676: */ mcimadamore@676: List> read(File file) throws IOException, AttributionException { mcimadamore@676: JavacTool tool = JavacTool.create(); jjh@808: r.errors = 0; mcimadamore@676: Iterable files = fm.getJavaFileObjects(file); mcimadamore@676: String[] opts = { "-XDshouldStopPolicy=ATTR", "-XDverboseCompilePolicy" }; mcimadamore@676: JavacTask task = tool.getTask(pw, fm, r, Arrays.asList(opts), null, files); mcimadamore@676: final List analyzedElems = new ArrayList<>(); mcimadamore@676: task.setTaskListener(new TaskListener() { mcimadamore@676: public void started(TaskEvent e) { mcimadamore@676: if (e.getKind() == TaskEvent.Kind.ANALYZE) mcimadamore@676: analyzedElems.add(e.getTypeElement()); mcimadamore@676: } mcimadamore@676: public void finished(TaskEvent e) { } mcimadamore@676: }); mcimadamore@676: mcimadamore@676: try { mcimadamore@676: Iterable trees = task.parse(); mcimadamore@676: task.analyze(); mcimadamore@676: List> res = new ArrayList<>(); jjg@683: //System.out.println("Try to add pairs. Elems are " + analyzedElems); mcimadamore@676: for (CompilationUnitTree t : trees) { mcimadamore@676: JCCompilationUnit cu = (JCCompilationUnit)t; mcimadamore@676: for (JCTree def : cu.defs) { jjg@1127: if (def.hasTag(CLASSDEF) && mcimadamore@676: analyzedElems.contains(((JCTree.JCClassDecl)def).sym)) { jjg@683: //System.out.println("Adding pair..."); mcimadamore@676: res.add(new Pair<>(cu, def)); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: } mcimadamore@676: return res; mcimadamore@676: } mcimadamore@676: catch (Throwable t) { mcimadamore@676: throw new AttributionException("Exception while attributing file: " + file); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Report an error. When the program is complete, the program will either mcimadamore@676: * exit or throw an Error if any errors have been reported. mcimadamore@676: * @param msg the error message mcimadamore@676: */ mcimadamore@676: void error(String msg) { mcimadamore@676: System.err.println(msg); mcimadamore@676: errors++; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Number of files that have been analyzed. */ mcimadamore@676: int fileCount; mcimadamore@676: /** Number of errors reported. */ mcimadamore@676: int errors; mcimadamore@676: /** Flag: don't report irrelevant files. */ mcimadamore@676: boolean quiet; mcimadamore@676: /** Flag: show errors in GUI viewer. */ mcimadamore@676: boolean gui; mcimadamore@676: /** The GUI viewer for errors. */ mcimadamore@676: Viewer viewer; mcimadamore@676: /** Flag: report files as they are processed. */ mcimadamore@676: boolean verbose; mcimadamore@676: /** Option: encoding for test files. */ mcimadamore@676: String encoding; mcimadamore@676: /** The set of tags for tree nodes to be analyzed; if empty, all tree nodes mcimadamore@676: * are analyzed. */ mcimadamore@676: Set tags = new HashSet(); mcimadamore@676: /** Set of files and directories to be excluded from analysis. */ mcimadamore@676: Set excludeFiles = new HashSet(); mcimadamore@676: /** Set of tag names to be excluded from analysis. */ mcimadamore@676: Set excludeTags = new HashSet(); mcimadamore@676: /** Utility class for trees */ mcimadamore@676: TreeUtil treeUtil = new TreeUtil(); mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Main class for testing assertions concerning types/symbol mcimadamore@676: * left uninitialized after attribution mcimadamore@676: */ mcimadamore@676: private class NPETester extends TreeScanner { mcimadamore@676: void test(List> trees) { mcimadamore@676: for (Pair p : trees) { mcimadamore@676: sourcefile = p.fst.sourcefile; mcimadamore@676: endPosTable = p.fst.endPositions; mcimadamore@676: encl = new Info(p.snd, endPosTable); mcimadamore@676: p.snd.accept(this); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: @Override mcimadamore@676: public void scan(JCTree tree) { mcimadamore@676: if (tree == null || mcimadamore@676: excludeTags.contains(treeUtil.nameFromTag(tree.getTag()))) { mcimadamore@676: return; mcimadamore@676: } mcimadamore@676: mcimadamore@676: Info self = new Info(tree, endPosTable); mcimadamore@676: check(!mandatoryType(tree) || mcimadamore@676: (tree.type != null && mcimadamore@676: checkFields(tree)), mcimadamore@676: "'null' found in tree ", mcimadamore@676: self); mcimadamore@676: mcimadamore@676: Info prevEncl = encl; mcimadamore@676: encl = self; mcimadamore@676: tree.accept(this); mcimadamore@676: encl = prevEncl; mcimadamore@676: } mcimadamore@676: mcimadamore@676: private boolean mandatoryType(JCTree that) { mcimadamore@676: return that instanceof JCTree.JCExpression || jjg@1127: that.hasTag(VARDEF) || jjg@1127: that.hasTag(METHODDEF) || jjg@1127: that.hasTag(CLASSDEF); mcimadamore@676: } mcimadamore@676: mcimadamore@676: private final List excludedFields = Arrays.asList("varargsElement"); mcimadamore@676: mcimadamore@676: void check(boolean ok, String label, Info self) { mcimadamore@676: if (!ok) { mcimadamore@676: if (gui) { mcimadamore@676: if (viewer == null) mcimadamore@676: viewer = new Viewer(); mcimadamore@676: viewer.addEntry(sourcefile, label, encl, self); mcimadamore@676: } mcimadamore@676: error(label + self.toString() + " encl: " + encl.toString() + " in file: " + sourcefile + " " + self.tree); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: boolean checkFields(JCTree t) { mcimadamore@676: List fieldsToCheck = treeUtil.getFieldsOfType(t, mcimadamore@676: excludedFields, mcimadamore@676: Symbol.class, mcimadamore@676: Type.class); mcimadamore@676: for (Field f : fieldsToCheck) { mcimadamore@676: try { mcimadamore@676: if (f.get(t) == null) { mcimadamore@676: return false; mcimadamore@676: } mcimadamore@676: } mcimadamore@676: catch (IllegalAccessException e) { mcimadamore@676: System.err.println("Cannot read field: " + f); mcimadamore@676: //swallow it mcimadamore@676: } mcimadamore@676: } mcimadamore@676: return true; mcimadamore@676: } mcimadamore@676: mcimadamore@676: @Override mcimadamore@676: public void visitImport(JCImport tree) { } mcimadamore@676: mcimadamore@676: @Override mcimadamore@676: public void visitTopLevel(JCCompilationUnit tree) { mcimadamore@676: scan(tree.defs); mcimadamore@676: } mcimadamore@676: mcimadamore@676: JavaFileObject sourcefile; ksrini@1138: EndPosTable endPosTable; mcimadamore@676: Info encl; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Utility class providing easy access to position and other info for a tree node. mcimadamore@676: */ mcimadamore@676: private class Info { mcimadamore@676: Info() { mcimadamore@676: tree = null; jjg@1127: tag = ERRONEOUS; mcimadamore@676: start = 0; mcimadamore@676: pos = 0; mcimadamore@676: end = Integer.MAX_VALUE; mcimadamore@676: } mcimadamore@676: ksrini@1138: Info(JCTree tree, EndPosTable endPosTable) { mcimadamore@676: this.tree = tree; mcimadamore@676: tag = tree.getTag(); mcimadamore@676: start = TreeInfo.getStartPos(tree); mcimadamore@676: pos = tree.pos; mcimadamore@676: end = TreeInfo.getEndPos(tree, endPosTable); mcimadamore@676: } mcimadamore@676: mcimadamore@676: @Override mcimadamore@676: public String toString() { mcimadamore@676: return treeUtil.nameFromTag(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]"; mcimadamore@676: } mcimadamore@676: mcimadamore@676: final JCTree tree; jjg@1127: final JCTree.Tag tag; mcimadamore@676: final int start; mcimadamore@676: final int pos; mcimadamore@676: final int end; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Names for tree tags. mcimadamore@676: */ mcimadamore@676: private static class TreeUtil { jjg@1127: String nameFromTag(JCTree.Tag tag) { jjg@1127: String name = tag.name(); mcimadamore@676: return (name == null) ? "??" : name; mcimadamore@676: } mcimadamore@676: mcimadamore@676: List getFieldsOfType(JCTree t, List excludeNames, Class... types) { mcimadamore@676: List buf = new ArrayList(); mcimadamore@676: for (Field f : t.getClass().getDeclaredFields()) { mcimadamore@676: if (!excludeNames.contains(f.getName())) { mcimadamore@676: for (Class type : types) { mcimadamore@676: if (type.isAssignableFrom(f.getType())) { mcimadamore@676: f.setAccessible(true); mcimadamore@676: buf.add(f); mcimadamore@676: break; mcimadamore@676: } mcimadamore@676: } mcimadamore@676: } mcimadamore@676: } mcimadamore@676: return buf; mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Thrown when errors are found parsing a java file. mcimadamore@676: */ mcimadamore@676: private static class ParseException extends Exception { mcimadamore@676: ParseException(String msg) { mcimadamore@676: super(msg); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: private static class AttributionException extends Exception { mcimadamore@676: AttributionException(String msg) { mcimadamore@676: super(msg); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * DiagnosticListener to report diagnostics and count any errors that occur. mcimadamore@676: */ mcimadamore@676: private static class Reporter implements DiagnosticListener { mcimadamore@676: Reporter(PrintWriter out) { mcimadamore@676: this.out = out; mcimadamore@676: } mcimadamore@676: mcimadamore@676: public void report(Diagnostic diagnostic) { mcimadamore@676: out.println(diagnostic); mcimadamore@676: switch (diagnostic.getKind()) { mcimadamore@676: case ERROR: mcimadamore@676: errors++; mcimadamore@676: } mcimadamore@676: } mcimadamore@676: int errors; mcimadamore@676: PrintWriter out; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * GUI viewer for issues found by TreePosTester. The viewer provides a drop mcimadamore@676: * down list for selecting error conditions, a header area providing details mcimadamore@676: * about an error, and a text area with the ranges of text highlighted as mcimadamore@676: * appropriate. mcimadamore@676: */ mcimadamore@676: private class Viewer extends JFrame { mcimadamore@676: /** mcimadamore@676: * Create a viewer. mcimadamore@676: */ mcimadamore@676: Viewer() { mcimadamore@676: initGUI(); mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Add another entry to the list of errors. mcimadamore@676: * @param file The file containing the error mcimadamore@676: * @param check The condition that was being tested, and which failed mcimadamore@676: * @param encl the enclosing tree node mcimadamore@676: * @param self the tree node containing the error mcimadamore@676: */ mcimadamore@676: void addEntry(JavaFileObject file, String check, Info encl, Info self) { mcimadamore@676: Entry e = new Entry(file, check, encl, self); mcimadamore@676: DefaultComboBoxModel m = (DefaultComboBoxModel) entries.getModel(); mcimadamore@676: m.addElement(e); mcimadamore@676: if (m.getSize() == 1) mcimadamore@676: entries.setSelectedItem(e); mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** mcimadamore@676: * Initialize the GUI window. mcimadamore@676: */ mcimadamore@676: private void initGUI() { mcimadamore@676: JPanel head = new JPanel(new GridBagLayout()); mcimadamore@676: GridBagConstraints lc = new GridBagConstraints(); mcimadamore@676: GridBagConstraints fc = new GridBagConstraints(); mcimadamore@676: fc.anchor = GridBagConstraints.WEST; mcimadamore@676: fc.fill = GridBagConstraints.HORIZONTAL; mcimadamore@676: fc.gridwidth = GridBagConstraints.REMAINDER; mcimadamore@676: mcimadamore@676: entries = new JComboBox(); mcimadamore@676: entries.addActionListener(new ActionListener() { mcimadamore@676: public void actionPerformed(ActionEvent e) { mcimadamore@676: showEntry((Entry) entries.getSelectedItem()); mcimadamore@676: } mcimadamore@676: }); mcimadamore@676: fc.insets.bottom = 10; mcimadamore@676: head.add(entries, fc); mcimadamore@676: fc.insets.bottom = 0; mcimadamore@676: head.add(new JLabel("check:"), lc); mcimadamore@676: head.add(checkField = createTextField(80), fc); mcimadamore@676: fc.fill = GridBagConstraints.NONE; mcimadamore@676: head.add(setBackground(new JLabel("encl:"), enclColor), lc); mcimadamore@676: head.add(enclPanel = new InfoPanel(), fc); mcimadamore@676: head.add(setBackground(new JLabel("self:"), selfColor), lc); mcimadamore@676: head.add(selfPanel = new InfoPanel(), fc); mcimadamore@676: add(head, BorderLayout.NORTH); mcimadamore@676: mcimadamore@676: body = new JTextArea(); mcimadamore@676: body.setFont(Font.decode(Font.MONOSPACED)); mcimadamore@676: body.addCaretListener(new CaretListener() { mcimadamore@676: public void caretUpdate(CaretEvent e) { mcimadamore@676: int dot = e.getDot(); mcimadamore@676: int mark = e.getMark(); mcimadamore@676: if (dot == mark) mcimadamore@676: statusText.setText("dot: " + dot); mcimadamore@676: else mcimadamore@676: statusText.setText("dot: " + dot + ", mark:" + mark); mcimadamore@676: } mcimadamore@676: }); mcimadamore@676: JScrollPane p = new JScrollPane(body, mcimadamore@676: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, mcimadamore@676: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); mcimadamore@676: p.setPreferredSize(new Dimension(640, 480)); mcimadamore@676: add(p, BorderLayout.CENTER); mcimadamore@676: mcimadamore@676: statusText = createTextField(80); mcimadamore@676: add(statusText, BorderLayout.SOUTH); mcimadamore@676: mcimadamore@676: pack(); mcimadamore@676: setLocationRelativeTo(null); // centered on screen mcimadamore@676: setVisible(true); mcimadamore@676: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Show an entry that has been selected. */ mcimadamore@676: private void showEntry(Entry e) { mcimadamore@676: try { mcimadamore@676: // update simple fields mcimadamore@676: setTitle(e.file.getName()); mcimadamore@676: checkField.setText(e.check); mcimadamore@676: enclPanel.setInfo(e.encl); mcimadamore@676: selfPanel.setInfo(e.self); mcimadamore@676: // show file text with highlights mcimadamore@676: body.setText(e.file.getCharContent(true).toString()); mcimadamore@676: Highlighter highlighter = body.getHighlighter(); mcimadamore@676: highlighter.removeAllHighlights(); mcimadamore@676: addHighlight(highlighter, e.encl, enclColor); mcimadamore@676: addHighlight(highlighter, e.self, selfColor); mcimadamore@676: scroll(body, getMinPos(enclPanel.info, selfPanel.info)); mcimadamore@676: } catch (IOException ex) { mcimadamore@676: body.setText("Cannot read " + e.file.getName() + ": " + e); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Create a test field. */ mcimadamore@676: private JTextField createTextField(int width) { mcimadamore@676: JTextField f = new JTextField(width); mcimadamore@676: f.setEditable(false); mcimadamore@676: f.setBorder(null); mcimadamore@676: return f; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Add a highlighted region based on the positions in an Info object. */ mcimadamore@676: private void addHighlight(Highlighter h, Info info, Color c) { mcimadamore@676: int start = info.start; mcimadamore@676: int end = info.end; mcimadamore@676: if (start == -1 && end == -1) mcimadamore@676: return; mcimadamore@676: if (start == -1) mcimadamore@676: start = end; mcimadamore@676: if (end == -1) mcimadamore@676: end = start; mcimadamore@676: try { mcimadamore@676: h.addHighlight(info.start, info.end, mcimadamore@676: new DefaultHighlighter.DefaultHighlightPainter(c)); mcimadamore@676: if (info.pos != -1) { mcimadamore@676: Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40% mcimadamore@676: h.addHighlight(info.pos, info.pos + 1, mcimadamore@676: new DefaultHighlighter.DefaultHighlightPainter(c2)); mcimadamore@676: } mcimadamore@676: } catch (BadLocationException e) { mcimadamore@676: e.printStackTrace(); mcimadamore@676: } mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Get the minimum valid position in a set of info objects. */ mcimadamore@676: private int getMinPos(Info... values) { mcimadamore@676: int i = Integer.MAX_VALUE; mcimadamore@676: for (Info info: values) { mcimadamore@676: if (info.start >= 0) i = Math.min(i, info.start); mcimadamore@676: if (info.pos >= 0) i = Math.min(i, info.pos); mcimadamore@676: if (info.end >= 0) i = Math.min(i, info.end); mcimadamore@676: } mcimadamore@676: return (i == Integer.MAX_VALUE) ? 0 : i; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Set the background on a component. */ mcimadamore@676: private JComponent setBackground(JComponent comp, Color c) { mcimadamore@676: comp.setOpaque(true); mcimadamore@676: comp.setBackground(c); mcimadamore@676: return comp; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Scroll a text area to display a given position near the middle of the visible area. */ mcimadamore@676: private void scroll(final JTextArea t, final int pos) { mcimadamore@676: // Using invokeLater appears to give text a chance to sort itself out mcimadamore@676: // before the scroll happens; otherwise scrollRectToVisible doesn't work. mcimadamore@676: // Maybe there's a better way to sync with the text... mcimadamore@676: EventQueue.invokeLater(new Runnable() { mcimadamore@676: public void run() { mcimadamore@676: try { mcimadamore@676: Rectangle r = t.modelToView(pos); mcimadamore@676: JScrollPane p = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, t); mcimadamore@676: r.y = Math.max(0, r.y - p.getHeight() * 2 / 5); mcimadamore@676: r.height += p.getHeight() * 4 / 5; mcimadamore@676: t.scrollRectToVisible(r); mcimadamore@676: } catch (BadLocationException ignore) { mcimadamore@676: } mcimadamore@676: } mcimadamore@676: }); mcimadamore@676: } mcimadamore@676: mcimadamore@676: private JComboBox entries; mcimadamore@676: private JTextField checkField; mcimadamore@676: private InfoPanel enclPanel; mcimadamore@676: private InfoPanel selfPanel; mcimadamore@676: private JTextArea body; mcimadamore@676: private JTextField statusText; mcimadamore@676: mcimadamore@676: private Color selfColor = new Color(0.f, 1.f, 0.f, 0.2f); // 20% green mcimadamore@676: private Color enclColor = new Color(1.f, 0.f, 0.f, 0.2f); // 20% red mcimadamore@676: mcimadamore@676: /** Panel to display an Info object. */ mcimadamore@676: private class InfoPanel extends JPanel { mcimadamore@676: InfoPanel() { mcimadamore@676: add(tagName = createTextField(20)); mcimadamore@676: add(new JLabel("start:")); mcimadamore@676: add(addListener(start = createTextField(6))); mcimadamore@676: add(new JLabel("pos:")); mcimadamore@676: add(addListener(pos = createTextField(6))); mcimadamore@676: add(new JLabel("end:")); mcimadamore@676: add(addListener(end = createTextField(6))); mcimadamore@676: } mcimadamore@676: mcimadamore@676: void setInfo(Info info) { mcimadamore@676: this.info = info; mcimadamore@676: tagName.setText(treeUtil.nameFromTag(info.tag)); mcimadamore@676: start.setText(String.valueOf(info.start)); mcimadamore@676: pos.setText(String.valueOf(info.pos)); mcimadamore@676: end.setText(String.valueOf(info.end)); mcimadamore@676: } mcimadamore@676: mcimadamore@676: JTextField addListener(final JTextField f) { mcimadamore@676: f.addMouseListener(new MouseAdapter() { mcimadamore@676: @Override mcimadamore@676: public void mouseClicked(MouseEvent e) { mcimadamore@676: body.setCaretPosition(Integer.valueOf(f.getText())); mcimadamore@676: body.getCaret().setVisible(true); mcimadamore@676: } mcimadamore@676: }); mcimadamore@676: return f; mcimadamore@676: } mcimadamore@676: mcimadamore@676: Info info; mcimadamore@676: JTextField tagName; mcimadamore@676: JTextField start; mcimadamore@676: JTextField pos; mcimadamore@676: JTextField end; mcimadamore@676: } mcimadamore@676: mcimadamore@676: /** Object to record information about an error to be displayed. */ mcimadamore@676: private class Entry { mcimadamore@676: Entry(JavaFileObject file, String check, Info encl, Info self) { mcimadamore@676: this.file = file; mcimadamore@676: this.check = check; mcimadamore@676: this.encl = encl; mcimadamore@676: this.self= self; mcimadamore@676: } mcimadamore@676: mcimadamore@676: @Override mcimadamore@676: public String toString() { mcimadamore@676: return file.getName() + " " + check + " " + getMinPos(encl, self); mcimadamore@676: } mcimadamore@676: mcimadamore@676: final JavaFileObject file; mcimadamore@676: final String check; mcimadamore@676: final Info encl; mcimadamore@676: final Info self; mcimadamore@676: } mcimadamore@676: } mcimadamore@676: }