langtools/test/tools/javac/tree/TreeScannerTest.java
author jjg
Thu, 04 Feb 2010 10:14:28 -0800
changeset 4877 b642d21c9f74
child 5520 86e4b9a9da40
permissions -rw-r--r--
6923080: TreeScanner.visitNewClass should scan tree.typeargs Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     1
/*
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     2
 * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     4
 *
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     8
 *
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    13
 * accompanied this code).
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    14
 *
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    18
 *
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    21
 * have any questions.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    22
 */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    23
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    24
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    25
/**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    26
 * Utility and test program to check javac's internal TreeScanner class.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    27
 * The program can be run standalone, or as a jtreg test.  For info on
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    28
 * command line args, run program with no args.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    29
 *
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    30
 * <p>
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    31
 * jtreg: Note that by using the -r switch in the test description below, this test
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    32
 * will process all java files in the langtools/test directory, thus implicitly
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    33
 * covering any new language features that may be tested in this test suite.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    34
 */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    35
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    36
/*
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    37
 * @test
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    38
 * @bug 6923080
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    39
 * @summary TreeScanner.visitNewClass should scan tree.typeargs
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    40
 * @run main TreeScannerTest -q -r .
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    41
 */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    42
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    43
import java.io.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    44
import java.lang.reflect.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    45
import java.util.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    46
import javax.tools.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    47
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    48
import com.sun.source.tree.CompilationUnitTree;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    49
import com.sun.source.util.JavacTask;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    50
import com.sun.tools.javac.api.JavacTool;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    51
import com.sun.tools.javac.tree.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    52
import com.sun.tools.javac.tree.JCTree.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    53
import com.sun.tools.javac.util.List;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    54
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    55
public class TreeScannerTest {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    56
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    57
     * Main entry point.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    58
     * If test.src is set, program runs in jtreg mode, and will throw an Error
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    59
     * if any errors arise, otherwise System.exit will be used. In jtreg mode,
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    60
     * the default base directory for file args is the value of ${test.src}.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    61
     * In jtreg mode, the -r option can be given to change the default base
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    62
     * directory to the root test directory.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    63
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    64
    public static void main(String... args) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    65
        String testSrc = System.getProperty("test.src");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    66
        File baseDir = (testSrc == null) ? null : new File(testSrc);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    67
        boolean ok = new TreeScannerTest().run(baseDir, args);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    68
        if (!ok) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    69
            if (testSrc != null)  // jtreg mode
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    70
                throw new Error("failed");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    71
            else
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    72
                System.exit(1);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    73
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    74
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    75
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    76
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    77
     * Run the program. A base directory can be provided for file arguments.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    78
     * In jtreg mode, the -r option can be given to change the default base
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    79
     * directory to the test root directory. For other options, see usage().
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    80
     * @param baseDir base directory for any file arguments.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    81
     * @param args command line args
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    82
     * @return true if successful or in gui mode
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    83
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    84
    boolean run(File baseDir, String... args) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    85
        if (args.length == 0) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    86
            usage(System.out);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    87
            return true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    88
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    89
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    90
        ArrayList<File> files = new ArrayList<File>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    91
        for (int i = 0; i < args.length; i++) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    92
            String arg = args[i];
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    93
            if (arg.equals("-q"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    94
                quiet = true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    95
            else if (arg.equals("-v"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    96
                verbose = true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    97
            else if (arg.equals("-r")) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    98
                File d = baseDir;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    99
                while (!new File(d, "TEST.ROOT").exists()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   100
                    d = d.getParentFile();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   101
                    if (d == null)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   102
                        throw new Error("cannot find TEST.ROOT");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   103
                }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   104
                baseDir = d;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   105
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   106
            else if (arg.startsWith("-"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   107
                throw new Error("unknown option: " + arg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   108
            else {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   109
                while (i < args.length)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   110
                    files.add(new File(baseDir, args[i++]));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   111
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   112
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   113
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   114
        for (File file: files) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   115
            if (file.exists())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   116
                test(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   117
            else
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   118
                error("File not found: " + file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   119
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   120
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   121
        if (fileCount != 1)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   122
            System.err.println(fileCount + " files read");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   123
        if (errors > 0)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   124
            System.err.println(errors + " errors");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   125
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   126
        return (errors == 0);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   127
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   128
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   129
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   130
     * Print command line help.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   131
     * @param out output stream
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   132
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   133
    void usage(PrintStream out) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   134
        out.println("Usage:");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   135
        out.println("  java TreeScannerTest options... files...");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   136
        out.println("");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   137
        out.println("where options include:");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   138
        out.println("-q        Quiet: don't report on inapplicable files");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   139
        out.println("-v        Verbose: report on files as they are being read");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   140
        out.println("");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   141
        out.println("files may be directories or files");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   142
        out.println("directories will be scanned recursively");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   143
        out.println("non java files, or java files which cannot be parsed, will be ignored");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   144
        out.println("");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   145
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   146
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   147
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   148
     * Test a file. If the file is a directory, it will be recursively scanned
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   149
     * for java files.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   150
     * @param file the file or directory to test
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   151
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   152
    void test(File file) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   153
        if (file.isDirectory()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   154
            for (File f: file.listFiles()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   155
                test(f);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   156
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   157
            return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   158
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   159
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   160
        if (file.isFile() && file.getName().endsWith(".java")) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   161
            try {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   162
                if (verbose)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   163
                    System.err.println(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   164
                fileCount++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   165
                ScanTester t = new ScanTester();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   166
                t.test(read(file));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   167
            } catch (ParseException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   168
                if (!quiet) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   169
                    error("Error parsing " + file + "\n" + e.getMessage());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   170
                }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   171
            } catch (IOException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   172
                error("Error reading " + file + ": " + e);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   173
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   174
            return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   175
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   176
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   177
        if (!quiet)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   178
            error("File " + file + " ignored");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   179
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   180
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   181
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   182
     * Read a file.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   183
     * @param file the file to be read
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   184
     * @return the tree for the content of the file
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   185
     * @throws IOException if any IO errors occur
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   186
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   187
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   188
    JCCompilationUnit read(File file) throws IOException, ParseException {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   189
        StringWriter sw = new StringWriter();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   190
        PrintWriter pw = new PrintWriter(sw);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   191
        Reporter r = new Reporter(pw);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   192
        JavacTool tool = JavacTool.create();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   193
        StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   194
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   195
        JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   196
        Iterable<? extends CompilationUnitTree> trees = task.parse();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   197
        pw.flush();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   198
        if (r.errors > 0)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   199
            throw new ParseException(sw.toString());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   200
        Iterator<? extends CompilationUnitTree> iter = trees.iterator();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   201
        if (!iter.hasNext())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   202
            throw new Error("no trees found");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   203
        JCCompilationUnit t = (JCCompilationUnit) iter.next();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   204
        if (iter.hasNext())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   205
            throw new Error("too many trees found");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   206
        return t;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   207
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   208
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   209
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   210
     * Report an error. When the program is complete, the program will either
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   211
     * exit or throw an Error if any errors have been reported.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   212
     * @param msg the error message
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   213
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   214
    void error(String msg) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   215
        System.err.println(msg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   216
        errors++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   217
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   218
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   219
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   220
     *  Report an error for a specific tree node.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   221
     *  @param file the source file for the tree
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   222
     *  @param t    the tree node
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   223
     *  @param label an indication of the error
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   224
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   225
    void error(JavaFileObject file, JCTree t, String msg) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   226
        error(file.getName() + ":" + getLine(file, t) + ": " + msg + " " + trim(t, 64));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   227
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   228
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   229
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   230
     * Get a trimmed string for a tree node, with normalized white space and limited length.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   231
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   232
    String trim(JCTree t, int len) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   233
        String s = t.toString().replaceAll("[\r\n]+", " ").replaceAll(" +", " ");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   234
        return (s.length() < len) ? s : s.substring(0, len);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   235
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   236
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   237
    /** Number of files that have been analyzed. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   238
    int fileCount;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   239
    /** Number of errors reported. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   240
    int errors;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   241
    /** Flag: don't report irrelevant files. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   242
    boolean quiet;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   243
    /** Flag: report files as they are processed. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   244
    boolean verbose;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   245
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   246
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   247
     * Main class for testing operation of tree scanner.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   248
     * The set of nodes found by the scanner are compared
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   249
     * against the set of nodes found by reflection.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   250
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   251
    private class ScanTester extends TreeScanner {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   252
        /** Main entry method for the class. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   253
        void test(JCCompilationUnit tree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   254
            sourcefile = tree.sourcefile;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   255
            found = new HashSet<JCTree>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   256
            scan(tree);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   257
            expect = new HashSet<JCTree>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   258
            reflectiveScan(tree);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   259
            if (found.equals(expect))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   260
                return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   261
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   262
            error("Differences found for " + tree.sourcefile.getName());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   263
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   264
            if (found.size() != expect.size())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   265
                error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   266
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   267
            Set<JCTree> missing = new HashSet<JCTree>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   268
            missing.addAll(expect);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   269
            missing.removeAll(found);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   270
            for (JCTree t: missing)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   271
                error(tree.sourcefile, t, "missing");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   272
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   273
            Set<JCTree> excess = new HashSet<JCTree>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   274
            excess.addAll(found);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   275
            excess.removeAll(expect);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   276
            for (JCTree t: excess)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   277
                error(tree.sourcefile, t, "unexpected");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   278
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   279
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   280
        /** Record all tree nodes found by scanner. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   281
        @Override
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   282
        public void scan(JCTree tree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   283
            if (tree == null)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   284
                return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   285
            System.err.println("FOUND: " + tree.getTag() + " " + trim(tree, 64));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   286
            found.add(tree);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   287
            super.scan(tree);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   288
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   289
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   290
        /** record all tree nodes found by reflection. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   291
        public void reflectiveScan(Object o) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   292
            if (o == null)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   293
                return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   294
            if (o instanceof JCTree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   295
                JCTree tree = (JCTree) o;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   296
                System.err.println("EXPECT: " + tree.getTag() + " " + trim(tree, 64));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   297
                expect.add(tree);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   298
                for (Field f: getFields(tree)) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   299
                    try {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   300
                        //System.err.println("FIELD: " + f.getName());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   301
                        reflectiveScan(f.get(tree));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   302
                    } catch (IllegalAccessException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   303
                        error(e.toString());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   304
                    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   305
                }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   306
            } else if (o instanceof List) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   307
                List<?> list = (List<?>) o;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   308
                for (Object item: list)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   309
                    reflectiveScan(item);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   310
            } else
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   311
                error("unexpected item: " + o);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   312
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   313
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   314
        JavaFileObject sourcefile;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   315
        Set<JCTree> found;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   316
        Set<JCTree> expect;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   317
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   318
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   319
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   320
     * Thrown when errors are found parsing a java file.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   321
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   322
    private static class ParseException extends Exception {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   323
        ParseException(String msg) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   324
            super(msg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   325
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   326
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   327
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   328
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   329
     * DiagnosticListener to report diagnostics and count any errors that occur.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   330
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   331
    private static class Reporter implements DiagnosticListener<JavaFileObject> {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   332
        Reporter(PrintWriter out) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   333
            this.out = out;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   334
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   335
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   336
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   337
            out.println(diagnostic);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   338
            switch (diagnostic.getKind()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   339
                case ERROR:
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   340
                    errors++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   341
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   342
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   343
        int errors;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   344
        PrintWriter out;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   345
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   346
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   347
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   348
     * Get the set of fields for a tree node that may contain child tree nodes.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   349
     * These are the fields that are subtypes of JCTree or List.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   350
     * The results are cached, based on the tree's tag.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   351
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   352
    Set<Field> getFields(JCTree tree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   353
        Set<Field> fields = map.get(tree.getTag());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   354
        if (fields == null) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   355
            fields = new HashSet<Field>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   356
            for (Field f: tree.getClass().getFields()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   357
                Class<?> fc = f.getType();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   358
                if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   359
                    fields.add(f);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   360
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   361
            map.put(tree.getTag(), fields);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   362
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   363
        return fields;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   364
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   365
    // where
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   366
    Map<Integer, Set<Field>> map = new HashMap<Integer,Set<Field>>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   367
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   368
    /** Get the line number for the primary position for a tree.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   369
     * The code is intended to be simple, although not necessarily efficient.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   370
     * However, note that a file manager such as JavacFileManager is likely
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   371
     * to cache the results of file.getCharContent, avoiding the need to read
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   372
     * the bits from disk each time this method is called.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   373
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   374
    int getLine(JavaFileObject file, JCTree tree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   375
        try {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   376
            CharSequence cs = file.getCharContent(true);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   377
            int line = 1;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   378
            for (int i = 0; i < tree.pos; i++) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   379
                if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   380
                    line++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   381
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   382
            return line;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   383
        } catch (IOException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   384
            return -1;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   385
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   386
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   387
}