Search in sources :

Example 51 with ArgumentListExpression

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

the class ListExpressionTransformer method transformRegularConstructor.

private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
    // can be replaced with a direct constructor call
    List<Expression> transformedArgs = transformArguments(expr);
    ConstructorCallExpression cce = new ConstructorCallExpression(target.getDeclaringClass(), new ArgumentListExpression(transformedArgs));
    cce.setSourcePosition(expr);
    cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
    return cce;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 52 with ArgumentListExpression

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

the class MapConstructorASTTransformation method assignField.

private static void assignField(boolean useSetters, Parameter map, BlockStatement body, String name) {
    ArgumentListExpression nameArg = args(constX(name));
    body.addStatement(ifS(callX(varX(map), "containsKey", nameArg), useSetters ? stmt(callThisX(getSetterName(name), callX(varX(map), "get", nameArg))) : assignS(propX(varX("this"), name), callX(varX(map), "get", nameArg))));
}
Also used : ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 53 with ArgumentListExpression

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

the class AbstractTypeCheckingExtension method argTypesMatches.

public boolean argTypesMatches(MethodCall call, Class... classes) {
    ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments());
    ClassNode[] argumentTypes = typeCheckingVisitor.getArgumentTypes(argumentListExpression);
    return argTypesMatches(argumentTypes, classes);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 54 with ArgumentListExpression

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

the class MarkupBuilderCodeTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression bin) {
    Expression left = bin.getLeftExpression();
    Expression right = bin.getRightExpression();
    boolean assignment = bin.getOperation().getType() == Types.ASSIGN;
    if (assignment && left instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) left;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            String varName = var.getName();
            if (!"modelTypes".equals(varName)) {
                MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
                callGetModel.setImplicitThis(true);
                callGetModel.setSourcePosition(left);
                MethodCallExpression mce = new MethodCallExpression(callGetModel, "put", new ArgumentListExpression(new ConstantExpression(varName), right));
                mce.setSourcePosition(left);
                mce.setImplicitThis(false);
                return transform(mce);
            }
        }
    }
    if (assignment && left instanceof VariableExpression && right instanceof ClosureExpression) {
        VariableExpression var = (VariableExpression) left;
        if ("modelTypes".equals(var.getName())) {
            // template declaring its expected types from model directly
            // modelTypes = {
            //  List<String> items
            //  ...
            // }
            Map<String, ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression) right);
            Expression result = new EmptyExpression();
            result.setSourcePosition(bin);
            classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
            return result;
        }
    }
    return super.transform(bin);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression)

Example 55 with ArgumentListExpression

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

the class MarkupBuilderCodeTransformer method tryTransformInclude.

private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                // not a valid import type, do not modify the code
                }
            }
        }
    }
    return super.transform(exp);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Aggregations

ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)108 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)73 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)72 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)66 Expression (org.codehaus.groovy.ast.expr.Expression)61 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)50 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)49 ClassNode (org.codehaus.groovy.ast.ClassNode)38 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)38 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)36 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)34 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)33 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)32 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)28 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)28 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)28 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)27 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)26 MethodNode (org.codehaus.groovy.ast.MethodNode)23 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)22