test/langtools/tools/javac/tree/AbstractTreeScannerTest.java
author jlahoda
Tue, 13 Aug 2019 10:27:32 +0200
changeset 57723 54a04db114d8
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody() Summary: DocTreeScanner.visitAttribute and visitLiteral have to scan through the subnodes of AttributeTree and LiteralTree, respectivelly. Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
     1
/*
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
     2
 * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
4877
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    21
 * questions.
4877
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
import java.io.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    25
import java.lang.reflect.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    26
import java.util.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    27
import javax.tools.*;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    28
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
    29
import com.sun.source.doctree.DocCommentTree;
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
    30
import com.sun.source.doctree.DocTree;
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    31
import com.sun.source.tree.CompilationUnitTree;
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    32
import com.sun.source.tree.Tree;
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    33
import com.sun.source.util.JavacTask;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.api.JavacTool;
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
    35
import com.sun.tools.javac.tree.DCTree;
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
    36
import com.sun.tools.javac.tree.DCTree.DCDocComment;
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    37
import com.sun.tools.javac.tree.JCTree;
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
    39
import com.sun.tools.javac.util.Pair;
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    40
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    41
public abstract class AbstractTreeScannerTest {
4877
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
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    44
     * Run the program. A base directory can be provided for file arguments.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    45
     * 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
    46
     * directory to the test root directory. For other options, see usage().
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    47
     * @param baseDir base directory for any file arguments.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    48
     * @param args command line args
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    49
     * @return true if successful or in gui mode
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    50
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    51
    boolean run(File baseDir, String... args) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    52
        if (args.length == 0) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    53
            usage(System.out);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    54
            return true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    55
        }
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
        ArrayList<File> files = new ArrayList<File>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    58
        for (int i = 0; i < args.length; i++) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    59
            String arg = args[i];
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    60
            if (arg.equals("-q"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    61
                quiet = true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    62
            else if (arg.equals("-v"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    63
                verbose = true;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    64
            else if (arg.equals("-r")) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    65
                File d = baseDir;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    66
                while (!new File(d, "TEST.ROOT").exists()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    67
                    d = d.getParentFile();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    68
                    if (d == null)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    69
                        throw new Error("cannot find TEST.ROOT");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    70
                }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    71
                baseDir = d;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    72
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    73
            else if (arg.startsWith("-"))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    74
                throw new Error("unknown option: " + arg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    75
            else {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    76
                while (i < args.length)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    77
                    files.add(new File(baseDir, args[i++]));
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    78
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    79
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    80
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    81
        for (File file: files) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    82
            if (file.exists())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    83
                test(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    84
            else
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    85
                error("File not found: " + file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    86
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    87
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    88
        if (fileCount != 1)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    89
            System.err.println(fileCount + " files read");
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    90
        System.err.println(treeCount + " tree nodes compared");
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    91
        if (errors > 0)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    92
            System.err.println(errors + " errors");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    93
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    94
        return (errors == 0);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    95
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    96
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    97
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    98
     * Print command line help.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
    99
     * @param out output stream
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   100
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   101
    void usage(PrintStream out) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   102
        out.println("Usage:");
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   103
        out.println("  java " + getClass().getName() + " options... files...");
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   104
        out.println("");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   105
        out.println("where options include:");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   106
        out.println("-q        Quiet: don't report on inapplicable files");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   107
        out.println("-v        Verbose: report on files as they are being read");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   108
        out.println("");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   109
        out.println("files may be directories or files");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   110
        out.println("directories will be scanned recursively");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   111
        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
   112
        out.println("");
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
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   115
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   116
     * 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
   117
     * for java files.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   118
     * @param file the file or directory to test
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
    void test(File file) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   121
        if (file.isDirectory()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   122
            for (File f: file.listFiles()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   123
                test(f);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   124
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   125
            return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   126
        }
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
        if (file.isFile() && file.getName().endsWith(".java")) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   129
            try {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   130
                if (verbose)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   131
                    System.err.println(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   132
                fileCount++;
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   133
                treeCount += test(read(file));
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   134
            } catch (ParseException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   135
                if (!quiet) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   136
                    error("Error parsing " + file + "\n" + e.getMessage());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   137
                }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   138
            } catch (IOException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   139
                error("Error reading " + file + ": " + e);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   140
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   141
            return;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   142
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   143
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   144
        if (!quiet)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   145
            error("File " + file + " ignored");
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
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   148
    abstract int test(Pair<JavacTask, JCCompilationUnit> taskAndTree);
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   149
7846
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   150
    // 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: 6601
diff changeset
   151
    StringWriter sw = new StringWriter();
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   152
    PrintWriter pw = new PrintWriter(sw);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   153
    Reporter r = new Reporter(pw);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   154
    JavacTool tool = JavacTool.create();
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   155
    StandardJavaFileManager fm = tool.getStandardFileManager(r, null, null);
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   156
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   157
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   158
     * Read a file.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   159
     * @param file the file to be read
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   160
     * @return the tree for the content of the file
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   161
     * @throws IOException if any IO errors occur
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   162
     * @throws TreePosTest.ParseException if any errors occur while parsing the file
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   163
     */
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   164
    Pair<JavacTask, JCCompilationUnit> read(File file) throws IOException, ParseException {
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   165
        JavacTool tool = JavacTool.create();
7846
f72ce57eb9c1 6982992: Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
jjh
parents: 6601
diff changeset
   166
        r.errors = 0;
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   167
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   168
        JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   169
        Iterable<? extends CompilationUnitTree> trees = task.parse();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   170
        pw.flush();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   171
        if (r.errors > 0)
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   172
            throw new ParseException(sw.toString());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   173
        Iterator<? extends CompilationUnitTree> iter = trees.iterator();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   174
        if (!iter.hasNext())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   175
            throw new Error("no trees found");
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   176
        JCCompilationUnit t = (JCCompilationUnit) iter.next();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   177
        if (iter.hasNext())
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   178
            throw new Error("too many trees found");
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   179
        return Pair.of(task, t);
4877
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
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   183
     * Report an error. When the program is complete, the program will either
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   184
     * exit or throw an Error if any errors have been reported.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   185
     * @param msg the error message
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   186
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   187
    void error(String msg) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   188
        System.err.println(msg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   189
        errors++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   190
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   191
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   192
    /**
6601
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   193
     * Report an error. When the program is complete, the program will either
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   194
     * exit or throw an Error if any errors have been reported.
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   195
     * @param msg the error message
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   196
     */
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   197
    void error(JavaFileObject file, String msg) {
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   198
        System.err.println(file.getName() + ": " + msg);
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   199
        errors++;
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   200
    }
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   201
90c4a1a64217 6985115: tests create too much output
jjg
parents: 6597
diff changeset
   202
    /**
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   203
     *  Report an error for a specific tree node.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   204
     *  @param file the source file for the tree
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   205
     *  @param t    the tree node
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   206
     *  @param label an indication of the error
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   207
     */
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   208
    void error(JavaFileObject file, Tree tree, String msg) {
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   209
        JCTree t = (JCTree) tree;
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   210
        error(file.getName() + ":" + getLine(file, t.pos) + ": " + msg + " " + trim(t, 64));
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   211
    }
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   212
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   213
    /**
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   214
     *  Report an error for a specific tree node.
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   215
     *  @param file the source file for the tree
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   216
     *  @param t    the tree node
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   217
     *  @param label an indication of the error
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   218
     */
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   219
    void error(JavaFileObject file, DocCommentTree comment, DocTree tree, String msg) {
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   220
        DCDocComment dc = (DCDocComment) comment;
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   221
        DCTree t = (DCTree) tree;
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   222
        error(file.getName() + ":" + getLine(file, t.getSourcePosition(dc)) + ": " + msg + " " + trim(t, 64));
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   223
    }
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
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   226
     * 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
   227
     */
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   228
    String trim(Object tree, int len) {
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   229
        String s = tree.toString().replaceAll("\\s+", " ");
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   230
        return (s.length() < len) ? s : s.substring(0, len);
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
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   233
    /** Number of files that have been analyzed. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   234
    int fileCount;
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   235
    /** Number of trees that have been successfully compared. */
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
   236
    int treeCount;
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   237
    /** Number of errors reported. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   238
    int errors;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   239
    /** Flag: don't report irrelevant files. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   240
    boolean quiet;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   241
    /** Flag: report files as they are processed. */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   242
    boolean verbose;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   243
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   244
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
     * Thrown when errors are found parsing a java file.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   247
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   248
    private static class ParseException extends Exception {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   249
        ParseException(String msg) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   250
            super(msg);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   251
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   252
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   253
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   254
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   255
     * DiagnosticListener to report diagnostics and count any errors that occur.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   256
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   257
    private static class Reporter implements DiagnosticListener<JavaFileObject> {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   258
        Reporter(PrintWriter out) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   259
            this.out = out;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   260
        }
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
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   263
            out.println(diagnostic);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   264
            switch (diagnostic.getKind()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   265
                case ERROR:
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   266
                    errors++;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   267
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   268
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   269
        int errors;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   270
        PrintWriter out;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   271
    }
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
    /**
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   274
     * 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
   275
     * These are the fields that are subtypes of JCTree or List.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   276
     * The results are cached, based on the tree's tag.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   277
     */
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   278
    Set<Field> getFields(JCTree tree) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   279
        Set<Field> fields = map.get(tree.getTag());
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   280
        if (fields == null) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   281
            fields = new HashSet<Field>();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   282
            for (Field f: tree.getClass().getFields()) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   283
                Class<?> fc = f.getType();
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   284
                if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   285
                    fields.add(f);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   286
            }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   287
            map.put(tree.getTag(), fields);
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
        return fields;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   290
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   291
    // where
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7846
diff changeset
   292
    Map<JCTree.Tag, Set<Field>> map = new HashMap<JCTree.Tag,Set<Field>>();
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   293
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   294
    /**
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   295
     * Get the set of fields for a tree node that may contain child tree nodes.
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   296
     * These are the fields that are subtypes of DCTree or List.
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   297
     * The results are cached, based on the tree's tag.
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   298
     */
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   299
    Set<Field> getFields(DCTree tree) {
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   300
        Set<Field> fields = dcMap.get(tree.getKind());
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   301
        if (fields == null) {
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   302
            fields = new HashSet<Field>();
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   303
            for (Field f: tree.getClass().getFields()) {
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   304
                Class<?> fc = f.getType();
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   305
                if (DCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   306
                    fields.add(f);
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   307
            }
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   308
            dcMap.put(tree.getKind(), fields);
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   309
        }
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   310
        return fields;
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   311
    }
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   312
    // where
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   313
    Map<DCTree.Kind, Set<Field>> dcMap = new HashMap<>();
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   314
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   315
    /** Get the line number for the primary position for a tree.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   316
     * The code is intended to be simple, although not necessarily efficient.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   317
     * However, note that a file manager such as JavacFileManager is likely
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   318
     * to cache the results of file.getCharContent, avoiding the need to read
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   319
     * the bits from disk each time this method is called.
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   320
     */
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   321
    int getLine(JavaFileObject file, long pos) {
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   322
        try {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   323
            CharSequence cs = file.getCharContent(true);
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   324
            int line = 1;
57723
54a04db114d8 8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
jlahoda
parents: 47216
diff changeset
   325
            for (int i = 0; i < pos; i++) {
4877
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   326
                if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   327
                    line++;
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
            return line;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   330
        } catch (IOException e) {
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   331
            return -1;
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   332
        }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   333
    }
b642d21c9f74 6923080: TreeScanner.visitNewClass should scan tree.typeargs
jjg
parents:
diff changeset
   334
}