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