Search in sources :

Example 1 with CatchBlock

use of org.apache.calcite.linq4j.tree.CatchBlock in project ignite-3 by apache.

the class RexToLixTranslator method handleMethodCheckedExceptions.

/**
 * Handle checked Exceptions declared in Method. In such case, method call should be wrapped in a try...catch block.
 * " final Type method_call; try { method_call = callExpr } catch (Exception e) { throw new RuntimeException(e); } "
 */
Expression handleMethodCheckedExceptions(Expression callExpr) {
    // Try statement
    ParameterExpression methodCall = Expressions.parameter(callExpr.getType(), list.newName("method_call"));
    list.add(Expressions.declare(Modifier.FINAL, methodCall, null));
    Statement st = Expressions.statement(Expressions.assign(methodCall, callExpr));
    // Catch Block, wrap checked exception in unchecked exception
    ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
    Expression uncheckedException = Expressions.new_(RuntimeException.class, e);
    CatchBlock cb = Expressions.catch_(e, Expressions.throw_(uncheckedException));
    list.add(Expressions.tryCatch(st, cb));
    return methodCall;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Statement(org.apache.calcite.linq4j.tree.Statement) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) CatchBlock(org.apache.calcite.linq4j.tree.CatchBlock) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 2 with CatchBlock

use of org.apache.calcite.linq4j.tree.CatchBlock in project calcite by apache.

the class InlinerTest method testInlineInTryCatchStatement.

@Test
void testInlineInTryCatchStatement() {
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(Modifier.FINAL, t, ONE));
    final ParameterExpression u = Expressions.parameter(int.class, "u");
    builder.add(Expressions.declare(Modifier.FINAL, u, null));
    Statement st = Expressions.statement(Expressions.assign(u, Expressions.makeBinary(ExpressionType.Add, t, TWO)));
    ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
    CatchBlock cb = Expressions.catch_(e, Expressions.throw_(e));
    builder.add(Expressions.tryCatch(st, cb));
    builder.add(Expressions.return_(null, u));
    assertEquals("{\n" + "  final int u;\n" + "  try {\n" + "    u = 1 + 2;\n" + "  } catch (Exception e) {\n" + "    throw e;\n" + "  }\n" + "  return u;\n" + "}\n", builder.toBlock().toString());
}
Also used : DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Statement(org.apache.calcite.linq4j.tree.Statement) CatchBlock(org.apache.calcite.linq4j.tree.CatchBlock) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with CatchBlock

use of org.apache.calcite.linq4j.tree.CatchBlock in project calcite by apache.

the class RexToLixTranslator method handleMethodCheckedExceptions.

/**
 * Handle checked Exceptions declared in Method. In such case,
 * method call should be wrapped in a try...catch block.
 * "
 *      final Type method_call;
 *      try {
 *        method_call = callExpr
 *      } catch (Exception e) {
 *        throw new RuntimeException(e);
 *      }
 * "
 */
Expression handleMethodCheckedExceptions(Expression callExpr) {
    // Try statement
    ParameterExpression methodCall = Expressions.parameter(callExpr.getType(), list.newName("method_call"));
    list.add(Expressions.declare(Modifier.FINAL, methodCall, null));
    Statement st = Expressions.statement(Expressions.assign(methodCall, callExpr));
    // Catch Block, wrap checked exception in unchecked exception
    ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
    Expression uncheckedException = Expressions.new_(RuntimeException.class, e);
    CatchBlock cb = Expressions.catch_(e, Expressions.throw_(uncheckedException));
    list.add(Expressions.tryCatch(st, cb));
    return methodCall;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Statement(org.apache.calcite.linq4j.tree.Statement) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) CatchBlock(org.apache.calcite.linq4j.tree.CatchBlock) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Aggregations

CatchBlock (org.apache.calcite.linq4j.tree.CatchBlock)3 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)3 Statement (org.apache.calcite.linq4j.tree.Statement)3 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)2 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)2 Expression (org.apache.calcite.linq4j.tree.Expression)2 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 DeclarationStatement (org.apache.calcite.linq4j.tree.DeclarationStatement)1 Test (org.junit.jupiter.api.Test)1