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

mercurial