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