How to (programmatically) add expression to existing method?

I'm trying to generate class with main method with "System.out.println("Hello world")" inside.

How to add this expression, "System.out.println("Hello world")", to existing node<StaticMethodDeclaration> mainMethod?

I created everything up to DotExpression. It requires to create operand and operation. Operand is StaticFieldReference to System.out, and operation is InstanceMethodCallOperation to println, and I have now clue how to create this references :(

Can you show some code? Or, at least, point to right examples?

Thank you
Comment actions Permalink
Found solution.
There's a special syntax for declaring expressions:

node<StatementList> mainStList = mainMethod.body.set new(StatementList);
node<ExpressionStatement> expressionSt = mainStList.statement.add new(ExpressionStatement);
node<DotExpression> dotExpr = <System.out.println("Hello World")>;
expressionSt.expression = dotExpr;

Got idea from this:

Just press left angle bracket, select "<quotation>" from list, than rerun completion and replace "<abstract concept>" with DotExpression, and then carefully replace first part (using completion too) with System and press dot on keyboard... The rest is obvious.

I will create video tutorial on this topic later (in russian)
Comment actions Permalink
Not just for expressions. With quotations you can instantiate any concept. Type "StatementList" instead of DotExpression and you are able to set the whole method body in one statement.

mainMethod.body = <   
System.out.println(" World!"); 
Or even the whole method (StaticMethodDeclaration).
node<ClassConcept> cls = ...; 
public static void main(String[] args) { 
  System.out.print("Hello World!");  

Please sign in to leave a comment.