//1) Register CustomExpression object as a parameter
String expToGroupByName = group.getName() + "_custom_expression";
registerCustomExpressionParameter(expToGroupByName, customExpression);
//2) Create a variable which is calculated through the custom expression
JRDesignVariable gvar = new JRDesignVariable();
String varName = group.getName() + "_variable_for_group_expression";
gvar.setName(varName);
gvar.setCalculation(JRDesignVariable.CALCULATION_NOTHING);
gvar.setValueClassName(customExpression.getClassName());
String expText = ExpressionUtils.createCustomExpressionInvocationText(expToGroupByName);
JRDesignExpression gvarExp = new JRDesignExpression();
gvarExp.setValueClassName(customExpression.getClassName());
gvarExp.setText(expText);
gvar.setExpression(gvarExp);
getDjd().addVariable(gvar);
//3) Make the group expression point to the variable
jrExpression.setText("$V{"+varName+"}");
jrExpression.setValueClassName(customExpression.getClassName());