33 * @run main ASTBuilder |
33 * @run main ASTBuilder |
34 */ |
34 */ |
35 |
35 |
36 import java.net.URI; |
36 import java.net.URI; |
37 import java.util.Arrays; |
37 import java.util.Arrays; |
|
38 import java.util.EnumSet; |
38 import java.util.function.Consumer; |
39 import java.util.function.Consumer; |
39 |
40 |
|
41 import javax.lang.model.element.Modifier; |
40 import javax.tools.JavaCompiler; |
42 import javax.tools.JavaCompiler; |
41 import javax.tools.JavaFileObject; |
43 import javax.tools.JavaFileObject; |
42 import javax.tools.SimpleJavaFileObject; |
44 import javax.tools.SimpleJavaFileObject; |
43 import javax.tools.ToolProvider; |
45 import javax.tools.ToolProvider; |
44 |
46 |
55 import com.sun.source.tree.ClassTree; |
57 import com.sun.source.tree.ClassTree; |
56 import com.sun.source.tree.IdentifierTree; |
58 import com.sun.source.tree.IdentifierTree; |
57 import com.sun.source.tree.MemberReferenceTree; |
59 import com.sun.source.tree.MemberReferenceTree; |
58 import com.sun.source.tree.MemberSelectTree; |
60 import com.sun.source.tree.MemberSelectTree; |
59 import com.sun.source.tree.MethodTree; |
61 import com.sun.source.tree.MethodTree; |
|
62 import com.sun.source.tree.ModifiersTree; |
60 import com.sun.source.tree.VariableTree; |
63 import com.sun.source.tree.VariableTree; |
61 import toolbox.ToolBox.MemoryFileManager; |
64 import toolbox.ToolBox.MemoryFileManager; |
62 |
65 |
63 public class ASTBuilder { |
66 public class ASTBuilder { |
64 |
67 |
65 public static void main(String[] args) throws Exception { |
68 public static void main(String[] args) throws Exception { |
66 runTest("class Test {" + |
69 runTest("public class Test {" + |
67 " int x;" + |
70 " public static volatile int x;" + |
68 "}", |
71 "}", |
69 U -> U._class("Test", C -> C.field("x", Type::_int))); |
72 U -> U._class("Test", |
|
73 C -> C.modifiers(M -> M.modifier(Modifier.PUBLIC)) |
|
74 .field("x", |
|
75 Type::_int, |
|
76 F -> F.modifiers(M -> M.modifier(Modifier.PUBLIC) |
|
77 .modifier(Modifier.STATIC) |
|
78 .modifier(Modifier.VOLATILE) |
|
79 ) |
|
80 ) |
|
81 ) |
|
82 ); |
70 runTest("class Test extends Exception implements java.util.List<Map<String, String>>, CharSequence {" + |
83 runTest("class Test extends Exception implements java.util.List<Map<String, String>>, CharSequence {" + |
71 " int x1 = 2;" + |
84 " int x1 = 2;" + |
72 " int x2 = 2 + x1;" + |
85 " int x2 = 2 + x1;" + |
73 " int test(int param) {" + |
86 " int test(int param) {" + |
74 " if (param == 0) return 0;" + |
87 " if (param == 0) return 0;" + |
237 @Override |
250 @Override |
238 public Void visitVariable(VariableTree node, Void p) { |
251 public Void visitVariable(VariableTree node, Void p) { |
239 result.append(node.getName()); |
252 result.append(node.getName()); |
240 return super.visitVariable(node, p); |
253 return super.visitVariable(node, p); |
241 } |
254 } |
|
255 @Override |
|
256 public Void visitModifiers(ModifiersTree node, Void p) { |
|
257 result.append(node.getFlags()); |
|
258 return super.visitModifiers(node, p); |
|
259 } |
242 }.scan(t, null); |
260 }.scan(t, null); |
243 return result.toString(); |
261 return result.toString(); |
244 } |
262 } |
245 |
263 |
246 static class MyFileObject extends SimpleJavaFileObject { |
264 static class MyFileObject extends SimpleJavaFileObject { |