langtools/test/tools/javac/api/T6392782.java
author darcy
Wed, 01 Jun 2011 23:56:31 -0700
changeset 10180 b293c1f36ac4
parent 7681 1f0819a3341f
child 27319 030080f03e4f
permissions -rw-r--r--
7025784: Add SourceVersion.RELEASE_8 7025786: Add -source 8 and -target 8 to javac 7025789: Change javac source and target default to 8 Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
7681
1f0819a3341f 6962318: Update copyright year
ohair
parents: 6597
diff changeset
     2
 * Copyright (c) 2006, 2010, 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();
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
        Iterable<? extends JavaFileObject> files =
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
            fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6392782.class.getName()+".java")));
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        JavacTask task = tool.getTask(null, fm, null, null, null, files);
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        Iterable<? extends Tree> trees = task.parse();
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        TreeScanner<Integer,Void> scanner = new MyScanner();
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        check(scanner, 6, scanner.scan(trees, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        CountNodes nodeCounter = new CountNodes();
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    50
        // 359 nodes with the regular parser; 360 nodes with EndPosParser
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    51
        // We automatically switch to EndPosParser when calling JavacTask.parse()
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    52
        check(nodeCounter, 360, nodeCounter.scan(trees, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        CountIdentifiers idCounter = new CountIdentifiers();
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    55
        check(idCounter, 107, idCounter.scan(trees, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private static void check(TreeScanner<?,?> scanner, int expect, int found) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        if (found != expect)
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            throw new AssertionError(scanner.getClass().getName() + ": expected: " + expect + " found: " + found);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    static class MyScanner extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        public Integer visitImport(ImportTree tree, Void ignore) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            //System.err.println(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            return 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        public Integer reduce(Integer i1, Integer i2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            return (i1 == null ? 0 : i1) + (i2 == null ? 0 : i2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    static class CountNodes extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        public Integer scan(Tree node, Void p) {
6597
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    79
            if (node == null)
9367c22c445f 6983239: TreeScanner does not scan default value for method
jjg
parents: 5520
diff changeset
    80
                return 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            Integer n = super.scan(node, p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            return (n == null ? 0 : n) + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        public Integer reduce(Integer r1, Integer r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    // example from TreeScanner javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    static class CountIdentifiers extends TreeScanner<Integer,Void> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        public Integer visitIdentifier(IdentifierTree node, Void p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            return 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        public Integer reduce(Integer r1, Integer r2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
}