if (thenStmt instanceof JReturnStatement
&& elseStmt instanceof JReturnStatement && currentMethod != null) {
// Special case
// if () { return ..; } else { return ..; } =>
// return ... ? ... : ...;
JExpression thenExpression = ((JReturnStatement) thenStmt).getExpr();
JExpression elseExpression = ((JReturnStatement) elseStmt).getExpr();
if (thenExpression == null || elseExpression == null) {
// empty returns are not supported.
return null;
}
JConditional conditional = new JConditional(sourceInfo,
currentMethod.getType(), condExpr, thenExpression, elseExpression);
JReturnStatement returnStatement = new JReturnStatement(sourceInfo,
conditional);
return returnStatement;
}
if (elseStmt != null) {
// if () { } else { } -> ... ? ... : ... ;
JExpression thenExpression = extractExpression(thenStmt);
JExpression elseExpression = extractExpression(elseStmt);
if (thenExpression != null && elseExpression != null) {
JConditional conditional = new JConditional(sourceInfo,
JPrimitiveType.VOID, condExpr, thenExpression, elseExpression);
return conditional.makeStatement();
}
} else {
// if () { } -> ... && ...;
JExpression thenExpression = extractExpression(thenStmt);
if (thenExpression != null) {
JBinaryOperator binaryOperator = JBinaryOperator.AND;
JExpression unflipExpression = maybeUnflipBoolean(condExpr);
if (unflipExpression != null) {
condExpr = unflipExpression;
binaryOperator = JBinaryOperator.OR;
}