1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/T6392782.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,100 @@ 1.4 +/* 1.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6392782 1.30 + * @summary TreeScanner.visitImport returns null, not result of nested scan 1.31 + */ 1.32 + 1.33 +import java.io.*; 1.34 +import java.util.*; 1.35 +import javax.tools.*; 1.36 +import com.sun.source.tree.*; 1.37 +import com.sun.source.util.*; 1.38 +import com.sun.tools.javac.api.*; 1.39 + 1.40 +public class T6392782 { 1.41 + public static void main(String... args) throws IOException { 1.42 + String testSrc = System.getProperty("test.src", "."); 1.43 + JavacTool tool = JavacTool.create(); 1.44 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.45 + Iterable<? extends JavaFileObject> files = 1.46 + fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6392782.class.getName()+".java"))); 1.47 + JavacTask task = tool.getTask(null, fm, null, null, null, files); 1.48 + Iterable<? extends Tree> trees = task.parse(); 1.49 + TreeScanner<Integer,Void> scanner = new MyScanner(); 1.50 + check(scanner, 6, scanner.scan(trees, null)); 1.51 + 1.52 + CountNodes nodeCounter = new CountNodes(); 1.53 + // 383 nodes with the regular parser; 384 nodes with EndPosParser 1.54 + // We automatically swith to EndPosParser when calling JavacTask.parse() 1.55 + check(nodeCounter, 384, nodeCounter.scan(trees, null)); 1.56 + 1.57 + CountIdentifiers idCounter = new CountIdentifiers(); 1.58 + check(idCounter, 106, idCounter.scan(trees, null)); 1.59 + } 1.60 + 1.61 + private static void check(TreeScanner<?,?> scanner, int expect, int found) { 1.62 + if (found != expect) 1.63 + throw new AssertionError(scanner.getClass().getName() + ": expected: " + expect + " found: " + found); 1.64 + } 1.65 + 1.66 + static class MyScanner extends TreeScanner<Integer,Void> { 1.67 + @Override 1.68 + public Integer visitImport(ImportTree tree, Void ignore) { 1.69 + //System.err.println(tree); 1.70 + return 1; 1.71 + } 1.72 + 1.73 + @Override 1.74 + public Integer reduce(Integer i1, Integer i2) { 1.75 + return (i1 == null ? 0 : i1) + (i2 == null ? 0 : i2); 1.76 + } 1.77 + } 1.78 + 1.79 + // example from TreeScanner javadoc 1.80 + static class CountNodes extends TreeScanner<Integer,Void> { 1.81 + @Override 1.82 + public Integer scan(Tree node, Void p) { 1.83 + Integer n = super.scan(node, p); 1.84 + return (n == null ? 0 : n) + 1; 1.85 + } 1.86 + @Override 1.87 + public Integer reduce(Integer r1, Integer r2) { 1.88 + return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 1.89 + } 1.90 + } 1.91 + 1.92 + // example from TreeScanner javadoc 1.93 + static class CountIdentifiers extends TreeScanner<Integer,Void> { 1.94 + @Override 1.95 + public Integer visitIdentifier(IdentifierTree node, Void p) { 1.96 + return 1; 1.97 + } 1.98 + @Override 1.99 + public Integer reduce(Integer r1, Integer r2) { 1.100 + return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 1.101 + } 1.102 + } 1.103 +}