Search in sources :

Example 1 with Node

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

the class ExpressionTest method testWriteTryCatchFinally.

@Test
public void testWriteTryCatchFinally() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.tryCatchFinally(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.statement(Expressions.call(Expressions.constant("foo"), "toUpperCase")), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.throw_(Expressions.new_(IndexOutOfBoundsException.class))));
    assertEquals("try {\n" + "  return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + "  return null;\n" + "} catch (RuntimeException re) {\n" + "  throw new IndexOutOfBoundsException();\n" + "} finally {\n" + "  \"foo\".toUpperCase();\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.Test)

Example 2 with Node

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

the class ExpressionTest method testWriteWhile.

@Test
public void testWriteWhile() {
    DeclarationStatement xDecl;
    DeclarationStatement yDecl;
    Node node = Expressions.block(xDecl = Expressions.declare(0, "x", Expressions.constant(10)), yDecl = Expressions.declare(0, "y", Expressions.constant(0)), Expressions.while_(Expressions.lessThan(xDecl.parameter, Expressions.constant(5)), Expressions.statement(Expressions.preIncrementAssign(yDecl.parameter))));
    assertEquals("{\n" + "  int x = 10;\n" + "  int y = 0;\n" + "  while (x < 5) {\n" + "    ++y;\n" + "  }\n" + "}\n", Expressions.toString(node));
}
Also used : Node(org.apache.calcite.linq4j.tree.Node) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.Test)

Example 3 with Node

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

the class ExpressionTest method testWriteTryFinally.

@Test
public void testWriteTryFinally() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.ifThen(Expressions.constant(true), Expressions.tryFinally(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.statement(Expressions.call(Expressions.constant("foo"), "toUpperCase"))));
    assertEquals("if (true) {\n" + "  try {\n" + "    return \"foo\".length();\n" + "  } finally {\n" + "    \"foo\".toUpperCase();\n" + "  }\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.Test)

Example 4 with Node

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

the class ExpressionTest method testWriteTryCatch.

@Test
public void testWriteTryCatch() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.tryCatch(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.return_(null, Expressions.call(re_, "toString"))));
    assertEquals("try {\n" + "  return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + "  return null;\n" + "} catch (RuntimeException re) {\n" + "  return re.toString();\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.Test)

Aggregations

Node (org.apache.calcite.linq4j.tree.Node)4 Test (org.junit.Test)4 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)3 DeclarationStatement (org.apache.calcite.linq4j.tree.DeclarationStatement)1