test/tools/javac/api/T6392782.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2006, 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 */
23
24 /*
25 * @test
26 * @bug 6392782
27 * @summary TreeScanner.visitImport returns null, not result of nested scan
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import javax.tools.*;
33 import com.sun.source.tree.*;
34 import com.sun.source.util.*;
35 import com.sun.tools.javac.api.*;
36
37 public class T6392782 {
38 public static void main(String... args) throws IOException {
39 String testSrc = System.getProperty("test.src", ".");
40 JavacTool tool = JavacTool.create();
41 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
42 Iterable<? extends JavaFileObject> files =
43 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6392782.class.getName()+".java")));
44 JavacTask task = tool.getTask(null, fm, null, null, null, files);
45 Iterable<? extends Tree> trees = task.parse();
46 TreeScanner<Integer,Void> scanner = new MyScanner();
47 check(scanner, 6, scanner.scan(trees, null));
48
49 CountNodes nodeCounter = new CountNodes();
50 // 359 nodes with the regular parser; 360 nodes with EndPosParser
51 // We automatically switch to EndPosParser when calling JavacTask.parse()
52 check(nodeCounter, 360, nodeCounter.scan(trees, null));
53
54 CountIdentifiers idCounter = new CountIdentifiers();
55 check(idCounter, 107, idCounter.scan(trees, null));
56 }
57
58 private static void check(TreeScanner<?,?> scanner, int expect, int found) {
59 if (found != expect)
60 throw new AssertionError(scanner.getClass().getName() + ": expected: " + expect + " found: " + found);
61 }
62
63 static class MyScanner extends TreeScanner<Integer,Void> {
64 @Override
65 public Integer visitImport(ImportTree tree, Void ignore) {
66 //System.err.println(tree);
67 return 1;
68 }
69
70 @Override
71 public Integer reduce(Integer i1, Integer i2) {
72 return (i1 == null ? 0 : i1) + (i2 == null ? 0 : i2);
73 }
74 }
75
76 static class CountNodes extends TreeScanner<Integer,Void> {
77 @Override
78 public Integer scan(Tree node, Void p) {
79 if (node == null)
80 return 0;
81 Integer n = super.scan(node, p);
82 return (n == null ? 0 : n) + 1;
83 }
84 @Override
85 public Integer reduce(Integer r1, Integer r2) {
86 return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
87 }
88 }
89
90 // example from TreeScanner javadoc
91 static class CountIdentifiers extends TreeScanner<Integer,Void> {
92 @Override
93 public Integer visitIdentifier(IdentifierTree node, Void p) {
94 return 1;
95 }
96 @Override
97 public Integer reduce(Integer r1, Integer r2) {
98 return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2);
99 }
100 }
101 }

mercurial