test/tools/javac/tree/JavacTreeScannerTest.java

Thu, 09 Sep 2010 13:31:28 -0700

author
jjg
date
Thu, 09 Sep 2010 13:31:28 -0700
changeset 679
fc73f83cd563
parent 554
test/tools/javac/tree/TreeScannerTest.java@9d9f26857129
child 683
bbc9765d9ec6
permissions
-rw-r--r--

6983239: TreeScanner does not scan default value for method
Reviewed-by: mcimadamore

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

mercurial