1 /* 2 * Copyright (c) 2010, 2019, 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.source.util.JavacTask; 53 import com.sun.tools.javac.tree.JCTree; 54 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 55 import com.sun.tools.javac.tree.TreeScanner; 56 import com.sun.tools.javac.util.List; 57 import com.sun.tools.javac.util.Pair; 58 59 public class JavacTreeScannerTest extends AbstractTreeScannerTest { 60 /** 61 * Main entry point. 62 * If test.src is set, program runs in jtreg mode, and will throw an Error 63 * if any errors arise, otherwise System.exit will be used. In jtreg mode, 64 * the default base directory for file args is the value of ${test.src}. 65 * In jtreg mode, the -r option can be given to change the default base 66 * directory to the root test directory. 67 */ main(String... args)68 public static void main(String... args) { 69 String testSrc = System.getProperty("test.src"); 70 File baseDir = (testSrc == null) ? null : new File(testSrc); 71 boolean ok = new JavacTreeScannerTest().run(baseDir, args); 72 if (!ok) { 73 if (testSrc != null) // jtreg mode 74 throw new Error("failed"); 75 else 76 System.exit(1); 77 } 78 } 79 test(Pair<JavacTask, JCCompilationUnit> taskAndTree)80 int test(Pair<JavacTask, JCCompilationUnit> taskAndTree) { 81 return new ScanTester().test(taskAndTree.snd); 82 } 83 84 /** 85 * Main class for testing operation of tree scanner. 86 * The set of nodes found by the scanner are compared 87 * against the set of nodes found by reflection. 88 */ 89 private class ScanTester extends TreeScanner { 90 /** Main entry method for the class. */ test(JCCompilationUnit tree)91 int test(JCCompilationUnit tree) { 92 sourcefile = tree.sourcefile; 93 found = new HashSet<JCTree>(); 94 scan(tree); 95 expect = new HashSet<JCTree>(); 96 reflectiveScan(tree); 97 98 if (found.equals(expect)) { 99 //System.err.println(sourcefile.getName() + ": trees compared OK"); 100 return found.size(); 101 } 102 103 error(sourcefile, "differences found"); 104 105 if (found.size() != expect.size()) { 106 error("Size mismatch; found: " + found.size() + ", expected: " + expect.size()); 107 Set<JCTree> notFound = new HashSet<>(expect); 108 notFound.removeAll(found); 109 if (!notFound.isEmpty()) { 110 System.err.println("found by reflective access to the AST, but not found in the scanner API:"); 111 notFound.forEach(t -> System.err.println(trim(t, 64))); 112 } 113 Set<JCTree> notExpected = new HashSet<>(found); 114 notExpected.removeAll(expect); 115 if (!notExpected.isEmpty()) { 116 System.err.println("found in the scanner API, but not found by reflective access to the AST:"); 117 notExpected.forEach(t -> System.err.println(trim(t, 64))); 118 } 119 } 120 121 Set<JCTree> missing = new HashSet<JCTree>(); 122 missing.addAll(expect); 123 missing.removeAll(found); 124 for (JCTree t: missing) 125 error(sourcefile, t, "missing"); 126 127 Set<JCTree> excess = new HashSet<JCTree>(); 128 excess.addAll(found); 129 excess.removeAll(expect); 130 for (JCTree t: excess) 131 error(sourcefile, t, "unexpected"); 132 133 return 0; 134 } 135 136 /** Record all tree nodes found by scanner. */ 137 @Override scan(JCTree tree)138 public void scan(JCTree tree) { 139 if (tree == null) 140 return; 141 //System.err.println("FOUND: " + tree.getTag() + " " + trim(tree, 64)); 142 found.add(tree); 143 super.scan(tree); 144 } 145 146 /** record all tree nodes found by reflection. */ reflectiveScan(Object o)147 public void reflectiveScan(Object o) { 148 if (o == null) 149 return; 150 if (o instanceof JCTree) { 151 JCTree tree = (JCTree) o; 152 //System.err.println("EXPECT: " + tree.getTag() + " " + trim(tree, 64)); 153 expect.add(tree); 154 for (Field f: getFields(tree)) { 155 try { 156 //System.err.println("FIELD: " + f.getName()); 157 reflectiveScan(f.get(tree)); 158 } catch (IllegalAccessException e) { 159 error(e.toString()); 160 } 161 } 162 } else if (o instanceof List) { 163 List<?> list = (List<?>) o; 164 for (Object item: list) 165 reflectiveScan(item); 166 } else if (o instanceof Pair) { 167 return; 168 } else 169 error("unexpected item: " + o); 170 } 171 172 JavaFileObject sourcefile; 173 Set<JCTree> found; 174 Set<JCTree> expect; 175 } 176 } 177