Search in sources :

Example 91 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.

the class SqlWhereVisitor method visitBinaryExpression.

public void visitBinaryExpression(BinaryExpression expression) {
    Expression left = expression.getLeftExpression();
    Expression right = expression.getRightExpression();
    boolean leaf = (right instanceof ConstantExpression || left instanceof ConstantExpression);
    if (!leaf)
        buffer.append("(");
    left.visit(this);
    buffer.append(" ");
    Token token = expression.getOperation();
    buffer.append(tokenAsSql(token));
    buffer.append(" ");
    right.visit(this);
    if (!leaf)
        buffer.append(")");
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Token(org.codehaus.groovy.syntax.Token)

Example 92 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project spring-boot by spring-projects.

the class GenericBomAstTransformation method addDependencyManagementBom.

private void addDependencyManagementBom(ModuleNode node, String module) {
    AnnotatedNode annotated = getAnnotatedNode(node);
    if (annotated != null) {
        AnnotationNode bom = getAnnotation(annotated);
        List<Expression> expressions = new ArrayList<>(getConstantExpressions(bom.getMember("value")));
        expressions.add(new ConstantExpression(module));
        bom.setMember("value", new ListExpression(expressions));
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList)

Example 93 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.

the class SuperCallTraitTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression exp) {
    Expression trn = super.transform(exp);
    if (trn instanceof BinaryExpression) {
        BinaryExpression bin = (BinaryExpression) trn;
        Expression leftExpression = bin.getLeftExpression();
        if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) {
            ClassNode traitReceiver = null;
            PropertyExpression leftPropertyExpression = (PropertyExpression) leftExpression;
            if (isTraitSuperPropertyExpression(leftPropertyExpression.getObjectExpression())) {
                PropertyExpression pexp = (PropertyExpression) leftPropertyExpression.getObjectExpression();
                traitReceiver = pexp.getObjectExpression().getType();
            }
            if (traitReceiver != null) {
                // A.super.foo = ...
                TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
                ClassNode helper = helpers.getHelper();
                String setterName = MetaProperty.getSetterName(leftPropertyExpression.getPropertyAsString());
                List<MethodNode> methods = helper.getMethods(setterName);
                for (MethodNode method : methods) {
                    Parameter[] parameters = method.getParameters();
                    if (parameters.length == 2 && parameters[0].getType().equals(traitReceiver)) {
                        ArgumentListExpression args = new ArgumentListExpression(new VariableExpression("this"), transform(exp.getRightExpression()));
                        MethodCallExpression setterCall = new MethodCallExpression(new ClassExpression(helper), setterName, args);
                        setterCall.setMethodTarget(method);
                        setterCall.setImplicitThis(false);
                        return setterCall;
                    }
                }
                return bin;
            }
        }
    }
    return trn;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 94 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.

the class Verifier method addFieldInitialization.

protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
    Expression expression = fieldNode.getInitialExpression();
    if (expression != null) {
        final FieldExpression fe = new FieldExpression(fieldNode);
        if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
            fe.setUseReferenceDirectly(true);
        }
        ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
        if (fieldNode.isStatic()) {
            // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
            // initialized first so that code dependent on it does not see their values as empty
            Expression initialValueExpression = fieldNode.getInitialValueExpression();
            if (initialValueExpression instanceof ConstantExpression) {
                ConstantExpression cexp = (ConstantExpression) initialValueExpression;
                cexp = transformToPrimitiveConstantIfPossible(cexp);
                if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
                    // GROOVY-5150: primitive type constants will be initialized directly
                    return;
                }
                staticList.add(0, statement);
            } else {
                staticList.add(statement);
            }
            // to avoid double initialization in case of several constructors
            fieldNode.setInitialValueExpression(null);
            /*
                 * If it is a statement for an explicitly declared static field inside an enum, store its
                 * reference. For enums, they need to be handled differently as such init statements should
                 * come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
                 */
            if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
                initStmtsAfterEnumValuesInit.add(statement);
            }
        } else {
            list.add(statement);
        }
    }
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 95 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy by apache.

the class BinaryExpressionHelper method evaluateBinaryExpressionWithAssignment.

protected void evaluateBinaryExpressionWithAssignment(String method, BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    AsmClassGenerator acg = controller.getAcg();
    OperandStack operandStack = controller.getOperandStack();
    if (leftExpression instanceof BinaryExpression) {
        BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
        if (leftBinExpr.getOperation().getType() == Types.LEFT_SQUARE_BRACKET) {
            evaluateArrayAssignmentWithOperator(method, expression, leftBinExpr);
            return;
        }
    }
    evaluateBinaryExpression(method, expression);
    // br to leave a copy of rvalue on the stack. see also isPopRequired()
    operandStack.dup();
    controller.getCompileStack().pushLHS(true);
    leftExpression.visit(acg);
    controller.getCompileStack().popLHS();
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Aggregations

Expression (org.codehaus.groovy.ast.expr.Expression)369 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)257 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)244 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)187 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)187 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)152 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)151 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)149 ClassNode (org.codehaus.groovy.ast.ClassNode)146 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)134 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)130 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)113 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)103 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)95 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)84 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)78 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)63 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)63 Statement (org.codehaus.groovy.ast.stmt.Statement)63 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)61