Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
25 /**
26 * Utility and test program to check javac's internal TreeScanner class.
27 * The program can be run standalone, or as a jtreg test. For info on
28 * command line args, run program with no args.
29 *
30 * <p>
31 * jtreg: Note that by using the -r switch in the test description below, this test
32 * will process all java files in the langtools/test directory, thus implicitly
33 * covering any new language features that may be tested in this test suite.
34 */
36 /*
37 * @test
38 * @bug 6923080
39 * @summary TreeScanner.visitNewClass should scan tree.typeargs
40 * @build AbstractTreeScannerTest JavacTreeScannerTest
41 * @run main JavacTreeScannerTest -q -r .
42 */
44 import java.io.*;
45 import java.lang.reflect.*;
46 import java.util.*;
47 import javax.tools.*;
49 import com.sun.tools.javac.tree.JCTree;
50 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
51 import com.sun.tools.javac.tree.TreeScanner;
52 import com.sun.tools.javac.util.List;
54 public class JavacTreeScannerTest extends AbstractTreeScannerTest {
55 /**
56 * Main entry point.
57 * If test.src is set, program runs in jtreg mode, and will throw an Error
58 * if any errors arise, otherwise System.exit will be used. In jtreg mode,
59 * the default base directory for file args is the value of ${test.src}.
60 * In jtreg mode, the -r option can be given to change the default base
61 * directory to the root test directory.
62 */
63 public static void main(String... args) {
64 String testSrc = System.getProperty("test.src");
65 File baseDir = (testSrc == null) ? null : new File(testSrc);
66 boolean ok = new JavacTreeScannerTest().run(baseDir, args);
67 if (!ok) {
68 if (testSrc != null) // jtreg mode
69 throw new Error("failed");
70 else
71 System.exit(1);
72 }
73 }
75 int test(JCCompilationUnit tree) {
76 return new ScanTester().test(tree);
77 }
79 /**
80 * Main class for testing operation of tree scanner.
81 * The set of nodes found by the scanner are compared
82 * against the set of nodes found by reflection.
83 */
84 private class ScanTester extends TreeScanner {
85 /** Main entry method for the class. */
86 int test(JCCompilationUnit tree) {
87 sourcefile = tree.sourcefile;
88 found = new HashSet<JCTree>();
89 scan(tree);
90 expect = new HashSet<JCTree>();
91 reflectiveScan(tree);
93 if (found.equals(expect)) {
94 //System.err.println(sourcefile.getName() + ": trees compared OK");
95 return found.size();
96 }
98 error(sourcefile, "differences found");
100 if (found.size() != expect.size())
101 error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
103 Set<JCTree> missing = new HashSet<JCTree>();
104 missing.addAll(expect);
105 missing.removeAll(found);
106 for (JCTree t: missing)
107 error(sourcefile, t, "missing");
109 Set<JCTree> excess = new HashSet<JCTree>();
110 excess.addAll(found);
111 excess.removeAll(expect);
112 for (JCTree t: excess)
113 error(sourcefile, t, "unexpected");
115 return 0;
116 }
118 /** Record all tree nodes found by scanner. */
119 @Override
120 public void scan(JCTree tree) {
121 if (tree == null)
122 return;
123 //System.err.println("FOUND: " + tree.getTag() + " " + trim(tree, 64));
124 found.add(tree);
125 super.scan(tree);
126 }
128 /** record all tree nodes found by reflection. */
129 public void reflectiveScan(Object o) {
130 if (o == null)
131 return;
132 if (o instanceof JCTree) {
133 JCTree tree = (JCTree) o;
134 //System.err.println("EXPECT: " + tree.getTag() + " " + trim(tree, 64));
135 expect.add(tree);
136 for (Field f: getFields(tree)) {
137 try {
138 //System.err.println("FIELD: " + f.getName());
139 reflectiveScan(f.get(tree));
140 } catch (IllegalAccessException e) {
141 error(e.toString());
142 }
143 }
144 } else if (o instanceof List) {
145 List<?> list = (List<?>) o;
146 for (Object item: list)
147 reflectiveScan(item);
148 } else
149 error("unexpected item: " + o);
150 }
152 JavaFileObject sourcefile;
153 Set<JCTree> found;
154 Set<JCTree> expect;
155 }
156 }