author | darcy |
Tue, 05 Jul 2011 16:37:24 -0700 | |
changeset 10192 | 378321489bea |
parent 9599 | 0996df19ea87 |
child 14963 | 974d4423c999 |
permissions | -rw-r--r-- |
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
1 |
/* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
2 |
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
4 |
* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
8 |
* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
14 |
* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
18 |
* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
21 |
* questions. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
22 |
*/ |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
23 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
24 |
/* |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
25 |
* @test |
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
9599
diff
changeset
|
26 |
* @bug 7029150 7025809 |
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
27 |
* @summary Test support for union types |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
28 |
* @library ../../../lib |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
29 |
*/ |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
30 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
31 |
import java.net.URI; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
32 |
import java.util.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
33 |
import javax.annotation.processing.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
34 |
import javax.lang.model.element.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
35 |
import javax.lang.model.type.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
36 |
import javax.lang.model.util.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
37 |
import javax.tools.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
38 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
39 |
import com.sun.source.tree.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
40 |
import com.sun.source.util.*; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
41 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
42 |
public class TestUnionType extends JavacTestingAbstractProcessor { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
43 |
enum TestKind { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
44 |
SingleType("E1", "E1", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
45 |
"VariableTree: E1 e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
46 |
"VariableTree: elem EXCEPTION_PARAMETER e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
47 |
"VariableTree: elem.type DECLARED", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
48 |
"VariableTree: elem.type.elem CLASS E1", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
49 |
"VariableTree: type DECLARED", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
50 |
"VariableTree: type.elem CLASS E1", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
51 |
"VariableTree: type.elem.type DECLARED"), |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
52 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
53 |
ValidTypes("E1, E2", "E1 | E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
54 |
"VariableTree: E1 | E2 e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
55 |
"VariableTree: elem EXCEPTION_PARAMETER e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
56 |
"VariableTree: elem.type UNION Test.E1,Test.E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
57 |
"VariableTree: elem.type.elem null", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
58 |
"VariableTree: type UNION Test.E1,Test.E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
59 |
"VariableTree: type.elem null"), |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
60 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
61 |
InvalidTypes("E1, E2", "E1 | EMissing", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
62 |
"VariableTree: E1 | EMissing e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
63 |
"VariableTree: elem EXCEPTION_PARAMETER e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
64 |
"VariableTree: elem.type UNION Test.E1,EMissing", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
65 |
"VariableTree: elem.type.elem null", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
66 |
"VariableTree: type UNION Test.E1,EMissing", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
67 |
"VariableTree: type.elem null"), |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
68 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
69 |
Uncaught("E1", "E1 | E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
70 |
"VariableTree: E1 | E2 e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
71 |
"VariableTree: elem EXCEPTION_PARAMETER e", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
72 |
"VariableTree: elem.type UNION Test.E1,Test.E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
73 |
"VariableTree: elem.type.elem null", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
74 |
"VariableTree: type UNION Test.E1,Test.E2", |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
75 |
"VariableTree: type.elem null"); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
76 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
77 |
TestKind(String throwsTypes, String catchTypes, String... gold) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
78 |
this.throwsTypes = throwsTypes; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
79 |
this.catchTypes = catchTypes; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
80 |
this.gold = Arrays.asList(gold); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
81 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
82 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
83 |
final String throwsTypes; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
84 |
final String catchTypes; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
85 |
final List<String> gold; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
86 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
87 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
88 |
static class TestFileObject extends SimpleJavaFileObject { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
89 |
public static final String template = |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
90 |
"class Test {\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
91 |
+ " class E1 extends Exception { }\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
92 |
+ " class E2 extends Exception { }\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
93 |
+ " void doSomething() throws #T { }\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
94 |
+ " void test() {\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
95 |
+ " try {\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
96 |
+ " doSomething();\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
97 |
+ " } catch (#C e) {\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
98 |
+ " }\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
99 |
+ " }\n" |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
100 |
+ "}\n"; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
101 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
102 |
public TestFileObject(TestKind tk) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
103 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
104 |
this.tk = tk; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
105 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
106 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
107 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
108 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
109 |
return template |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
110 |
.replace("#T", tk.throwsTypes) |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
111 |
.replace("#C", tk.catchTypes); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
112 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
113 |
final TestKind tk; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
114 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
115 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
116 |
public static void main(String... args) throws Exception { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
117 |
JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
118 |
List<String> options = Arrays.asList("-proc:only"); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
119 |
for (TestKind tk: TestKind.values()) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
120 |
System.err.println("Test: " + tk); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
121 |
TestUnionType p = new TestUnionType(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
122 |
JavaFileObject fo = new TestFileObject(tk); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
123 |
JavaCompiler.CompilationTask task = comp.getTask(null, null, null, options, null, Arrays.asList(fo)); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
124 |
task.setProcessors(Arrays.asList(p)); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
125 |
boolean ok = task.call(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
126 |
System.err.println("compilation " + (ok ? "passed" : "failed")); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
127 |
if (!ok) |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
128 |
throw new Exception("compilation failed unexpectedly"); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
129 |
if (!p.log.equals(tk.gold)) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
130 |
System.err.println("Expected output:"); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
131 |
for (String g: tk.gold) |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
132 |
System.err.println(g); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
133 |
throw new Exception("unexpected output from test"); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
134 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
135 |
System.err.println(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
136 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
137 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
138 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
139 |
Trees trees; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
140 |
List<String> log; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
141 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
142 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
143 |
public void init(ProcessingEnvironment env) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
144 |
super.init(env); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
145 |
trees = Trees.instance(env); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
146 |
log = new ArrayList<String>(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
147 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
148 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
149 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
150 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
151 |
if (!roundEnv.processingOver()) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
152 |
for (Element e: roundEnv.getRootElements()) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
153 |
scan(trees.getPath(e)); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
154 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
155 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
156 |
return true; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
157 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
158 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
159 |
void scan(TreePath path) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
160 |
new Scanner().scan(path, null); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
161 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
162 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
163 |
class Scanner extends TreePathScanner<Void,Void> { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
164 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
165 |
public Void visitVariable(VariableTree tree, Void ignore) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
166 |
TreePath p = getCurrentPath(); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
167 |
Element e = trees.getElement(p); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
168 |
if (e.getKind() == ElementKind.EXCEPTION_PARAMETER) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
169 |
log("VariableTree: " + tree); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
170 |
log("VariableTree: elem " + print(e)); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
171 |
log("VariableTree: elem.type " + print(e.asType())); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
172 |
log("VariableTree: elem.type.elem " + print(types.asElement(e.asType()))); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
173 |
TypeMirror tm = trees.getTypeMirror(p); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
174 |
log("VariableTree: type " + print(tm)); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
175 |
log("VariableTree: type.elem " + print(types.asElement(tm))); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
176 |
if (types.asElement(tm) != null) |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
177 |
log("VariableTree: type.elem.type " + print(types.asElement(tm).asType())); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
178 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
179 |
return super.visitVariable(tree, null); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
180 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
181 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
182 |
String print(TypeMirror tm) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
183 |
return (tm == null) ? null : new TypePrinter().visit(tm); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
184 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
185 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
186 |
String print(Element e) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
187 |
return (e == null) ? null : (e.getKind() + " " + e.getSimpleName()); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
188 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
189 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
190 |
void log(String msg) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
191 |
System.err.println(msg); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
192 |
log.add(msg); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
193 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
194 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
195 |
|
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
9599
diff
changeset
|
196 |
class TypePrinter extends SimpleTypeVisitor<String, Void> { |
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
197 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
198 |
protected String defaultAction(TypeMirror tm, Void ignore) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
199 |
return String.valueOf(tm.getKind()); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
200 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
201 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
202 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
203 |
public String visitUnion(UnionType t, Void ignore) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
204 |
return (t.getKind() + " " + t.getAlternatives()); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
205 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
206 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
diff
changeset
|
207 |
} |