1 /*
2  * Copyright (c) 2010, 2017, 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  */
23 
24 /**
25  * Utility and test program to check javac's internal TreeScanner class.
26  * The program can be run standalone, or as a jtreg test.  For info on
27  * command line args, run program with no args.
28  *
29  * <p>
30  * jtreg: Note that by using the -r switch in the test description below, this test
31  * will process all java files in the langtools/test directory, thus implicitly
32  * covering any new language features that may be tested in this test suite.
33  */
34 
35 /*
36  * @test
37  * @bug 6923080
38  * @summary TreeScanner.visitNewClass should scan tree.typeargs
39  * @modules jdk.compiler/com.sun.tools.javac.api
40  *          jdk.compiler/com.sun.tools.javac.file
41  *          jdk.compiler/com.sun.tools.javac.tree
42  *          jdk.compiler/com.sun.tools.javac.util
43  * @build AbstractTreeScannerTest JavacTreeScannerTest
44  * @run main JavacTreeScannerTest -q -r .
45  */
46 
47 import java.io.*;
48 import java.lang.reflect.*;
49 import java.util.*;
50 import javax.tools.*;
51 
52 import com.sun.tools.javac.tree.JCTree;
53 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
54 import com.sun.tools.javac.tree.TreeScanner;
55 import com.sun.tools.javac.util.List;
56 
57 public class JavacTreeScannerTest extends AbstractTreeScannerTest {
58     /**
59      * Main entry point.
60      * If test.src is set, program runs in jtreg mode, and will throw an Error
61      * if any errors arise, otherwise System.exit will be used. In jtreg mode,
62      * the default base directory for file args is the value of ${test.src}.
63      * In jtreg mode, the -r option can be given to change the default base
64      * directory to the root test directory.
65      */
main(String... args)66     public static void main(String... args) {
67         String testSrc = System.getProperty("test.src");
68         File baseDir = (testSrc == null) ? null : new File(testSrc);
69         boolean ok = new JavacTreeScannerTest().run(baseDir, args);
70         if (!ok) {
71             if (testSrc != null)  // jtreg mode
72                 throw new Error("failed");
73             else
74                 System.exit(1);
75         }
76     }
77 
test(JCCompilationUnit tree)78     int test(JCCompilationUnit tree) {
79         return new ScanTester().test(tree);
80     }
81 
82     /**
83      * Main class for testing operation of tree scanner.
84      * The set of nodes found by the scanner are compared
85      * against the set of nodes found by reflection.
86      */
87     private class ScanTester extends TreeScanner {
88         /** Main entry method for the class. */
test(JCCompilationUnit tree)89         int test(JCCompilationUnit tree) {
90             sourcefile = tree.sourcefile;
91             found = new HashSet<JCTree>();
92             scan(tree);
93             expect = new HashSet<JCTree>();
94             reflectiveScan(tree);
95 
96             if (found.equals(expect)) {
97                 //System.err.println(sourcefile.getName() + ": trees compared OK");
98                 return found.size();
99             }
100 
101             error(sourcefile, "differences found");
102 
103             if (found.size() != expect.size())
104                 error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
105 
106             Set<JCTree> missing = new HashSet<JCTree>();
107             missing.addAll(expect);
108             missing.removeAll(found);
109             for (JCTree t: missing)
110                 error(sourcefile, t, "missing");
111 
112             Set<JCTree> excess = new HashSet<JCTree>();
113             excess.addAll(found);
114             excess.removeAll(expect);
115             for (JCTree t: excess)
116                 error(sourcefile, t, "unexpected");
117 
118             return 0;
119         }
120 
121         /** Record all tree nodes found by scanner. */
122         @Override
scan(JCTree tree)123         public void scan(JCTree tree) {
124             if (tree == null)
125                 return;
126             //System.err.println("FOUND: " + tree.getTag() + " " + trim(tree, 64));
127             found.add(tree);
128             super.scan(tree);
129         }
130 
131         /** record all tree nodes found by reflection. */
reflectiveScan(Object o)132         public void reflectiveScan(Object o) {
133             if (o == null)
134                 return;
135             if (o instanceof JCTree) {
136                 JCTree tree = (JCTree) o;
137                 //System.err.println("EXPECT: " + tree.getTag() + " " + trim(tree, 64));
138                 expect.add(tree);
139                 for (Field f: getFields(tree)) {
140                     try {
141                         //System.err.println("FIELD: " + f.getName());
142                         reflectiveScan(f.get(tree));
143                     } catch (IllegalAccessException e) {
144                         error(e.toString());
145                     }
146                 }
147             } else if (o instanceof List) {
148                 List<?> list = (List<?>) o;
149                 for (Object item: list)
150                     reflectiveScan(item);
151             } else
152                 error("unexpected item: " + o);
153         }
154 
155         JavaFileObject sourcefile;
156         Set<JCTree> found;
157         Set<JCTree> expect;
158     }
159 }
160