|
1 /* |
|
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any 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 // 383 nodes with the regular parser; 384 nodes with EndPosParser |
|
51 // We automatically swith to EndPosParser when calling JavacTask.parse() |
|
52 check(nodeCounter, 384, nodeCounter.scan(trees, null)); |
|
53 |
|
54 CountIdentifiers idCounter = new CountIdentifiers(); |
|
55 check(idCounter, 106, 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 // example from TreeScanner javadoc |
|
77 static class CountNodes extends TreeScanner<Integer,Void> { |
|
78 @Override |
|
79 public Integer scan(Tree node, Void p) { |
|
80 Integer n = super.scan(node, p); |
|
81 return (n == null ? 0 : n) + 1; |
|
82 } |
|
83 @Override |
|
84 public Integer reduce(Integer r1, Integer r2) { |
|
85 return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); |
|
86 } |
|
87 } |
|
88 |
|
89 // example from TreeScanner javadoc |
|
90 static class CountIdentifiers extends TreeScanner<Integer,Void> { |
|
91 @Override |
|
92 public Integer visitIdentifier(IdentifierTree node, Void p) { |
|
93 return 1; |
|
94 } |
|
95 @Override |
|
96 public Integer reduce(Integer r1, Integer r2) { |
|
97 return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); |
|
98 } |
|
99 } |
|
100 } |