1 /*
   2  * Copyright (c) 2010, 2017, 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  */
  23 
  24 /**
  25  * Utility and test program to check javac's internal TreeScanner class.
  26  * The program can be run standalone, or as a jtreg test.  For info on
  27  * command line args, run program with no args.
  28  *
  29  * <p>
  30  * jtreg: Note that by using the -r switch in the test description below, this test
  31  * will process all java files in the langtools/test directory, thus implicitly
  32  * covering any new language features that may be tested in this test suite.
  33  */
  34 
  35 /*
  36  * @test
  37  * @bug 6923080
  38  * @summary TreeScanner.visitNewClass should scan tree.typeargs
  39  * @modules jdk.compiler/com.sun.tools.javac.api
  40  *          jdk.compiler/com.sun.tools.javac.file
  41  *          jdk.compiler/com.sun.tools.javac.tree
  42  *          jdk.compiler/com.sun.tools.javac.util
  43  * @build AbstractTreeScannerTest JavacTreeScannerTest
  44  * @run main JavacTreeScannerTest -q -r .
  45  */
  46 
  47 import java.io.*;
  48 import java.lang.reflect.*;
  49 import java.util.*;
  50 
  51 import javax.tools.*;
  52 
  53 import com.sun.tools.javac.tree.JCTree;
  54 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  55 import com.sun.tools.javac.tree.TreeScanner;
  56 import com.sun.tools.javac.util.List;
  57 import com.sun.tools.javac.util.Pair;
  58 
  59 public class JavacTreeScannerTest extends AbstractTreeScannerTest {
  60     /**
  61      * Main entry point.
  62      * If test.src is set, program runs in jtreg mode, and will throw an Error
  63      * if any errors arise, otherwise System.exit will be used. In jtreg mode,
  64      * the default base directory for file args is the value of ${test.src}.
  65      * In jtreg mode, the -r option can be given to change the default base
  66      * directory to the root test directory.
  67      */
  68     public static void main(String... args) {
  69         String testSrc = System.getProperty("test.src");
  70         File baseDir = (testSrc == null) ? null : new File(testSrc);
  71         boolean ok = new JavacTreeScannerTest().run(baseDir, args);
  72         if (!ok) {
  73             if (testSrc != null)  // jtreg mode
  74                 throw new Error("failed");
  75             else
  76                 System.exit(1);
  77         }
  78     }
  79 
  80     int test(JCCompilationUnit tree) {
  81         return new ScanTester().test(tree);
  82     }
  83 
  84     /**
  85      * Main class for testing operation of tree scanner.
  86      * The set of nodes found by the scanner are compared
  87      * against the set of nodes found by reflection.
  88      */
  89     private class ScanTester extends TreeScanner {
  90         /** Main entry method for the class. */
  91         int test(JCCompilationUnit tree) {
  92             sourcefile = tree.sourcefile;
  93             found = new HashSet<JCTree>();
  94             scan(tree);
  95             expect = new HashSet<JCTree>();
  96             reflectiveScan(tree);
  97 
  98             if (found.equals(expect)) {
  99                 //System.err.println(sourcefile.getName() + ": trees compared OK");
 100                 return found.size();
 101             }
 102 
 103             error(sourcefile, "differences found");
 104 
 105             if (found.size() != expect.size())
 106                 error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
 107 
 108             Set<JCTree> missing = new HashSet<JCTree>();
 109             missing.addAll(expect);
 110             missing.removeAll(found);
 111             for (JCTree t: missing)
 112                 error(sourcefile, t, "missing");
 113 
 114             Set<JCTree> excess = new HashSet<JCTree>();
 115             excess.addAll(found);
 116             excess.removeAll(expect);
 117             for (JCTree t: excess)
 118                 error(sourcefile, t, "unexpected");
 119 
 120             return 0;
 121         }
 122 
 123         /** Record all tree nodes found by scanner. */
 124         @Override
 125         public void scan(JCTree tree) {
 126             if (tree == null)
 127                 return;
 128             //System.err.println("FOUND: " + tree.getTag() + " " + trim(tree, 64));
 129             found.add(tree);
 130             super.scan(tree);
 131         }
 132 
 133         /** record all tree nodes found by reflection. */
 134         public void reflectiveScan(Object o) {
 135             if (o == null)
 136                 return;
 137             if (o instanceof JCTree) {
 138                 JCTree tree = (JCTree) o;
 139                 //System.err.println("EXPECT: " + tree.getTag() + " " + trim(tree, 64));
 140                 expect.add(tree);
 141                 for (Field f: getFields(tree)) {
 142                     try {
 143                         //System.err.println("FIELD: " + f.getName());
 144                         reflectiveScan(f.get(tree));
 145                     } catch (IllegalAccessException e) {
 146                         error(e.toString());
 147                     }
 148                 }
 149             } else if (o instanceof List) {
 150                 List<?> list = (List<?>) o;
 151                 for (Object item: list)
 152                     reflectiveScan(item);
 153             } else if (o instanceof Pair) {
 154                 return;
 155             } else
 156                 error("unexpected item: " + o);
 157         }
 158 
 159         JavaFileObject sourcefile;
 160         Set<JCTree> found;
 161         Set<JCTree> expect;
 162     }
 163 }