langtools/test/tools/javac/api/T6392782.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 7681 1f0819a3341f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6392782
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary TreeScanner.visitImport returns null, not result of nested scan
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.api.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
public class T6392782 {
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    public static void main(String... args) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
        String testSrc = System.getProperty("test.src", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
        JavacTool tool = JavacTool.create();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    41
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    42
            Iterable<? extends JavaFileObject> files =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    43
                fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6392782.class.getName()+".java")));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    44
            JavacTask task = tool.getTask(null, fm, null, null, null, files);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    45
            Iterable<? extends Tree> trees = task.parse();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    46
            TreeScanner<Integer,Void> scanner = new MyScanner();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    47
            check(scanner, 6, scanner.scan(trees, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    49
            CountNodes nodeCounter = new CountNodes();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    50
            // 359 nodes with the regular parser; 360 nodes with EndPosParser
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    51
            // We automatically switch to EndPosParser when calling JavacTask.parse()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    52
            check(nodeCounter, 362, nodeCounter.scan(trees, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    54
            CountIdentifiers idCounter = new CountIdentifiers();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    55
            check(idCounter, 107, idCounter.scan(trees, null));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7681
diff changeset
    56
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private static void check(TreeScanner<?,?> scanner, int expect, int found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        if (found != expect)
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            throw new AssertionError(scanner.getClass().getName() + ": expected: " + expect + " found: " + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    static class MyScanner extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        public Integer visitImport(ImportTree tree, Void ignore) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            //System.err.println(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            return 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        public Integer reduce(Integer i1, Integer i2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            return (i1 == null ? 0 : i1) + (i2 == null ? 0 : i2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    static class CountNodes extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        public Integer scan(Tree node, Void p) {
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    80
            if (node == null)
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    81
                return 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            Integer n = super.scan(node, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            return (n == null ? 0 : n) + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        public Integer reduce(Integer r1, Integer r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    // example from TreeScanner javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    static class CountIdentifiers extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        public Integer visitIdentifier(IdentifierTree node, Void p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            return 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        public Integer reduce(Integer r1, Integer r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
}