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

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

mercurial