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 6473148 27 * @summary TreePath.iterator() throws NPE 28 * @modules jdk.compiler 29 */ 30import java.io.*; 31import java.util.Arrays; 32import java.util.Iterator; 33import java.util.Set; 34 35import javax.annotation.processing.*; 36import javax.lang.model.SourceVersion; 37import javax.lang.model.element.Element; 38import javax.lang.model.element.TypeElement; 39import javax.lang.model.util.ElementFilter; 40import javax.tools.JavaCompiler; 41import javax.tools.JavaFileObject; 42import javax.tools.StandardJavaFileManager; 43import javax.tools.ToolProvider; 44 45import com.sun.source.tree.Tree; 46import com.sun.source.util.*; 47 48@SupportedAnnotationTypes("*") 49public class TestTreePath extends AbstractProcessor { 50 51 @Override 52 public boolean process(Set<? extends TypeElement> annotations, 53 RoundEnvironment roundEnv) { 54 final Trees trees = Trees.instance(this.processingEnv); 55 for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) { 56 checkTreePath(trees, element, 2); 57 for (Element member : element.getEnclosedElements()) 58 checkTreePath(trees, member, 3); 59 } 60 return true; 61 } 62 63 private void checkTreePath(Trees trees, Element element, int expectedLength) { 64 TreePath path = trees.getPath(element); 65 assert path != null; 66 67 int enhancedLength = 0; 68 for (Tree tree : path) 69 ++enhancedLength; 70 71 if (enhancedLength != expectedLength) 72 throw new RuntimeException("found path length is wrong"); 73 74 int normalLoopLength = 0; 75 Iterator<Tree> iter = path.iterator(); 76 while (iter.hasNext()) { 77 iter.next(); 78 ++normalLoopLength; 79 } 80 if (normalLoopLength != expectedLength) 81 throw new RuntimeException("found path length is wrong"); 82 83 TreePath curr = path; 84 // using getParent 85 int whileLoopLength = 0; 86 while (curr != null) { 87 ++whileLoopLength; 88 curr = curr.getParentPath(); 89 } 90 if (whileLoopLength != expectedLength) 91 throw new RuntimeException("found path length is wrong"); 92 } 93 94 @Override 95 public SourceVersion getSupportedSourceVersion() { 96 return SourceVersion.latest(); 97 } 98 99 File writeTestFile() throws IOException { 100 File f = new File("Test.java"); 101 PrintWriter out = new PrintWriter(new FileWriter(f)); 102 out.println("class Test { void method() { } }"); 103 out.close(); 104 return f; 105 } 106 107 public void run() throws IOException { 108 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 109 try (StandardJavaFileManager fileManager 110 = compiler.getStandardFileManager(null, null, null)) { 111 Iterable<? extends JavaFileObject> tests 112 = fileManager.getJavaFileObjects(writeTestFile()); 113 114 JavaCompiler.CompilationTask task = 115 ToolProvider.getSystemJavaCompiler().getTask( 116 null, null, null, 117 Arrays.asList("-processor", this.getClass().getName()), null, 118 tests); 119 task.call(); 120 } 121 } 122 123 public static void main(String[] args) throws IOException { 124 new TestTreePath().run(); 125 } 126} 127