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