equal
deleted
inserted
replaced
35 |
35 |
36 import java.util.HashSet; |
36 import java.util.HashSet; |
37 import java.util.Locale; |
37 import java.util.Locale; |
38 import java.util.Set; |
38 import java.util.Set; |
39 |
39 |
|
40 import com.sun.source.tree.BlockTree; |
|
41 import com.sun.source.tree.IfTree; |
40 import com.sun.source.tree.MemberSelectTree; |
42 import com.sun.source.tree.MemberSelectTree; |
|
43 import com.sun.source.tree.MethodTree; |
41 import com.sun.source.tree.ParameterizedTypeTree; |
44 import com.sun.source.tree.ParameterizedTypeTree; |
|
45 import com.sun.source.tree.ReturnTree; |
|
46 import com.sun.source.tree.StatementTree; |
42 |
47 |
43 public class CodeBuilder { |
48 public class CodeBuilder { |
44 |
49 |
45 public static String buildCodeToGenerate(Tree t, String builder) { |
50 public static String buildCodeToGenerate(Tree t, String builder) { |
46 StringBuilder result = new StringBuilder(); |
51 StringBuilder result = new StringBuilder(); |
66 scan(node.getMembers(), p); |
71 scan(node.getMembers(), p); |
67 }); |
72 }); |
68 result.append(")"); |
73 result.append(")"); |
69 return null; |
74 return null; |
70 } |
75 } |
|
76 |
71 @Override |
77 @Override |
72 public Void visitCompilationUnit(CompilationUnitTree node, Void p) { |
78 public Void visitCompilationUnit(CompilationUnitTree node, Void p) { |
73 result.append(currentBuilder() + ".createCompilationUnitTree("); |
79 result.append(currentBuilder() + ".createCompilationUnitTree("); |
74 doScan("U", () -> super.visitCompilationUnit(node, p)); |
80 doScan("U", () -> super.visitCompilationUnit(node, p)); |
75 result.append(")"); |
81 result.append(")"); |
76 return null; |
82 return null; |
77 } |
83 } |
|
84 |
78 @Override |
85 @Override |
79 public Void visitVariable(VariableTree node, Void p) { |
86 public Void visitVariable(VariableTree node, Void p) { |
80 result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable! |
87 result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable! |
81 doScan("T", node.getType()); |
88 doScan("T", node.getType()); |
82 result.append(", "); |
89 result.append(", "); |
91 result.append(")"); |
98 result.append(")"); |
92 return null; |
99 return null; |
93 } |
100 } |
94 |
101 |
95 @Override |
102 @Override |
|
103 public Void visitMethod(MethodTree node, Void p) { |
|
104 result.append(currentBuilder() + ".method(\"" + node.getName() + "\", "); |
|
105 doScan("T", node.getReturnType()); |
|
106 result.append(", "); |
|
107 doScan("M", () -> { |
|
108 //TODO: other attributes! |
|
109 for (VariableTree param : node.getParameters()) { |
|
110 result.append(currentBuilder() + ".parameter("); |
|
111 doScan("T", param.getType()); |
|
112 result.append(", "); |
|
113 doScan("P", () -> { |
|
114 result.append(currentBuilder() + ".name(\"" + param.getName() + "\")"); |
|
115 }); |
|
116 //TODO: other attributes! |
|
117 result.append(")"); |
|
118 } |
|
119 if (node.getBody() != null) {//TODO: test no/null body! |
|
120 result.append(currentBuilder() + ".body("); |
|
121 doScan("B", node.getBody()); |
|
122 result.append(")"); |
|
123 } |
|
124 }); |
|
125 result.append(")"); |
|
126 return null; |
|
127 } |
|
128 |
|
129 @Override |
96 public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) { |
130 public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) { |
97 result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()"); |
131 result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()"); |
98 return null; |
132 return null; |
99 } |
133 } |
100 |
134 |
118 return null; |
152 return null; |
119 } |
153 } |
120 |
154 |
121 @Override |
155 @Override |
122 public Void visitBinary(BinaryTree node, Void p) { |
156 public Void visitBinary(BinaryTree node, Void p) { |
|
157 String methodName; |
123 switch (node.getKind()) { |
158 switch (node.getKind()) { |
124 case PLUS: |
159 case PLUS: |
125 result.append(currentBuilder() + ".plus("); |
160 methodName = "plus"; break; |
126 doScan("E", node.getLeftOperand()); |
161 case EQUAL_TO: |
127 result.append(", "); |
162 methodName = "equal_to"; break; |
128 doScan("E", node.getRightOperand()); |
|
129 result.append(")"); |
|
130 break; |
|
131 default: throw new IllegalStateException("Not handled: " + node.getKind()); |
163 default: throw new IllegalStateException("Not handled: " + node.getKind()); |
132 } |
164 } |
|
165 result.append(currentBuilder() + "." + methodName + "("); |
|
166 doScan("E", node.getLeftOperand()); |
|
167 result.append(", "); |
|
168 doScan("E", node.getRightOperand()); |
|
169 result.append(")"); |
133 return null; |
170 return null; |
134 } |
171 } |
135 |
172 |
136 @Override |
173 @Override |
137 public Void visitMemberSelect(MemberSelectTree node, Void p) { |
174 public Void visitMemberSelect(MemberSelectTree node, Void p) { |
145 } |
182 } |
146 |
183 |
147 @Override |
184 @Override |
148 public Void visitIdentifier(IdentifierTree node, Void p) { |
185 public Void visitIdentifier(IdentifierTree node, Void p) { |
149 result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")"); |
186 result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")"); |
|
187 return null; |
|
188 } |
|
189 |
|
190 // @Override |
|
191 // public Void visitBlock(BlockTree node, Void p) { |
|
192 //// for (StatementTree st : node.getStatements()) { |
|
193 //// result.append(curr) |
|
194 //// } |
|
195 // return super.visitBlock(node, p); |
|
196 // } |
|
197 |
|
198 @Override |
|
199 public Void visitIf(IfTree node, Void p) { |
|
200 result.append(currentBuilder() + "._if("); |
|
201 doScan("E", node.getCondition()); |
|
202 result.append(", "); |
|
203 doScan("S", node.getThenStatement()); |
|
204 if (node.getElseStatement() != null) { |
|
205 result.append(", "); |
|
206 doScan("S", node.getElseStatement()); |
|
207 } |
|
208 result.append(")"); |
|
209 return null; |
|
210 } |
|
211 |
|
212 @Override |
|
213 public Void visitReturn(ReturnTree node, Void p) { |
|
214 result.append(currentBuilder() + "._return("); |
|
215 if (node.getExpression()!= null) { |
|
216 doScan("E", node.getExpression()); |
|
217 } |
|
218 result.append(")"); |
150 return null; |
219 return null; |
151 } |
220 } |
152 |
221 |
153 private void handleDeclaredType(Tree t) { |
222 private void handleDeclaredType(Tree t) { |
154 doScan("T", () -> { |
223 doScan("T", () -> { |