test/tools/javac/tree/SourceTreeScannerTest.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 683
bbc9765d9ec6
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

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@683 94
jjg@679 95 if (found.equals(expect)) {
jjg@683 96 //System.err.println(sourcefile.getName() + ": trees compared OK");
jjg@679 97 return found.size();
jjg@679 98 }
jjg@489 99
jjg@683 100 error(sourcefile.getName() + ": differences found");
jjg@489 101
jjg@489 102 if (found.size() != expect.size())
jjg@489 103 error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
jjg@489 104
jjg@679 105 Set<Tree> missing = new HashSet<Tree>();
jjg@489 106 missing.addAll(expect);
jjg@489 107 missing.removeAll(found);
jjg@679 108 for (Tree t: missing)
jjg@683 109 error(sourcefile, t, "missing");
jjg@489 110
jjg@679 111 Set<Tree> excess = new HashSet<Tree>();
jjg@489 112 excess.addAll(found);
jjg@489 113 excess.removeAll(expect);
jjg@679 114 for (Tree t: excess)
jjg@683 115 error(sourcefile, t, "unexpected");
jjg@679 116
jjg@679 117 return 0;
jjg@489 118 }
jjg@489 119
jjg@489 120 /** Record all tree nodes found by scanner. */
jjg@489 121 @Override
jjg@679 122 public Void scan(Tree tree, Void ignore) {
jjg@489 123 if (tree == null)
jjg@679 124 return null;
jjg@683 125 //System.err.println("FOUND: " + tree.getKind() + " " + trim(tree, 64));
jjg@489 126 found.add(tree);
jjg@679 127 return super.scan(tree, ignore);
jjg@489 128 }
jjg@489 129
jjg@489 130 /** record all tree nodes found by reflection. */
jjg@489 131 public void reflectiveScan(Object o) {
jjg@489 132 if (o == null)
jjg@489 133 return;
jjg@489 134 if (o instanceof JCTree) {
jjg@489 135 JCTree tree = (JCTree) o;
jjg@683 136 //System.err.println("EXPECT: " + tree.getKind() + " " + trim(tree, 64));
jjg@489 137 expect.add(tree);
jjg@489 138 for (Field f: getFields(tree)) {
jjg@679 139 if (TypeBoundKind.class.isAssignableFrom(f.getType())) {
jjg@679 140 // not part of public API
jjg@679 141 continue;
jjg@679 142 }
jjg@679 143 if (JCTree.JCNewArray.class.isAssignableFrom(tree.getClass())
jjg@679 144 && (f.getName().equals("annotations")
jjg@679 145 || f.getName().equals("dimAnnotations"))) {
jjg@679 146 // these fields are incorrectly missing from the public API
jjg@679 147 // (CR 6983297)
jjg@679 148 continue;
jjg@679 149 }
jjg@489 150 try {
jjg@489 151 //System.err.println("FIELD: " + f.getName());
jjg@489 152 reflectiveScan(f.get(tree));
jjg@489 153 } catch (IllegalAccessException e) {
jjg@489 154 error(e.toString());
jjg@489 155 }
jjg@489 156 }
jjg@489 157 } else if (o instanceof List) {
jjg@489 158 List<?> list = (List<?>) o;
jjg@489 159 for (Object item: list)
jjg@489 160 reflectiveScan(item);
jjg@489 161 } else
jjg@489 162 error("unexpected item: " + o);
jjg@489 163 }
jjg@489 164
jjg@489 165 JavaFileObject sourcefile;
jjg@679 166 Set<Tree> found;
jjg@679 167 Set<Tree> expect;
jjg@489 168 }
jjg@489 169 }

mercurial