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