1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/TestTreePath.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,124 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6473148 1.30 + * @summary TreePath.iterator() throws NPE 1.31 + */ 1.32 +import java.io.*; 1.33 +import java.util.Arrays; 1.34 +import java.util.Iterator; 1.35 +import java.util.Set; 1.36 + 1.37 +import javax.annotation.processing.*; 1.38 +import javax.lang.model.SourceVersion; 1.39 +import javax.lang.model.element.Element; 1.40 +import javax.lang.model.element.TypeElement; 1.41 +import javax.lang.model.util.ElementFilter; 1.42 +import javax.tools.JavaCompiler; 1.43 +import javax.tools.JavaFileObject; 1.44 +import javax.tools.StandardJavaFileManager; 1.45 +import javax.tools.ToolProvider; 1.46 + 1.47 +import com.sun.source.tree.Tree; 1.48 +import com.sun.source.util.*; 1.49 + 1.50 +@SupportedAnnotationTypes("*") 1.51 +public class TestTreePath extends AbstractProcessor { 1.52 + 1.53 + @Override 1.54 + public boolean process(Set<? extends TypeElement> annotations, 1.55 + RoundEnvironment roundEnv) { 1.56 + final Trees trees = Trees.instance(this.processingEnv); 1.57 + for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) { 1.58 + checkTreePath(trees, element, 2); 1.59 + for (Element member : element.getEnclosedElements()) 1.60 + checkTreePath(trees, member, 3); 1.61 + } 1.62 + return true; 1.63 + } 1.64 + 1.65 + private void checkTreePath(Trees trees, Element element, int expectedLength) { 1.66 + TreePath path = trees.getPath(element); 1.67 + assert path != null; 1.68 + 1.69 + int enhancedLength = 0; 1.70 + for (Tree tree : path) 1.71 + ++enhancedLength; 1.72 + 1.73 + if (enhancedLength != expectedLength) 1.74 + throw new RuntimeException("found path length is wrong"); 1.75 + 1.76 + int normalLoopLength = 0; 1.77 + Iterator<Tree> iter = path.iterator(); 1.78 + while (iter.hasNext()) { 1.79 + iter.next(); 1.80 + ++normalLoopLength; 1.81 + } 1.82 + if (normalLoopLength != expectedLength) 1.83 + throw new RuntimeException("found path length is wrong"); 1.84 + 1.85 + TreePath curr = path; 1.86 + // using getParent 1.87 + int whileLoopLength = 0; 1.88 + while (curr != null) { 1.89 + ++whileLoopLength; 1.90 + curr = curr.getParentPath(); 1.91 + } 1.92 + if (whileLoopLength != expectedLength) 1.93 + throw new RuntimeException("found path length is wrong"); 1.94 + } 1.95 + 1.96 + @Override 1.97 + public SourceVersion getSupportedSourceVersion() { 1.98 + return SourceVersion.latest(); 1.99 + } 1.100 + 1.101 + File writeTestFile() throws IOException { 1.102 + File f = new File("Test.java"); 1.103 + PrintWriter out = new PrintWriter(new FileWriter(f)); 1.104 + out.println("class Test { void method() { } }"); 1.105 + out.close(); 1.106 + return f; 1.107 + } 1.108 + 1.109 + public void run() throws IOException { 1.110 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.111 + StandardJavaFileManager fileManager 1.112 + = compiler.getStandardFileManager(null, null, null); 1.113 + Iterable<? extends JavaFileObject> tests 1.114 + = fileManager.getJavaFileObjects(writeTestFile()); 1.115 + 1.116 + JavaCompiler.CompilationTask task = 1.117 + ToolProvider.getSystemJavaCompiler().getTask( 1.118 + null, null, null, 1.119 + Arrays.asList("-processor", this.getClass().getName()), null, 1.120 + tests); 1.121 + task.call(); 1.122 + } 1.123 + 1.124 + public static void main(String[] args) throws IOException { 1.125 + new TestTreePath().run(); 1.126 + } 1.127 +}