Esempio n. 1
0
 public void testWriteArray() {
   assertEquals(
       "1 + integers[2 + index]",
       Expressions.toString(
           Expressions.add(
               Expressions.constant(1),
               Expressions.arrayIndex(
                   Expressions.variable(int[].class, "integers"),
                   Expressions.add(
                       Expressions.constant(2), Expressions.variable(int.class, "index"))))));
 }
Esempio n. 2
0
  public void testLambdaCallsBinaryOp() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Integer.TYPE, "arg");

    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr =
        Expressions.lambda(
            Expressions.add(paramExpr, Expressions.constant(2)), Arrays.asList(paramExpr));

    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals(
        "new net.hydromatic.linq4j.function.Function1() {\n"
            + "  public int apply(Integer arg) {\n"
            + "    return arg + 2;\n"
            + "  }\n"
            + "  public Object apply(Object arg) {\n"
            + "    return apply(\n"
            + "      (Integer) arg);\n"
            + "  }\n"
            + "}\n",
        s);

    // Compile and run the lambda expression.
    // The value of the parameter is 1.
    int n = (Integer) lambdaExpr.compile().dynamicInvoke(1);

    // This code example produces the following output:
    //
    // arg => (arg +2)
    // 3
    assertEquals(3, n);
  }
Esempio n. 3
0
 public void testBlockBuilder() {
   BlockBuilder statements = new BlockBuilder();
   Expression one = statements.append("one", Expressions.constant(1));
   Expression two = statements.append("two", Expressions.constant(2));
   Expression three = statements.append("three", Expressions.add(one, two));
   Expression six = statements.append("six", Expressions.multiply(three, two));
   Expression eighteen = statements.append("eighteen", Expressions.multiply(three, six));
   statements.add(Expressions.return_(null, eighteen));
   assertEquals(
       "{\n"
           + "  final int three = 1 + 2;\n"
           + "  final int six = three * 2;\n"
           + "  final int eighteen = three * six;\n"
           + "  return eighteen;\n"
           + "}\n",
       Expressions.toString(statements.toBlock()));
 }
Esempio n. 4
0
  public void testWrite() {
    assertEquals(
        "1 + 2.0F + 3L + Long.valueOf(4L)",
        Expressions.toString(
            Expressions.add(
                Expressions.add(
                    Expressions.add(Expressions.constant(1), Expressions.constant(2F, Float.TYPE)),
                    Expressions.constant(3L, Long.TYPE)),
                Expressions.constant(4L, Long.class))));

    assertEquals(
        "new java.math.BigDecimal(31415926L, 7)",
        Expressions.toString(Expressions.constant(BigDecimal.valueOf(314159260, 8))));

    // Parentheses needed, to override the left-associativity of +.
    assertEquals(
        "1 + (2 + 3)",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(1),
                Expressions.add(Expressions.constant(2), Expressions.constant(3)))));

    // No parentheses needed; higher precedence of * achieves the desired
    // effect.
    assertEquals(
        "1 + 2 * 3",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(1),
                Expressions.multiply(Expressions.constant(2), Expressions.constant(3)))));

    assertEquals(
        "1 * (2 + 3)",
        Expressions.toString(
            Expressions.multiply(
                Expressions.constant(1),
                Expressions.add(Expressions.constant(2), Expressions.constant(3)))));

    // Parentheses needed, to overcome right-associativity of =.
    assertEquals(
        "(1 = 2) = 3",
        Expressions.toString(
            Expressions.assign(
                Expressions.assign(Expressions.constant(1), Expressions.constant(2)),
                Expressions.constant(3))));

    // Ternary operator.
    assertEquals(
        "1 < 2 ? (3 < 4 ? 5 : 6) : 7 < 8 ? 9 : 10",
        Expressions.toString(
            Expressions.condition(
                Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)),
                Expressions.condition(
                    Expressions.lessThan(Expressions.constant(3), Expressions.constant(4)),
                    Expressions.constant(5),
                    Expressions.constant(6)),
                Expressions.condition(
                    Expressions.lessThan(Expressions.constant(7), Expressions.constant(8)),
                    Expressions.constant(9),
                    Expressions.constant(10)))));

    assertEquals(
        "0 + (double) (2 + 3)",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(0),
                Expressions.convert_(
                    Expressions.add(Expressions.constant(2), Expressions.constant(3)),
                    Double.TYPE))));

    assertEquals(
        "a.empno",
        Expressions.toString(
            Expressions.field(Expressions.parameter(Linq4jTest.Employee.class, "a"), "empno")));

    assertEquals(
        "java.util.Collections.EMPTY_LIST",
        Expressions.toString(Expressions.field(null, Collections.class, "EMPTY_LIST")));

    final ParameterExpression paramX = Expressions.parameter(String.class, "x");
    assertEquals(
        "new net.hydromatic.linq4j.function.Function1() {\n"
            + "  public int apply(String x) {\n"
            + "    return x.length();\n"
            + "  }\n"
            + "  public Object apply(Object x) {\n"
            + "    return apply(\n"
            + "      (String) x);\n"
            + "  }\n"
            + "}\n",
        Expressions.toString(
            Expressions.lambda(
                Function1.class,
                Expressions.call(paramX, "length", Collections.<Expression>emptyList()),
                Arrays.asList(paramX))));

    assertEquals(
        "new String[] {\n" + "  \"foo\",\n" + "  null,\n" + "  \"bar\\\"baz\"}",
        Expressions.toString(
            Expressions.newArrayInit(
                String.class,
                Arrays.<Expression>asList(
                    Expressions.constant("foo"),
                    Expressions.constant(null),
                    Expressions.constant("bar\"baz")))));

    assertEquals(
        "(int) ((String) (Object) \"foo\").length()",
        Expressions.toString(
            Expressions.convert_(
                Expressions.call(
                    Expressions.convert_(
                        Expressions.convert_(Expressions.constant("foo"), Object.class),
                        String.class),
                    "length",
                    Collections.<Expression>emptyList()),
                Integer.TYPE)));

    // resolving a static method
    assertEquals(
        "Integer.valueOf(\"0123\")",
        Expressions.toString(
            Expressions.call(
                Integer.class,
                "valueOf",
                Collections.<Expression>singletonList(Expressions.constant("0123")))));

    // precedence of not and instanceof
    assertEquals(
        "!(o instanceof String)",
        Expressions.toString(
            Expressions.not(
                Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class))));

    // not not
    assertEquals(
        "!!(o instanceof String)",
        Expressions.toString(
            Expressions.not(
                Expressions.not(
                    Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class)))));
  }