27 |
27 |
28 import com.sun.source.doctree.DocTree; |
28 import com.sun.source.doctree.DocTree; |
29 import com.sun.source.tree.CompilationUnitTree; |
29 import com.sun.source.tree.CompilationUnitTree; |
30 import com.sun.source.util.TreeBuilder; |
30 import com.sun.source.util.TreeBuilder; |
31 import com.sun.tools.javac.code.TypeTag; |
31 import com.sun.tools.javac.code.TypeTag; |
|
32 import com.sun.tools.javac.tree.JCTree; |
32 import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
33 import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
33 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
34 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
34 import com.sun.tools.javac.tree.JCTree.JCExpression; |
35 import com.sun.tools.javac.tree.JCTree.JCExpression; |
|
36 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
|
37 import com.sun.tools.javac.tree.JCTree.JCStatement; |
35 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
38 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
36 import com.sun.tools.javac.tree.JCTree.Tag; |
39 import com.sun.tools.javac.tree.JCTree.Tag; |
37 |
40 |
38 import com.sun.tools.javac.tree.TreeMaker; |
41 import com.sun.tools.javac.tree.TreeMaker; |
39 import com.sun.tools.javac.util.List; |
42 import com.sun.tools.javac.util.List; |
|
43 import com.sun.tools.javac.util.Name; |
40 import com.sun.tools.javac.util.Names; |
44 import com.sun.tools.javac.util.Names; |
41 |
45 |
42 /** |
46 /** |
43 * Implementation for TreeBuilder. |
47 * Implementation for TreeBuilder. |
44 * Note: this is only a crude experiment. |
48 * Note: this is only a crude experiment. |
223 throw new UnsupportedOperationException("Not supported yet."); |
235 throw new UnsupportedOperationException("Not supported yet."); |
224 } |
236 } |
225 |
237 |
226 } |
238 } |
227 |
239 |
|
240 private final class MethodImpl implements Method { |
|
241 |
|
242 private final JCMethodDecl result; |
|
243 |
|
244 public MethodImpl(JCExpression restype, String name) { |
|
245 result = make.MethodDef(make.Modifiers(0), names.fromString(name), restype, List.nil(), List.nil(), List.nil(), null, null); |
|
246 } |
|
247 |
|
248 @Override |
|
249 public Method parameter(Consumer<Type> type, Consumer<Parameter> parameter) { |
|
250 ParameterImpl paramImpl = new ParameterImpl(visitType(type)); |
|
251 parameter.accept(paramImpl); |
|
252 result.params = result.params.append(paramImpl.result); |
|
253 return this; |
|
254 } |
|
255 |
|
256 @Override |
|
257 public Method body(Consumer<Block> statements) { |
|
258 BlockImpl block = new BlockImpl(); |
|
259 statements.accept(block); |
|
260 result.body = make.Block(0, block.statements); |
|
261 return this; |
|
262 } |
|
263 |
|
264 @Override |
|
265 public Method modifiers(Consumer<Modifiers> modifiers) { |
|
266 throw new UnsupportedOperationException("Not supported yet."); |
|
267 } |
|
268 |
|
269 @Override |
|
270 public Method javadoc(DocTree doc) { |
|
271 throw new UnsupportedOperationException("Not supported yet."); |
|
272 } |
|
273 |
|
274 @Override |
|
275 public Method javadoc(String doc) { |
|
276 throw new UnsupportedOperationException("Not supported yet."); |
|
277 } |
|
278 |
|
279 } |
|
280 |
|
281 private final class ParameterImpl implements Parameter { |
|
282 |
|
283 private final JCVariableDecl result; |
|
284 |
|
285 public ParameterImpl(JCExpression type) { |
|
286 //TODO: infer name |
|
287 result = make.VarDef(make.Modifiers(0), null, type, null); |
|
288 } |
|
289 |
|
290 @Override |
|
291 public Parameter modifiers(Consumer<Modifiers> modifiers) { |
|
292 throw new UnsupportedOperationException("Not supported yet."); |
|
293 } |
|
294 |
|
295 @Override |
|
296 public Parameter name(String name) { |
|
297 result.name = names.fromString(name); //XXX: check not set yet. |
|
298 return this; |
|
299 } |
|
300 |
|
301 } |
|
302 |
|
303 private final class BlockImpl extends StatementBaseImpl<Block> implements Block { |
|
304 |
|
305 private List<JCStatement> statements = List.nil(); |
|
306 |
|
307 @Override |
|
308 protected Block addStatement(JCStatement stat) { |
|
309 statements = statements.append(stat); |
|
310 return this; |
|
311 } |
|
312 |
|
313 } |
|
314 |
|
315 private final class StatementImpl extends StatementBaseImpl<Void> implements Statement { |
|
316 private JCStatement result; |
|
317 |
|
318 @Override |
|
319 protected Void addStatement(JCStatement stat) { |
|
320 if (result != null) { |
|
321 throw new IllegalStateException(); |
|
322 } |
|
323 result = stat; |
|
324 return null; |
|
325 } |
|
326 } |
|
327 |
|
328 private abstract class StatementBaseImpl<S> implements StatementBase<S> { |
|
329 |
|
330 @Override |
|
331 public S _if(Consumer<Expression> cond, Consumer<Statement> ifPart) { |
|
332 JCExpression expr = visitExpression(cond); |
|
333 //TODO: should this automatic wrapping with parenthesized be here? |
|
334 expr = make.Parens(expr); |
|
335 StatementImpl ifStatement = new StatementImpl(); |
|
336 ifPart.accept(ifStatement); |
|
337 //TODO: check ifPart filled! |
|
338 return addStatement(make.If(expr, ifStatement.result, null)); |
|
339 } |
|
340 |
|
341 @Override |
|
342 public S _if(Consumer<Expression> cond, Consumer<Statement> ifPart, Consumer<Statement> elsePart) { |
|
343 JCExpression expr = visitExpression(cond); |
|
344 //TODO: should this automatic wrapping with parenthesized be here? |
|
345 expr = make.Parens(expr); |
|
346 StatementImpl ifStatement = new StatementImpl(); |
|
347 ifPart.accept(ifStatement); |
|
348 //TODO: check ifPart filled! |
|
349 StatementImpl elseStatement = new StatementImpl(); |
|
350 elsePart.accept(elseStatement); |
|
351 return addStatement(make.If(expr, ifStatement.result, elseStatement.result)); |
|
352 } |
|
353 |
|
354 @Override |
|
355 public S _return() { |
|
356 throw new UnsupportedOperationException("Not supported yet."); |
|
357 } |
|
358 |
|
359 @Override |
|
360 public S _return(Consumer<Expression> expr) { |
|
361 return addStatement(make.Return(visitExpression(expr))); |
|
362 } |
|
363 |
|
364 @Override |
|
365 public S expr(Consumer<Expression> expr) { |
|
366 throw new UnsupportedOperationException("Not supported yet."); |
|
367 } |
|
368 |
|
369 @Override |
|
370 public S skip() { |
|
371 throw new UnsupportedOperationException("Not supported yet."); |
|
372 } |
|
373 |
|
374 protected abstract S addStatement(JCStatement stat); |
|
375 } |
|
376 |
228 private final class ExpressionImpl implements Expression { |
377 private final class ExpressionImpl implements Expression { |
229 |
378 |
230 private JCExpression expr; |
379 private JCExpression expr; |
|
380 |
|
381 @Override |
|
382 public void equal_to(Consumer<Expression> lhs, Consumer<Expression> rhs) { |
|
383 expr = make.Binary(Tag.EQ, |
|
384 visitExpression(lhs), |
|
385 visitExpression(rhs)); |
|
386 } |
231 |
387 |
232 @Override |
388 @Override |
233 public void minusminus(Consumer<Expression> expr) { |
389 public void minusminus(Consumer<Expression> expr) { |
234 throw new UnsupportedOperationException("Not supported yet."); |
390 throw new UnsupportedOperationException("Not supported yet."); |
235 } |
391 } |