CodeModel / Jinvocation with JMethod.body()

274 views Asked by At

I have the next JInvocation:

JInvocation jInvoke = JExpr.invoke(anotherPageInstance, method);

where anotherPageInstance is JVar and method is JMethod.

I do some actions on this JInvocation, and then I want to add it to test.body().invoke where test is JMethod.

The main problem is that I can't, because test.body().invoke has no option to get JInvocation as argument.

I can do the next: test.body().invoke(anotherPageInstance, method), but this doesn't help me, because I want to save the JInvocation, and just when some conditions are taking place, add this Jinvocation to the body (and as I mentions before, meanwhile I do some action on this JInvocation).

Any help?

2

There are 2 answers

0
John Ericksen On BEST ANSWER

You can add an invocation the body (JBlock) by using the add() method:

test.body().add(jInvoke);
0
mcoblenz On

Instead of instantiating JInvocation with JExpr.invoke(), use the JExpr factory methods:

public static JInvocation invoke(JExpression lhs,
                                 JMethod method)
public static JInvocation invoke(JExpression lhs,
                                 String method)

See JExpr.