langtools/test/tools/javac/tree/TreePosTest.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 24895 dd091d389fbf
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) 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
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
4870
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5013
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5013
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5013
diff changeset
    21
 * questions.
4870
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;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11055
diff changeset
    76
import com.sun.tools.javac.tree.EndPosTable;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    77
import com.sun.tools.javac.tree.JCTree;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
    78
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    79
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    80
import com.sun.tools.javac.tree.JCTree.JCNewClass;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    81
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    82
import com.sun.tools.javac.tree.TreeInfo;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    83
import com.sun.tools.javac.tree.TreeScanner;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    84
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
    85
import static com.sun.tools.javac.tree.JCTree.Tag.*;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    86
import static com.sun.tools.javac.util.Position.NOPOS;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    87
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    88
/**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    89
 * 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
    90
 * 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
    91
 * 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
    92
 * run program with no args.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    93
 *
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    94
 * <p>
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    95
 * 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
    96
 * 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
    97
 * 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
    98
 */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    99
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   100
/*
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   101
 * @test
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   102
 * @bug 6919889
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   103
 * @summary assorted position errors in compiler syntax trees
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   104
 * OLD: -q -r -ef ./tools/javac/typeAnnotations -ef ./tools/javap/typeAnnotations -et ANNOTATED_TYPE .
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   105
 * @run main TreePosTest -q -r .
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   106
 */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   107
public class TreePosTest {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   108
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   109
     * Main entry point.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   110
     * 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
   111
     * 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
   112
     * 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
   113
     * 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
   114
     * 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
   115
     */
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   116
    public static void main(String... args) throws IOException {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   117
        String testSrc = System.getProperty("test.src");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   118
        File baseDir = (testSrc == null) ? null : new File(testSrc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   119
        boolean ok = new TreePosTest().run(baseDir, args);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   120
        if (!ok) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   121
            if (testSrc != null)  // jtreg mode
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   122
                throw new Error("failed");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   123
            else
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   124
                System.exit(1);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   125
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   126
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   127
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   128
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   129
     * 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
   130
     * 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
   131
     * directory to the test root directory. For other options, see usage().
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   132
     * @param baseDir base directory for any file arguments.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   133
     * @param args command line args
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   134
     * @return true if successful or in gui mode
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   135
     */
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   136
    boolean run(File baseDir, String... args) throws IOException {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   137
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   138
            if (args.length == 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   139
                usage(System.out);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   140
                return true;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   141
            }
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   142
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   143
            List<File> files = new ArrayList<File>();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   144
            for (int i = 0; i < args.length; i++) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   145
                String arg = args[i];
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   146
                if (arg.equals("-encoding") && i + 1 < args.length)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   147
                    encoding = args[++i];
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   148
                else if (arg.equals("-gui"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   149
                    gui = true;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   150
                else if (arg.equals("-q"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   151
                    quiet = true;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   152
                else if (arg.equals("-v"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   153
                    verbose = true;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   154
                else if (arg.equals("-t") && i + 1 < args.length)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   155
                    tags.add(args[++i]);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   156
                else if (arg.equals("-ef") && i + 1 < args.length)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   157
                    excludeFiles.add(new File(baseDir, args[++i]));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   158
                else if (arg.equals("-et") && i + 1 < args.length)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   159
                    excludeTags.add(args[++i]);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   160
                else if (arg.equals("-r")) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   161
                    if (excludeFiles.size() > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   162
                        throw new Error("-r must be used before -ef");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   163
                    File d = baseDir;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   164
                    while (!new File(d, "TEST.ROOT").exists()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   165
                        d = d.getParentFile();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   166
                        if (d == null)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   167
                            throw new Error("cannot find TEST.ROOT");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   168
                    }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   169
                    baseDir = d;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   170
                }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   171
                else if (arg.startsWith("-"))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   172
                    throw new Error("unknown option: " + arg);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   173
                else {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   174
                    while (i < args.length)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   175
                        files.add(new File(baseDir, args[i++]));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   176
                }
4870
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
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   179
            for (File file: files) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   180
                if (file.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   181
                    test(file);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   182
                else
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   183
                    error("File not found: " + file);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   184
            }
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   185
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   186
            if (fileCount != 1)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   187
                System.err.println(fileCount + " files read");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   188
            if (errors > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   189
                System.err.println(errors + " errors");
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   190
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   191
            return (gui || errors == 0);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   192
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   193
            fm.close();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 24895
diff changeset
   194
        }
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   195
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   196
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   197
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   198
     * Print command line help.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   199
     * @param out output stream
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   200
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   201
    void usage(PrintStream out) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   202
        out.println("Usage:");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   203
        out.println("  java TreePosTest options... files...");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   204
        out.println("");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   205
        out.println("where options include:");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   206
        out.println("-gui      Display returns in a GUI viewer");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   207
        out.println("-q        Quiet: don't report on inapplicable files");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   208
        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
   209
        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
   210
        out.println("          Can be repeated if desired");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   211
        out.println("-ef file  Exclude file or directory");
4933
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   212
        out.println("-et tag   Exclude tree nodes with given tag name");
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   213
        out.println("");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   214
        out.println("files may be directories or files");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   215
        out.println("directories will be scanned recursively");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   216
        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
   217
        out.println("");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   218
    }
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
     * 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
   222
     * for java files.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   223
     * @param file the file or directory to test
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
    void test(File file) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   226
        if (excludeFiles.contains(file)) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   227
            if (!quiet)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   228
                error("File " + file + " excluded");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   229
            return;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   230
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   231
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   232
        if (file.isDirectory()) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   233
            for (File f: file.listFiles()) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   234
                test(f);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   235
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   236
            return;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   237
        }
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
        if (file.isFile() && file.getName().endsWith(".java")) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   240
            try {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   241
                if (verbose)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   242
                    System.err.println(file);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   243
                fileCount++;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   244
                PosTester p = new PosTester();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   245
                p.test(read(file));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   246
            } catch (ParseException e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   247
                if (!quiet) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   248
                    error("Error parsing " + file + "\n" + e.getMessage());
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
            } catch (IOException e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   251
                error("Error reading " + file + ": " + e);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   252
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   253
            return;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   254
        }
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
        if (!quiet)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   257
            error("File " + file + " ignored");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   258
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   259
7846
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   260
    // See CR:  6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   261
    StringWriter sw = new StringWriter();
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   262
    PrintWriter pw = new PrintWriter(sw);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   263
    Reporter r = new Reporter(pw);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   264
    JavacTool tool = JavacTool.create();
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   265
    StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   266
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   267
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   268
     * Read a file.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   269
     * @param file the file to be read
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   270
     * @return the tree for the content of the file
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   271
     * @throws IOException if any IO errors occur
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   272
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   273
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   274
    JCCompilationUnit read(File file) throws IOException, ParseException {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   275
        JavacTool tool = JavacTool.create();
7846
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 7072
diff changeset
   276
        r.errors = 0;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   277
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   278
        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
   279
        Iterable<? extends CompilationUnitTree> trees = task.parse();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   280
        pw.flush();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   281
        if (r.errors > 0)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   282
            throw new ParseException(sw.toString());
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   283
        Iterator<? extends CompilationUnitTree> iter = trees.iterator();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   284
        if (!iter.hasNext())
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   285
            throw new Error("no trees found");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   286
        JCCompilationUnit t = (JCCompilationUnit) iter.next();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   287
        if (iter.hasNext())
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   288
            throw new Error("too many trees found");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   289
        return t;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   290
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   291
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   292
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   293
     * 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
   294
     * exit or throw an Error if any errors have been reported.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   295
     * @param msg the error message
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   296
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   297
    void error(String msg) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   298
        System.err.println(msg);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   299
        errors++;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   300
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   301
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   302
    /**
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   303
     * Names for tree tags.
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   304
     */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   305
    private static String getTagName(JCTree.Tag tag) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   306
        String name = tag.name();
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   307
        return (name == null) ? "??" : name;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   308
    }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   309
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   310
    /** Number of files that have been analyzed. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   311
    int fileCount;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   312
    /** Number of errors reported. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   313
    int errors;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   314
    /** Flag: don't report irrelevant files. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   315
    boolean quiet;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   316
    /** Flag: report files as they are processed. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   317
    boolean verbose;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   318
    /** Flag: show errors in GUI viewer. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   319
    boolean gui;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   320
    /** Option: encoding for test files. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   321
    String encoding;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   322
    /** The GUI viewer for errors. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   323
    Viewer viewer;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   324
    /** 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
   325
     * are analyzed. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   326
    Set<String> tags = new HashSet<String>();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   327
    /** Set of files and directories to be excluded from analysis. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   328
    Set<File> excludeFiles = new HashSet<File>();
4933
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   329
    /** Set of tag names to be excluded from analysis. */
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   330
    Set<String> excludeTags = new HashSet<String>();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   331
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   332
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   333
     * Main class for testing assertions concerning tree positions for tree nodes.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   334
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   335
    private class PosTester extends TreeScanner {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   336
        void test(JCCompilationUnit tree) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   337
            sourcefile = tree.sourcefile;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   338
            endPosTable = tree.endPositions;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   339
            encl = new Info();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   340
            tree.accept(this);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   341
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   342
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   343
        @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   344
        public void scan(JCTree tree) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   345
            if (tree == null)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   346
                return;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   347
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   348
            Info self = new Info(tree, endPosTable);
4933
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   349
            if (check(encl, self)) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   350
                // Modifiers nodes are present throughout the tree even where
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   351
                // there is no corresponding source text.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   352
                // Redundant semicolons in a class definition can cause empty
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   353
                // initializer blocks with no positions.
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   354
                if ((self.tag == MODIFIERS || self.tag == BLOCK)
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   355
                        && self.pos == NOPOS) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   356
                    // 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
   357
                    check("start == NOPOS", encl, self, self.start == NOPOS);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   358
                    check("end == NOPOS", encl, self, self.end == NOPOS);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   359
                } else {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   360
                    // For this node, start , pos, and endpos should be all defined
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   361
                    check("start != NOPOS", encl, self, self.start != NOPOS);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   362
                    check("pos != NOPOS", encl, self, self.pos != NOPOS);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   363
                    check("end != NOPOS", encl, self, self.end != NOPOS);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   364
                    // The following should normally be ordered
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   365
                    // encl.start <= start <= pos <= end <= encl.end
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   366
                    // In addition, the position of the enclosing node should be
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   367
                    // within this node.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   368
                    // The primary exceptions are for array type nodes, because of the
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   369
                    // need to support legacy syntax:
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   370
                    //    e.g.    int a[];    int[] b[];    int f()[] { return null; }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   371
                    // and because of inconsistent nesting of left and right of
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   372
                    // array declarations:
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   373
                    //    e.g.    int[][] a = new int[2][];
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 15385
diff changeset
   374
                    if (!(encl.tag == REFERENCE && self.tag == ANNOTATED_TYPE)) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   375
                    check("encl.start <= start", encl, self, encl.start <= self.start);
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 15385
diff changeset
   376
                    }
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   377
                    check("start <= pos", encl, self, self.start <= self.pos);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   378
                    if (!( (self.tag == TYPEARRAY ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   379
                            isAnnotatedArray(self.tree))
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   380
                            && (encl.tag == VARDEF ||
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   381
                                encl.tag == METHODDEF ||
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   382
                                encl.tag == TYPEARRAY ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   383
                                isAnnotatedArray(encl.tree))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   384
                           ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   385
                            encl.tag == ANNOTATED_TYPE && self.tag == SELECT
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 15385
diff changeset
   386
                           ||
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 15385
diff changeset
   387
                            encl.tag == REFERENCE && self.tag == ANNOTATED_TYPE
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   388
                         )) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   389
                        check("encl.pos <= start || end <= encl.pos",
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   390
                                encl, self, encl.pos <= self.start || self.end <= encl.pos);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   391
                    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   392
                    check("pos <= end", encl, self, self.pos <= self.end);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   393
                    if (!( (self.tag == TYPEARRAY || isAnnotatedArray(self.tree)) &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   394
                            (encl.tag == TYPEARRAY || isAnnotatedArray(encl.tree))
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   395
                           ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   396
                            encl.tag == MODIFIERS && self.tag == ANNOTATION
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   397
                         ) ) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   398
                        check("end <= encl.end", encl, self, self.end <= encl.end);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   399
                    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   400
                }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   401
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   402
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   403
            Info prevEncl = encl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   404
            encl = self;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   405
            tree.accept(this);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   406
            encl = prevEncl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   407
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   408
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   409
        private boolean isAnnotatedArray(JCTree tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   410
            return tree.hasTag(ANNOTATED_TYPE) &&
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   411
                            ((JCAnnotatedType)tree).underlyingType.hasTag(TYPEARRAY);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   412
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   413
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   414
        @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   415
        public void visitVarDef(JCVariableDecl tree) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   416
            // enum member declarations are desugared in the parser and have
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   417
            // ill-defined semantics for tree positions, so for now, we
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   418
            // skip the synthesized bits and just check parts which came from
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   419
            // the original source text
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   420
            if ((tree.mods.flags & Flags.ENUM) != 0) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   421
                scan(tree.mods);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   422
                if (tree.init != null) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   423
                    if (tree.init.hasTag(NEWCLASS)) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   424
                        JCNewClass init = (JCNewClass) tree.init;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   425
                        if (init.args != null && init.args.nonEmpty()) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   426
                            scan(init.args);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   427
                        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   428
                        if (init.def != null && init.def.defs != null) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   429
                            scan(init.def.defs);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   430
                        }
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
            } else
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   434
                super.visitVarDef(tree);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   435
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   436
4933
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   437
        boolean check(Info encl, Info self) {
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   438
            if (excludeTags.size() > 0) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   439
                if (encl != null && excludeTags.contains(getTagName(encl.tag))
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   440
                        || excludeTags.contains(getTagName(self.tag)))
4933
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   441
                    return false;
57f71400ba29 6926555: 6921979 breaks TreePosTest
jjg
parents: 4874
diff changeset
   442
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   443
            return tags.size() == 0 || tags.contains(getTagName(self.tag));
4870
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
        void check(String label, Info encl, Info self, boolean ok) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   447
            if (!ok) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   448
                if (gui) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   449
                    if (viewer == null)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   450
                        viewer = new Viewer();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   451
                    viewer.addEntry(sourcefile, label, encl, self);
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
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   454
                String s = "encl: " + encl.tree.toString() +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 13077
diff changeset
   455
                        "  this: " + self.tree.toString();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   456
                String msg = sourcefile.getName() + ": " + label + ": " +
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   457
                        "encl:" + encl + " this:" + self + "\n" +
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   458
                        s.substring(0, Math.min(80, s.length())).replaceAll("[\r\n]+", " ");
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   459
                error(msg);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   460
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   461
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   462
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   463
        JavaFileObject sourcefile;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   464
        EndPosTable endPosTable;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   465
        Info encl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   466
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   467
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   468
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   469
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   470
     * 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
   471
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   472
    private class Info {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   473
        Info() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   474
            tree = null;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   475
            tag = ERRONEOUS;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   476
            start = 0;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   477
            pos = 0;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   478
            end = Integer.MAX_VALUE;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   479
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   480
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   481
        Info(JCTree tree, EndPosTable endPosTable) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   482
            this.tree = tree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   483
            tag = tree.getTag();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   484
            start = TreeInfo.getStartPos(tree);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   485
            pos = tree.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   486
            end = TreeInfo.getEndPos(tree, endPosTable);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   487
        }
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
        @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   490
        public String toString() {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   491
            return getTagName(tree.getTag()) + "[start:" + start + ",pos:" + pos + ",end:" + end + "]";
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   492
        }
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
        final JCTree tree;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   495
        final JCTree.Tag tag;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   496
        final int start;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   497
        final int pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   498
        final int end;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   499
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   500
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   501
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   502
     * Thrown when errors are found parsing a java file.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   503
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   504
    private static class ParseException extends Exception {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   505
        ParseException(String msg) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   506
            super(msg);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   507
        }
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
     * DiagnosticListener to report diagnostics and count any errors that occur.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   512
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   513
    private static class Reporter implements DiagnosticListener<JavaFileObject> {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   514
        Reporter(PrintWriter out) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   515
            this.out = out;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   516
        }
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
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   519
            out.println(diagnostic);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   520
            switch (diagnostic.getKind()) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   521
                case ERROR:
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   522
                    errors++;
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
        int errors;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   526
        PrintWriter out;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   527
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   528
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   529
    /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   530
     * 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
   531
     * down list for selecting error conditions, a header area providing details
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   532
     * 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
   533
     * appropriate.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   534
     */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   535
    private class Viewer extends JFrame {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   536
        /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   537
         * Create a viewer.
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
        Viewer() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   540
            initGUI();
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
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   543
        /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   544
         * Add another entry to the list of errors.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   545
         * @param file The file containing the error
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   546
         * @param check The condition that was being tested, and which failed
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   547
         * @param encl the enclosing tree node
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   548
         * @param self the tree node containing the error
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
        void addEntry(JavaFileObject file, String check, Info encl, Info self) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   551
            Entry e = new Entry(file, check, encl, self);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   552
            DefaultComboBoxModel m = (DefaultComboBoxModel) entries.getModel();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   553
            m.addElement(e);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   554
            if (m.getSize() == 1)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   555
                entries.setSelectedItem(e);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   556
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   557
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   558
        /**
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   559
         * Initialize the GUI window.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   560
         */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   561
        private void initGUI() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   562
            JPanel head = new JPanel(new GridBagLayout());
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   563
            GridBagConstraints lc = new GridBagConstraints();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   564
            GridBagConstraints fc = new GridBagConstraints();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   565
            fc.anchor = GridBagConstraints.WEST;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   566
            fc.fill = GridBagConstraints.HORIZONTAL;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   567
            fc.gridwidth = GridBagConstraints.REMAINDER;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   568
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   569
            entries = new JComboBox();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   570
            entries.addActionListener(new ActionListener() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   571
                public void actionPerformed(ActionEvent e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   572
                    showEntry((Entry) entries.getSelectedItem());
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   573
                }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   574
            });
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   575
            fc.insets.bottom = 10;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   576
            head.add(entries, fc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   577
            fc.insets.bottom = 0;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   578
            head.add(new JLabel("check:"), lc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   579
            head.add(checkField = createTextField(80), fc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   580
            fc.fill = GridBagConstraints.NONE;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   581
            head.add(setBackground(new JLabel("encl:"), enclColor), lc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   582
            head.add(enclPanel = new InfoPanel(), fc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   583
            head.add(setBackground(new JLabel("self:"), selfColor), lc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   584
            head.add(selfPanel = new InfoPanel(), fc);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   585
            add(head, BorderLayout.NORTH);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   586
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   587
            body = new JTextArea();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   588
            body.setFont(Font.decode(Font.MONOSPACED));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   589
            body.addCaretListener(new CaretListener() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   590
                public void caretUpdate(CaretEvent e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   591
                    int dot = e.getDot();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   592
                    int mark = e.getMark();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   593
                    if (dot == mark)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   594
                        statusText.setText("dot: " + dot);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   595
                    else
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   596
                        statusText.setText("dot: " + dot + ", mark:" + mark);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   597
                }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   598
            });
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   599
            JScrollPane p = new JScrollPane(body,
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   600
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   601
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   602
            p.setPreferredSize(new Dimension(640, 480));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   603
            add(p, BorderLayout.CENTER);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   604
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   605
            statusText = createTextField(80);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   606
            add(statusText, BorderLayout.SOUTH);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   607
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   608
            pack();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   609
            setLocationRelativeTo(null); // centered on screen
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   610
            setVisible(true);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   611
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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
        /** Show an entry that has been selected. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   615
        private void showEntry(Entry e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   616
            try {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   617
                // update simple fields
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   618
                setTitle(e.file.getName());
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   619
                checkField.setText(e.check);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   620
                enclPanel.setInfo(e.encl);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   621
                selfPanel.setInfo(e.self);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   622
                // show file text with highlights
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   623
                body.setText(e.file.getCharContent(true).toString());
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   624
                Highlighter highlighter = body.getHighlighter();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   625
                highlighter.removeAllHighlights();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   626
                addHighlight(highlighter, e.encl, enclColor);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   627
                addHighlight(highlighter, e.self, selfColor);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   628
                scroll(body, getMinPos(enclPanel.info, selfPanel.info));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   629
            } catch (IOException ex) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   630
                body.setText("Cannot read " + e.file.getName() + ": " + e);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   631
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   632
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   633
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   634
        /** Create a test field. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   635
        private JTextField createTextField(int width) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   636
            JTextField f = new JTextField(width);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   637
            f.setEditable(false);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   638
            f.setBorder(null);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   639
            return f;
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
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   642
        /** 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
   643
        private void addHighlight(Highlighter h, Info info, Color c) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   644
            int start = info.start;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   645
            int end = info.end;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   646
            if (start == -1 && end == -1)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   647
                return;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   648
            if (start == -1)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   649
                start = end;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   650
            if (end == -1)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   651
                end = start;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   652
            try {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   653
                h.addHighlight(info.start, info.end,
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   654
                        new DefaultHighlighter.DefaultHighlightPainter(c));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   655
                if (info.pos != -1) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   656
                    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
   657
                    h.addHighlight(info.pos, info.pos + 1,
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   658
                        new DefaultHighlighter.DefaultHighlightPainter(c2));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   659
                }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   660
            } catch (BadLocationException e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   661
                e.printStackTrace();
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
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   665
        /** Get the minimum valid position in a set of info objects. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   666
        private int getMinPos(Info... values) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   667
            int i = Integer.MAX_VALUE;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   668
            for (Info info: values) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   669
                if (info.start >= 0) i = Math.min(i, info.start);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   670
                if (info.pos   >= 0) i = Math.min(i, info.pos);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   671
                if (info.end   >= 0) i = Math.min(i, info.end);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   672
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   673
            return (i == Integer.MAX_VALUE) ? 0 : i;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   674
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   675
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   676
        /** Set the background on a component. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   677
        private JComponent setBackground(JComponent comp, Color c) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   678
            comp.setOpaque(true);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   679
            comp.setBackground(c);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   680
            return comp;
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
        /** 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
   684
        private void scroll(final JTextArea t, final int pos) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   685
            // 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
   686
            // before the scroll happens; otherwise scrollRectToVisible doesn't work.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   687
            // Maybe there's a better way to sync with the text...
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   688
            EventQueue.invokeLater(new Runnable() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   689
                public void run() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   690
                    try {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   691
                        Rectangle r = t.modelToView(pos);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   692
                        JScrollPane p = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, t);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   693
                        r.y = Math.max(0, r.y - p.getHeight() * 2 / 5);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   694
                        r.height += p.getHeight() * 4 / 5;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   695
                        t.scrollRectToVisible(r);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   696
                    } catch (BadLocationException ignore) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   697
                    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   698
                }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   699
            });
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   700
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   701
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   702
        private JComboBox entries;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   703
        private JTextField checkField;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   704
        private InfoPanel enclPanel;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   705
        private InfoPanel selfPanel;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   706
        private JTextArea body;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   707
        private JTextField statusText;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   708
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   709
        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
   710
        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
   711
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   712
        /** Panel to display an Info object. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   713
        private class InfoPanel extends JPanel {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   714
            InfoPanel() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   715
                add(tagName = createTextField(20));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   716
                add(new JLabel("start:"));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   717
                add(addListener(start = createTextField(6)));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   718
                add(new JLabel("pos:"));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   719
                add(addListener(pos = createTextField(6)));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   720
                add(new JLabel("end:"));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   721
                add(addListener(end = createTextField(6)));
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
            void setInfo(Info info) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   725
                this.info = info;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   726
                tagName.setText(getTagName(info.tag));
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   727
                start.setText(String.valueOf(info.start));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   728
                pos.setText(String.valueOf(info.pos));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   729
                end.setText(String.valueOf(info.end));
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
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   732
            JTextField addListener(final JTextField f) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   733
                f.addMouseListener(new MouseAdapter() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   734
                    @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   735
                    public void mouseClicked(MouseEvent e) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   736
                        body.setCaretPosition(Integer.valueOf(f.getText()));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   737
                        body.getCaret().setVisible(true);
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
                return f;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   741
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   742
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   743
            Info info;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   744
            JTextField tagName;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   745
            JTextField start;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   746
            JTextField pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   747
            JTextField end;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   748
        }
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
        /** Object to record information about an error to be displayed. */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   751
        private class Entry {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   752
            Entry(JavaFileObject file, String check, Info encl, Info self) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   753
                this.file = file;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   754
                this.check = check;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   755
                this.encl = encl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   756
                this.self= self;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   757
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   758
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   759
            @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   760
            public String toString() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   761
                return file.getName() + " " + check + " " + getMinPos(encl, self);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   762
            }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   763
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   764
            final JavaFileObject file;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   765
            final String check;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   766
            final Info encl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   767
            final Info self;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   768
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   769
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   770
}
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
   771