488N/A/*
553N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
488N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
488N/A *
488N/A * This code is free software; you can redistribute it and/or modify it
488N/A * under the terms of the GNU General Public License version 2 only, as
488N/A * published by the Free Software Foundation.
488N/A *
488N/A * This code is distributed in the hope that it will be useful, but WITHOUT
488N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
488N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
488N/A * version 2 for more details (a copy is included in the LICENSE file that
488N/A * accompanied this code).
488N/A *
488N/A * You should have received a copy of the GNU General Public License version
488N/A * 2 along with this work; if not, write to the Free Software Foundation,
488N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
488N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
488N/A */
488N/A
488N/A
488N/A/**
488N/A * Utility and test program to check javac's internal TreeScanner class.
488N/A * The program can be run standalone, or as a jtreg test. For info on
488N/A * command line args, run program with no args.
488N/A *
488N/A * <p>
488N/A * jtreg: Note that by using the -r switch in the test description below, this test
488N/A * will process all java files in the langtools/test directory, thus implicitly
488N/A * covering any new language features that may be tested in this test suite.
488N/A */
488N/A
488N/A/*
488N/A * @test
488N/A * @bug 6923080
488N/A * @summary TreeScanner.visitNewClass should scan tree.typeargs
678N/A * @build AbstractTreeScannerTest SourceTreeScannerTest
678N/A * @run main SourceTreeScannerTest -q -r .
488N/A */
488N/A
488N/Aimport java.io.*;
488N/Aimport java.lang.reflect.*;
488N/Aimport java.util.*;
488N/Aimport javax.tools.*;
488N/A
678N/Aimport com.sun.source.tree.Tree;
678N/Aimport com.sun.source.util.TreeScanner;
678N/Aimport com.sun.tools.javac.tree.JCTree;
678N/Aimport com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
678N/Aimport com.sun.tools.javac.tree.JCTree.TypeBoundKind;
488N/Aimport com.sun.tools.javac.util.List;
488N/A
678N/Apublic class SourceTreeScannerTest extends AbstractTreeScannerTest {
488N/A /**
488N/A * Main entry point.
488N/A * If test.src is set, program runs in jtreg mode, and will throw an Error
488N/A * if any errors arise, otherwise System.exit will be used. In jtreg mode,
488N/A * the default base directory for file args is the value of ${test.src}.
488N/A * In jtreg mode, the -r option can be given to change the default base
488N/A * directory to the root test directory.
488N/A */
488N/A public static void main(String... args) {
488N/A String testSrc = System.getProperty("test.src");
488N/A File baseDir = (testSrc == null) ? null : new File(testSrc);
678N/A boolean ok = new SourceTreeScannerTest().run(baseDir, args);
488N/A if (!ok) {
488N/A if (testSrc != null) // jtreg mode
488N/A throw new Error("failed");
488N/A else
488N/A System.exit(1);
488N/A }
488N/A }
488N/A
678N/A int test(JCCompilationUnit tree) {
678N/A return new ScanTester().test(tree);
488N/A }
488N/A
488N/A /**
488N/A * Main class for testing operation of tree scanner.
488N/A * The set of nodes found by the scanner are compared
488N/A * against the set of nodes found by reflection.
488N/A */
678N/A private class ScanTester extends TreeScanner<Void,Void> {
488N/A /** Main entry method for the class. */
678N/A int test(JCCompilationUnit tree) {
488N/A sourcefile = tree.sourcefile;
678N/A found = new HashSet<Tree>();
678N/A scan(tree, null);
678N/A expect = new HashSet<Tree>();
488N/A reflectiveScan(tree);
682N/A
678N/A if (found.equals(expect)) {
682N/A //System.err.println(sourcefile.getName() + ": trees compared OK");
678N/A return found.size();
678N/A }
488N/A
682N/A error(sourcefile.getName() + ": differences found");
488N/A
488N/A if (found.size() != expect.size())
488N/A error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
488N/A
678N/A Set<Tree> missing = new HashSet<Tree>();
488N/A missing.addAll(expect);
488N/A missing.removeAll(found);
678N/A for (Tree t: missing)
682N/A error(sourcefile, t, "missing");
488N/A
678N/A Set<Tree> excess = new HashSet<Tree>();
488N/A excess.addAll(found);
488N/A excess.removeAll(expect);
678N/A for (Tree t: excess)
682N/A error(sourcefile, t, "unexpected");
678N/A
678N/A return 0;
488N/A }
488N/A
488N/A /** Record all tree nodes found by scanner. */
488N/A @Override
678N/A public Void scan(Tree tree, Void ignore) {
488N/A if (tree == null)
678N/A return null;
682N/A //System.err.println("FOUND: " + tree.getKind() + " " + trim(tree, 64));
488N/A found.add(tree);
678N/A return super.scan(tree, ignore);
488N/A }
488N/A
488N/A /** record all tree nodes found by reflection. */
488N/A public void reflectiveScan(Object o) {
488N/A if (o == null)
488N/A return;
488N/A if (o instanceof JCTree) {
488N/A JCTree tree = (JCTree) o;
682N/A //System.err.println("EXPECT: " + tree.getKind() + " " + trim(tree, 64));
488N/A expect.add(tree);
488N/A for (Field f: getFields(tree)) {
678N/A if (TypeBoundKind.class.isAssignableFrom(f.getType())) {
678N/A // not part of public API
678N/A continue;
678N/A }
678N/A if (JCTree.JCNewArray.class.isAssignableFrom(tree.getClass())
678N/A && (f.getName().equals("annotations")
678N/A || f.getName().equals("dimAnnotations"))) {
678N/A // these fields are incorrectly missing from the public API
678N/A // (CR 6983297)
678N/A continue;
678N/A }
488N/A try {
488N/A //System.err.println("FIELD: " + f.getName());
488N/A reflectiveScan(f.get(tree));
488N/A } catch (IllegalAccessException e) {
488N/A error(e.toString());
488N/A }
488N/A }
488N/A } else if (o instanceof List) {
488N/A List<?> list = (List<?>) o;
488N/A for (Object item: list)
488N/A reflectiveScan(item);
488N/A } else
488N/A error("unexpected item: " + o);
488N/A }
488N/A
488N/A JavaFileObject sourcefile;
678N/A Set<Tree> found;
678N/A Set<Tree> expect;
488N/A }
488N/A}