coderwurst coderwurst - 1 year ago 611
Java Question

Add JExpression (JOp.cond()) to JBlock (JMethod body)

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 =;
JExpression ifTrue ="... do something");
JExpression ifFalse ="... 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?

Answer Source

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:

1 == 1 ? System.out.pritln("true") : System.out.println("false");

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:

JDefinedClass output = codeModel._class(JMod.PUBLIC, "org.Test", ClassType.CLASS);
JMethod method = output.method(JMod.PUBLIC, codeModel.VOID, "test");
method.body().decl(codeModel.ref(String.class), "value", JOp.cond(JExpr.lit(1).eq(JExpr.lit(1)), JExpr.lit("true"), JExpr.lit("false")));


public class Test {

    public void test() {
        String value = ((1 == 1)?"true":"false");