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
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
Please sign in to leave a comment.
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: http://forum.jetbrains.com/thread/Meta-Programming-System-794
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)
mainMethod.body = < System.out.print("Hello"); System.out.println(" World!"); >;Or even the whole method (StaticMethodDeclaration).
node<ClassConcept> cls = ...; cls.staticMethod.add(< public static void main(String[] args) { System.out.print("Hello World!"); } >);