aoqi@0: /* aoqi@0: * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6473148 aoqi@0: * @summary TreePath.iterator() throws NPE aoqi@0: */ aoqi@0: import java.io.*; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.Iterator; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.annotation.processing.*; aoqi@0: import javax.lang.model.SourceVersion; aoqi@0: import javax.lang.model.element.Element; aoqi@0: import javax.lang.model.element.TypeElement; aoqi@0: import javax.lang.model.util.ElementFilter; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: import com.sun.source.tree.Tree; aoqi@0: import com.sun.source.util.*; aoqi@0: aoqi@0: @SupportedAnnotationTypes("*") aoqi@0: public class TestTreePath extends AbstractProcessor { aoqi@0: aoqi@0: @Override aoqi@0: public boolean process(Set annotations, aoqi@0: RoundEnvironment roundEnv) { aoqi@0: final Trees trees = Trees.instance(this.processingEnv); aoqi@0: for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) { aoqi@0: checkTreePath(trees, element, 2); aoqi@0: for (Element member : element.getEnclosedElements()) aoqi@0: checkTreePath(trees, member, 3); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: private void checkTreePath(Trees trees, Element element, int expectedLength) { aoqi@0: TreePath path = trees.getPath(element); aoqi@0: assert path != null; aoqi@0: aoqi@0: int enhancedLength = 0; aoqi@0: for (Tree tree : path) aoqi@0: ++enhancedLength; aoqi@0: aoqi@0: if (enhancedLength != expectedLength) aoqi@0: throw new RuntimeException("found path length is wrong"); aoqi@0: aoqi@0: int normalLoopLength = 0; aoqi@0: Iterator iter = path.iterator(); aoqi@0: while (iter.hasNext()) { aoqi@0: iter.next(); aoqi@0: ++normalLoopLength; aoqi@0: } aoqi@0: if (normalLoopLength != expectedLength) aoqi@0: throw new RuntimeException("found path length is wrong"); aoqi@0: aoqi@0: TreePath curr = path; aoqi@0: // using getParent aoqi@0: int whileLoopLength = 0; aoqi@0: while (curr != null) { aoqi@0: ++whileLoopLength; aoqi@0: curr = curr.getParentPath(); aoqi@0: } aoqi@0: if (whileLoopLength != expectedLength) aoqi@0: throw new RuntimeException("found path length is wrong"); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public SourceVersion getSupportedSourceVersion() { aoqi@0: return SourceVersion.latest(); aoqi@0: } aoqi@0: aoqi@0: File writeTestFile() throws IOException { aoqi@0: File f = new File("Test.java"); aoqi@0: PrintWriter out = new PrintWriter(new FileWriter(f)); aoqi@0: out.println("class Test { void method() { } }"); aoqi@0: out.close(); aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: public void run() throws IOException { aoqi@0: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fileManager aoqi@0: = compiler.getStandardFileManager(null, null, null); aoqi@0: Iterable tests aoqi@0: = fileManager.getJavaFileObjects(writeTestFile()); aoqi@0: aoqi@0: JavaCompiler.CompilationTask task = aoqi@0: ToolProvider.getSystemJavaCompiler().getTask( aoqi@0: null, null, null, aoqi@0: Arrays.asList("-processor", this.getClass().getName()), null, aoqi@0: tests); aoqi@0: task.call(); aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) throws IOException { aoqi@0: new TestTreePath().run(); aoqi@0: } aoqi@0: }