|
1 /* |
|
2 * Copyright (c) 2019, 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 9999999 |
|
27 * @summary XXX |
|
28 * @modules java.compiler |
|
29 * jdk.compiler |
|
30 */ |
|
31 |
|
32 import java.net.URI; |
|
33 import java.util.Arrays; |
|
34 import java.util.function.Consumer; |
|
35 |
|
36 import javax.tools.JavaCompiler; |
|
37 import javax.tools.JavaFileObject; |
|
38 import javax.tools.SimpleJavaFileObject; |
|
39 import javax.tools.ToolProvider; |
|
40 |
|
41 import com.sun.source.tree.CompilationUnitTree; |
|
42 import com.sun.source.tree.Tree; |
|
43 import com.sun.source.util.JavacTask; |
|
44 import com.sun.source.util.TreeBuilder; |
|
45 import com.sun.source.util.TreeBuilder.*; |
|
46 import com.sun.source.util.Trees; |
|
47 import com.sun.source.util.TreeScanner; |
|
48 |
|
49 public class ASTBuilder { |
|
50 |
|
51 public static void main(String[] args) throws Exception { |
|
52 runTest("class Test {" + |
|
53 " int x;" + |
|
54 "}", |
|
55 U -> U._class("Test", C -> C.field("x", Type::_int))); |
|
56 } |
|
57 |
|
58 private static void runTest(String expectedCode, Consumer<CompilationUnit> actualBuilder) throws Exception { |
|
59 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
|
60 assert tool != null; |
|
61 |
|
62 JavacTask expecteTask = (JavacTask) tool.getTask(null, null, null, |
|
63 null, null, Arrays.asList(new MyFileObject(expectedCode))); |
|
64 String expectedDump = dumpTree(expecteTask.parse().iterator().next()); |
|
65 |
|
66 JavacTask ct = (JavacTask) tool.getTask(null, null, null, |
|
67 null, null, Arrays.asList(new MyFileObject(""))); |
|
68 ct.parse(); //init javac |
|
69 Trees t = Trees.instance(ct); |
|
70 |
|
71 TreeBuilder builder = t.getTreeBuilder(); |
|
72 |
|
73 CompilationUnitTree cut = builder.createCompilationUnitTree(actualBuilder); |
|
74 |
|
75 String actualDump = dumpTree(cut); |
|
76 |
|
77 if (!actualDump.equals(expectedDump)) { |
|
78 throw new AssertionError("Expected and actual dump differ. Expected: " + expectedDump + "; actual: " + actualDump); |
|
79 } |
|
80 } |
|
81 |
|
82 static String dumpTree(Tree t) { |
|
83 StringBuilder result = new StringBuilder(); |
|
84 new TreeScanner<Void, Void>() { |
|
85 String sep = ""; |
|
86 @Override |
|
87 public Void scan(Tree tree, Void p) { |
|
88 result.append(sep); |
|
89 sep = " "; |
|
90 if (tree == null) { |
|
91 result.append("null"); |
|
92 return null; |
|
93 } |
|
94 result.append("("); |
|
95 result.append(tree.getKind().name()); |
|
96 super.scan(tree, p); |
|
97 result.append(")"); |
|
98 return null; |
|
99 } |
|
100 @Override |
|
101 public Void scan(Iterable<? extends Tree> nodes, Void p) { |
|
102 result.append(sep); |
|
103 sep = " "; |
|
104 if (nodes == null) { |
|
105 result.append("null"); |
|
106 return null; |
|
107 } |
|
108 result.append("("); |
|
109 super.scan(nodes, p); |
|
110 result.append(")"); |
|
111 return null; |
|
112 } |
|
113 }.scan(t, null); |
|
114 return result.toString(); |
|
115 } |
|
116 |
|
117 static class MyFileObject extends SimpleJavaFileObject { |
|
118 private String text; |
|
119 |
|
120 public MyFileObject(String text) { |
|
121 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
122 this.text = text; |
|
123 } |
|
124 |
|
125 @Override |
|
126 public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
127 return text; |
|
128 } |
|
129 } |
|
130 } |