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