jjg@489: /* ohair@554: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@489: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@489: * jjg@489: * This code is free software; you can redistribute it and/or modify it jjg@489: * under the terms of the GNU General Public License version 2 only, as jjg@489: * published by the Free Software Foundation. jjg@489: * jjg@489: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@489: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@489: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@489: * version 2 for more details (a copy is included in the LICENSE file that jjg@489: * accompanied this code). jjg@489: * jjg@489: * You should have received a copy of the GNU General Public License version jjg@489: * 2 along with this work; if not, write to the Free Software Foundation, jjg@489: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@489: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@489: */ jjg@489: jjg@489: jjg@489: /** jjg@489: * Utility and test program to check javac's internal TreeScanner class. jjg@489: * The program can be run standalone, or as a jtreg test. For info on jjg@489: * command line args, run program with no args. jjg@489: * jjg@489: *

jjg@489: * jtreg: Note that by using the -r switch in the test description below, this test jjg@489: * will process all java files in the langtools/test directory, thus implicitly jjg@489: * covering any new language features that may be tested in this test suite. jjg@489: */ jjg@489: jjg@489: /* jjg@489: * @test jjg@489: * @bug 6923080 jjg@489: * @summary TreeScanner.visitNewClass should scan tree.typeargs jjg@679: * @build AbstractTreeScannerTest SourceTreeScannerTest jjg@679: * @run main SourceTreeScannerTest -q -r . jjg@489: */ jjg@489: jjg@489: import java.io.*; jjg@489: import java.lang.reflect.*; jjg@489: import java.util.*; jjg@489: import javax.tools.*; jjg@489: jjg@679: import com.sun.source.tree.Tree; jjg@679: import com.sun.source.util.TreeScanner; jjg@679: import com.sun.tools.javac.tree.JCTree; jjg@679: import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; jjg@679: import com.sun.tools.javac.tree.JCTree.TypeBoundKind; jjg@489: import com.sun.tools.javac.util.List; jjg@489: jjg@679: public class SourceTreeScannerTest extends AbstractTreeScannerTest { jjg@489: /** jjg@489: * Main entry point. jjg@489: * If test.src is set, program runs in jtreg mode, and will throw an Error jjg@489: * if any errors arise, otherwise System.exit will be used. In jtreg mode, jjg@489: * the default base directory for file args is the value of ${test.src}. jjg@489: * In jtreg mode, the -r option can be given to change the default base jjg@489: * directory to the root test directory. jjg@489: */ jjg@489: public static void main(String... args) { jjg@489: String testSrc = System.getProperty("test.src"); jjg@489: File baseDir = (testSrc == null) ? null : new File(testSrc); jjg@679: boolean ok = new SourceTreeScannerTest().run(baseDir, args); jjg@489: if (!ok) { jjg@489: if (testSrc != null) // jtreg mode jjg@489: throw new Error("failed"); jjg@489: else jjg@489: System.exit(1); jjg@489: } jjg@489: } jjg@489: jjg@679: int test(JCCompilationUnit tree) { jjg@679: return new ScanTester().test(tree); jjg@489: } jjg@489: jjg@489: /** jjg@489: * Main class for testing operation of tree scanner. jjg@489: * The set of nodes found by the scanner are compared jjg@489: * against the set of nodes found by reflection. jjg@489: */ jjg@679: private class ScanTester extends TreeScanner { jjg@489: /** Main entry method for the class. */ jjg@679: int test(JCCompilationUnit tree) { jjg@489: sourcefile = tree.sourcefile; jjg@679: found = new HashSet(); jjg@679: scan(tree, null); jjg@679: expect = new HashSet(); jjg@489: reflectiveScan(tree); jjg@683: jjg@679: if (found.equals(expect)) { jjg@683: //System.err.println(sourcefile.getName() + ": trees compared OK"); jjg@679: return found.size(); jjg@679: } jjg@489: jjg@683: error(sourcefile.getName() + ": differences found"); jjg@489: jjg@489: if (found.size() != expect.size()) jjg@489: error("Size mismatch; found: " + found.size() + ", expected: " + expect.size()); jjg@489: jjg@679: Set missing = new HashSet(); jjg@489: missing.addAll(expect); jjg@489: missing.removeAll(found); jjg@679: for (Tree t: missing) jjg@683: error(sourcefile, t, "missing"); jjg@489: jjg@679: Set excess = new HashSet(); jjg@489: excess.addAll(found); jjg@489: excess.removeAll(expect); jjg@679: for (Tree t: excess) jjg@683: error(sourcefile, t, "unexpected"); jjg@679: jjg@679: return 0; jjg@489: } jjg@489: jjg@489: /** Record all tree nodes found by scanner. */ jjg@489: @Override jjg@679: public Void scan(Tree tree, Void ignore) { jjg@489: if (tree == null) jjg@679: return null; jjg@683: //System.err.println("FOUND: " + tree.getKind() + " " + trim(tree, 64)); jjg@489: found.add(tree); jjg@679: return super.scan(tree, ignore); jjg@489: } jjg@489: jjg@489: /** record all tree nodes found by reflection. */ jjg@489: public void reflectiveScan(Object o) { jjg@489: if (o == null) jjg@489: return; jjg@489: if (o instanceof JCTree) { jjg@489: JCTree tree = (JCTree) o; jjg@683: //System.err.println("EXPECT: " + tree.getKind() + " " + trim(tree, 64)); jjg@489: expect.add(tree); jjg@489: for (Field f: getFields(tree)) { jjg@679: if (TypeBoundKind.class.isAssignableFrom(f.getType())) { jjg@679: // not part of public API jjg@679: continue; jjg@679: } jjg@679: if (JCTree.JCNewArray.class.isAssignableFrom(tree.getClass()) jjg@679: && (f.getName().equals("annotations") jjg@679: || f.getName().equals("dimAnnotations"))) { jjg@679: // these fields are incorrectly missing from the public API jjg@679: // (CR 6983297) jjg@679: continue; jjg@679: } jjg@489: try { jjg@489: //System.err.println("FIELD: " + f.getName()); jjg@489: reflectiveScan(f.get(tree)); jjg@489: } catch (IllegalAccessException e) { jjg@489: error(e.toString()); jjg@489: } jjg@489: } jjg@489: } else if (o instanceof List) { jjg@489: List list = (List) o; jjg@489: for (Object item: list) jjg@489: reflectiveScan(item); jjg@489: } else jjg@489: error("unexpected item: " + o); jjg@489: } jjg@489: jjg@489: JavaFileObject sourcefile; jjg@679: Set found; jjg@679: Set expect; jjg@489: } jjg@489: }