Search in sources :

Example 41 with VariableExpression

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

the class MarkupBuilderCodeTransformer method transformMethodCall.

private Expression transformMethodCall(final MethodCallExpression exp) {
    String name = exp.getMethodAsString();
    if (exp.isImplicitThis() && "include".equals(name)) {
        return tryTransformInclude(exp);
    } else if (exp.isImplicitThis() && name.startsWith(":")) {
        List<Expression> args;
        if (exp.getArguments() instanceof ArgumentListExpression) {
            args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
        } else {
            args = Collections.singletonList(exp.getArguments());
        }
        Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
        MethodCallExpression call = new MethodCallExpression(new VariableExpression("this"), "methodMissing", newArguments);
        call.setImplicitThis(true);
        call.setSafe(exp.isSafe());
        call.setSpreadSafe(exp.isSpreadSafe());
        call.setSourcePosition(exp);
        return call;
    } else if (name != null && name.startsWith("$")) {
        MethodCallExpression reformatted = new MethodCallExpression(exp.getObjectExpression(), name.substring(1), exp.getArguments());
        reformatted.setImplicitThis(exp.isImplicitThis());
        reformatted.setSafe(exp.isSafe());
        reformatted.setSpreadSafe(exp.isSpreadSafe());
        reformatted.setSourcePosition(exp);
        // wrap in a stringOf { ... } closure call
        ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
        clos.setVariableScope(new VariableScope());
        MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"), "stringOf", clos);
        stringOf.setImplicitThis(true);
        stringOf.setSourcePosition(reformatted);
        return stringOf;
    }
    return super.transform(exp);
}
Also used : 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArrayList(java.util.ArrayList) List(java.util.List) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 42 with VariableExpression

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

the class MarkupBuilderCodeTransformer method transform.

@Override
public Expression transform(final Expression exp) {
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp instanceof MethodCallExpression) {
        return transformMethodCall((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        ClosureExpression cl = (ClosureExpression) exp;
        cl.getCode().visit(this);
        return cl;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) exp;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
            callGetModel.setImplicitThis(true);
            callGetModel.setSourcePosition(exp);
            String varName = var.getName();
            if ("model".equals(varName) || "unescaped".equals(varName)) {
                return callGetModel;
            }
            MethodCallExpression mce = new MethodCallExpression(callGetModel, "get", new ArgumentListExpression(new ConstantExpression(varName)));
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            MethodCallExpression yield = new MethodCallExpression(new VariableExpression("this"), "tryEscape", new ArgumentListExpression(mce));
            yield.setImplicitThis(true);
            yield.setSourcePosition(exp);
            yield.putNodeMetaData(TARGET_VARIABLE, varName);
            return autoEscape ? yield : mce;
        }
    }
    return super.transform(exp);
}
Also used : BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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)

Example 43 with VariableExpression

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

the class TraitTypeCheckingExtension method handleMissingMethod.

@Override
public List<MethodNode> handleMissingMethod(final ClassNode receiver, final String name, final ArgumentListExpression argumentList, final ClassNode[] argumentTypes, final MethodCall call) {
    String[] decomposed = Traits.decomposeSuperCallName(name);
    if (decomposed != null) {
        return convertToDynamicCall(call, receiver, decomposed, argumentTypes);
    }
    if (call instanceof MethodCallExpression) {
        MethodCallExpression mce = (MethodCallExpression) call;
        if (mce.getReceiver() instanceof VariableExpression) {
            VariableExpression var = (VariableExpression) mce.getReceiver();
            // GROOVY-7322
            // static method call in trait?
            ClassNode type = null;
            if (isStaticTraitReceiver(receiver, var)) {
                type = receiver.getGenericsTypes()[0].getType();
            } else if (isThisTraitReceiver(var)) {
                type = receiver;
            }
            if (type != null && Traits.isTrait(type)) {
                ClassNode helper = Traits.findHelper(type);
                Parameter[] params = new Parameter[argumentTypes.length + 1];
                params[0] = new Parameter(ClassHelper.CLASS_Type.getPlainNodeReference(), "staticSelf");
                for (int i = 1; i < params.length; i++) {
                    params[i] = new Parameter(argumentTypes[i - 1], "p" + i);
                }
                MethodNode method = helper.getDeclaredMethod(name, params);
                if (method != null) {
                    return Collections.singletonList(makeDynamic(call, method.getReturnType()));
                }
            }
        }
        ClassNode dynamic = mce.getNodeMetaData(TraitASTTransformation.DO_DYNAMIC);
        if (dynamic != null) {
            return Collections.singletonList(makeDynamic(call, dynamic));
        }
    }
    return NOTFOUND;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 44 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression 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 45 with VariableExpression

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

the class TraitComposer method createDelegatingForwarder.

private static Statement createDelegatingForwarder(final MethodNode forwarderMethod, final ClassNode next) {
    // generates --> next$Trait$Helper.method(this, arg1, arg2)
    TraitHelpersTuple helpers = Traits.findHelpers(next);
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new VariableExpression("this"));
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    StaticMethodCallExpression delegateCall = new StaticMethodCallExpression(helpers.getHelper(), forwarderMethod.getName(), args);
    Statement result;
    if (ClassHelper.VOID_TYPE.equals(forwarderMethod.getReturnType())) {
        BlockStatement stmt = new BlockStatement();
        stmt.addStatement(new ExpressionStatement(delegateCall));
        stmt.addStatement(new ReturnStatement(new ConstantExpression(null)));
        result = stmt;
    } else {
        result = new ReturnStatement(delegateCall);
    }
    return result;
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)176 Expression (org.codehaus.groovy.ast.expr.Expression)86 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)72 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)67 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)61 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)58 ClassNode (org.codehaus.groovy.ast.ClassNode)52 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)49 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)47 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)47 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)45 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)41 Parameter (org.codehaus.groovy.ast.Parameter)40 MethodNode (org.codehaus.groovy.ast.MethodNode)37 FieldNode (org.codehaus.groovy.ast.FieldNode)36 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)35 Statement (org.codehaus.groovy.ast.stmt.Statement)33