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