TestTreePath.java revision 2687:56f8be952a5c
159078Smdodd/*
259078Smdodd * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
359078Smdodd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
459078Smdodd *
559078Smdodd * This code is free software; you can redistribute it and/or modify it
659078Smdodd * under the terms of the GNU General Public License version 2 only, as
759078Smdodd * published by the Free Software Foundation.
859078Smdodd *
959078Smdodd * This code is distributed in the hope that it will be useful, but WITHOUT
1059078Smdodd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1159078Smdodd * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1259078Smdodd * version 2 for more details (a copy is included in the LICENSE file that
1359078Smdodd * accompanied this code).
1459078Smdodd *
1559078Smdodd * You should have received a copy of the GNU General Public License version
1659078Smdodd * 2 along with this work; if not, write to the Free Software Foundation,
1759078Smdodd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1859078Smdodd *
1959078Smdodd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2059078Smdodd * or visit www.oracle.com if you need additional information or have any
2159078Smdodd * questions.
2259078Smdodd */
2359078Smdodd
2459078Smdodd/*
2559078Smdodd * @test
2659078Smdodd * @bug 6473148
2759078Smdodd * @summary TreePath.iterator() throws NPE
28119418Sobrien */
29119418Sobrienimport java.io.*;
30119418Sobrienimport java.util.Arrays;
3159078Smdoddimport java.util.Iterator;
3259078Smdoddimport java.util.Set;
3359078Smdodd
34251164Sscottlimport javax.annotation.processing.*;
3559078Smdoddimport javax.lang.model.SourceVersion;
36251164Sscottlimport javax.lang.model.element.Element;
3759078Smdoddimport javax.lang.model.element.TypeElement;
3859078Smdoddimport javax.lang.model.util.ElementFilter;
3959078Smdoddimport javax.tools.JavaCompiler;
4059078Smdoddimport javax.tools.JavaFileObject;
4159078Smdoddimport javax.tools.StandardJavaFileManager;
4259078Smdoddimport javax.tools.ToolProvider;
4359078Smdodd
4459078Smdoddimport com.sun.source.tree.Tree;
4559078Smdoddimport com.sun.source.util.*;
4659078Smdodd
4759078Smdodd@SupportedAnnotationTypes("*")
4859078Smdoddpublic class TestTreePath extends AbstractProcessor {
49112795Smdodd
50112780Smdodd    @Override
51112795Smdodd    public boolean process(Set<? extends TypeElement> annotations,
5259078Smdodd            RoundEnvironment roundEnv) {
5359078Smdodd        final Trees trees = Trees.instance(this.processingEnv);
54112780Smdodd        for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) {
5559078Smdodd            checkTreePath(trees, element, 2);
56112780Smdodd            for (Element member : element.getEnclosedElements())
57112780Smdodd                checkTreePath(trees, member, 3);
58112780Smdodd        }
5959078Smdodd        return true;
60112780Smdodd    }
61112780Smdodd
62112780Smdodd    private void checkTreePath(Trees trees, Element element, int expectedLength) {
63112780Smdodd        TreePath path = trees.getPath(element);
64112780Smdodd        assert path != null;
65112780Smdodd
66112780Smdodd        int enhancedLength = 0;
67112780Smdodd        for (Tree tree : path)
68112780Smdodd            ++enhancedLength;
69112780Smdodd
70112780Smdodd        if (enhancedLength != expectedLength)
71112780Smdodd            throw new RuntimeException("found path length is wrong");
72112780Smdodd
73112780Smdodd        int normalLoopLength = 0;
74112780Smdodd        Iterator<Tree> iter = path.iterator();
75112780Smdodd        while (iter.hasNext()) {
76112780Smdodd          iter.next();
77112780Smdodd          ++normalLoopLength;
78112780Smdodd        }
79112780Smdodd        if (normalLoopLength != expectedLength)
80112780Smdodd            throw new RuntimeException("found path length is wrong");
81112780Smdodd
82112780Smdodd        TreePath curr = path;
83112780Smdodd        // using getParent
84112780Smdodd        int whileLoopLength = 0;
85112780Smdodd        while (curr != null) {
86112780Smdodd          ++whileLoopLength;
87112780Smdodd          curr = curr.getParentPath();
88112780Smdodd        }
89112795Smdodd        if (whileLoopLength != expectedLength)
90112780Smdodd            throw new RuntimeException("found path length is wrong");
91112780Smdodd    }
92112780Smdodd
93112780Smdodd    @Override
94112780Smdodd    public SourceVersion getSupportedSourceVersion() {
95112780Smdodd        return SourceVersion.latest();
96112780Smdodd    }
97112780Smdodd
98112780Smdodd    File writeTestFile() throws IOException {
99112780Smdodd        File f = new File("Test.java");
100112780Smdodd        PrintWriter out = new PrintWriter(new FileWriter(f));
101112780Smdodd        out.println("class Test { void method() { } }");
102112780Smdodd        out.close();
103112780Smdodd        return f;
104112780Smdodd    }
105112780Smdodd
106112780Smdodd    public void run() throws IOException {
107112780Smdodd        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
108112780Smdodd        try (StandardJavaFileManager fileManager
109112780Smdodd                = compiler.getStandardFileManager(null, null, null)) {
110112780Smdodd            Iterable<? extends JavaFileObject> tests
111112780Smdodd                = fileManager.getJavaFileObjects(writeTestFile());
112112780Smdodd
113112780Smdodd            JavaCompiler.CompilationTask task =
114112780Smdodd                ToolProvider.getSystemJavaCompiler().getTask(
115112780Smdodd                        null, null, null,
116112780Smdodd                        Arrays.asList("-processor", this.getClass().getName()), null,
117112795Smdodd                        tests);
118112780Smdodd            task.call();
119112780Smdodd        }
12059078Smdodd    }
12159078Smdodd
12259078Smdodd    public static void main(String[] args) throws IOException {
12359078Smdodd        new TestTreePath().run();
12459078Smdodd    }
12559078Smdodd}
12659078Smdodd