I want to refactor an if - else statement into a Ternary Operator.
if ((variable) == null) {
... do something
} else {
... do something else
}
Creating a Ternary Operator with Codemodel is quite simple, as with JOp.cond() we can pass in the condition, ifTrue and ifFalse parameters to return a ternary statement.
My issue is with adding a JExpression into a method body (JBlock):
private void printSomeObject(final JMethod toStringMethod, FieldOutline fo) {
String property = fo.getPropertyInfo().getName(false);
JBlock block = toStringMethod.body();
JExpression cond = JExpr.direct(property).eq(JExpr._null());
JExpression ifTrue = JExpr.direct("... do something");
JExpression ifFalse = JExpr.direct("... do something else");
JExpression ternary = JOp.cond(cond, ifTrue, ifFalse);
// toStringMethod.body().add(generateBody(ternary)); ONLY WORKS WITH JSTATEMENT
...
}
Has anyone an idea as to how to add a JExpression to a JBlock?
A ternary operator is used as a statement that returns a value. It is not a direct replacement for an if/else block.
Try compiling the following, it will fail:
JCodeModel is correct here in requiring the ternary to be a JExpression. To add it to a JBlock you need to assign it to a variable:
generates: