Package com.strobel.expressions

Examples of com.strobel.expressions.Expression


        assertTrue(delegate.test());
    }

    @Test
    public void testCompileToMethod() throws Exception {
        final Expression out = field(null, Type.of(System.class).getField("out"));

        final TypeBuilder<Runnable> typeBuilder = new TypeBuilder<>(
            "TestCompileToMethod",
            Modifier.PUBLIC | Modifier.FINAL,
            Types.Object,
            Type.list(Type.of(Runnable.class))
        );

        final MethodBuilder powerMethod = typeBuilder.defineMethod(
            "power",
            Modifier.PUBLIC | Modifier.FINAL,
            PrimitiveTypes.Integer,
            Type.list(PrimitiveTypes.Integer, PrimitiveTypes.Integer)
        );

        final MethodBuilder runMethod = typeBuilder.defineMethod(
            "run",
            Modifier.PUBLIC | Modifier.FINAL,
            PrimitiveTypes.Void
        );

        final Expression self = self(typeBuilder);

        final ParameterExpression base = variable(PrimitiveTypes.Integer, "base");
        final ParameterExpression power = variable(PrimitiveTypes.Integer, "power");
        final ParameterExpression accumulator = variable(PrimitiveTypes.Integer, "accumulator");
        final ParameterExpression variable = variable(PrimitiveTypes.Integer, "i");
View Full Code Here


        instance.run();
    }

    @Test
    public void testCompileToGeneratedMethod() throws Exception {
        final Expression out = field(null, Type.of(System.class).getField("out"));

        final TypeBuilder<Runnable> typeBuilder = new TypeBuilder<>(
            "TestCompileToGeneratedMethod",
            Modifier.PUBLIC | Modifier.FINAL,
            Types.Object,
            Type.list(Type.of(Runnable.class))
        );

        final Expression self = self(typeBuilder);

        final ParameterExpression base = variable(PrimitiveTypes.Integer, "base");
        final ParameterExpression power = variable(PrimitiveTypes.Integer, "power");
        final ParameterExpression accumulator = variable(PrimitiveTypes.Integer, "accumulator");
        final ParameterExpression variable = variable(PrimitiveTypes.Integer, "i");
View Full Code Here

        return OPTIMIZER.visitLambda(node);
    }

    @Override
    protected Expression visitBinary(final BinaryExpression node) {
        Expression reduced = reduceNullConstantComparison(node);

        if (reduced != null) {
            return visit(reduced);
        }
       
View Full Code Here

        return super.visitBinary(node);
    }
   
    @Override
    protected Expression visitUnary(final UnaryExpression node) {
        Expression reduced = reduceNullConstantCheck(node);

        if (reduced != null) {
            return visit(reduced);
        }
View Full Code Here

        return super.visitUnary(node);
    }

    private Expression reduceNullConstantCheck(final UnaryExpression node) {
        final Expression operand = node.getOperand();
        final ExpressionType nodeType = node.getNodeType();
        final ExpressionType operandNodeType = operand.getNodeType();

        if (nodeType == ExpressionType.IsNull) {
            if (ConstantCheck.isNull(operand)) {
                if (operandNodeType == ExpressionType.Parameter ||
                    operandNodeType == ExpressionType.Constant) {
View Full Code Here

        return null;
    }
   
    private Expression reduceDoubleNot(final UnaryExpression node) {
        final Type<?> type = node.getType();
        final Expression operand = node.getOperand();

        if (type != PrimitiveTypes.Boolean || operand.getType() != PrimitiveTypes.Boolean) {
            return null;
        }

        final ExpressionType nodeType = node.getNodeType();
        final ExpressionType operandNodeType = operand.getNodeType();
       
        if ((nodeType == ExpressionType.Not || nodeType == ExpressionType.IsFalse) &&
            (operandNodeType == ExpressionType.Not || operandNodeType == ExpressionType.IsFalse)) {
           
            return ((UnaryExpression)operand).getOperand();
View Full Code Here

        return null;
    }

    private Expression reduceNullConstantComparison(final BinaryExpression node) {
        final Expression left = visit(node.getLeft());
        final Expression right = visit(node.getRight());

        if (node.getType() != PrimitiveTypes.Boolean) {
            return null;
        }
View Full Code Here

        return null;
    }

    private Expression reduceBooleanConstantComparison(final BinaryExpression node) {
        final Expression left = visit(node.getLeft());
        final Expression right = visit(node.getRight());

        final ExpressionType nodeType = node.getNodeType();

        if (node.getType() != PrimitiveTypes.Boolean ||
            nodeType != ExpressionType.Equal && nodeType != ExpressionType.NotEqual) {
            return null;
        }

        if (ConstantCheck.isTrue(right)) {
            // true [op] true
            if (ConstantCheck.isTrue(left)) {
                return constant(nodeType == ExpressionType.Equal);
            }
            // false [op] true
            if (ConstantCheck.isFalse(left)) {
                return constant(nodeType == ExpressionType.NotEqual);
            }
            // expr [op] true
            if (TypeUtils.getUnderlyingPrimitiveOrSelf(left.getType()) == PrimitiveTypes.Boolean) {
                return nodeType == ExpressionType.Equal ? left : isFalse(left);
            }
            return null;
        }

        if (ConstantCheck.isFalse(right)) {
            // false [op] false
            if (ConstantCheck.isFalse(left)) {
                return constant(nodeType == ExpressionType.Equal);
            }
            // true [op] false
            if (ConstantCheck.isTrue(left)) {
                return constant(nodeType == ExpressionType.NotEqual);
            }
            // expr [op] false
            if (TypeUtils.getUnderlyingPrimitiveOrSelf(left.getType()) == PrimitiveTypes.Boolean) {
                return nodeType == ExpressionType.Equal ? isFalse(left) : left;
            }
            return null;
        }

        if (ConstantCheck.isTrue(left)) {
            // true [op] expr
            if (TypeUtils.getUnderlyingPrimitiveOrSelf(right.getType()) == PrimitiveTypes.Boolean) {
                return nodeType == ExpressionType.Equal ? right : isFalse(right);
            }
        }
        else if (ConstantCheck.isFalse(left)) {
            // false [op] expr
            if (TypeUtils.getUnderlyingPrimitiveOrSelf(right.getType()) == PrimitiveTypes.Boolean) {
                return nodeType == ExpressionType.NotEqual ? right : isFalse(right);
            }
        }

        return null;
View Full Code Here

TOP

Related Classes of com.strobel.expressions.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.