Search in sources :

Example 91 with MethodCallExpression

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

the class ControllerActionTransformer method addMethodToInvokeClosure.

protected void addMethodToInvokeClosure(ClassNode controllerClassNode, PropertyNode closureProperty, SourceUnit source, GeneratorContext context) {
    MethodNode method = controllerClassNode.getMethod(closureProperty.getName(), ZERO_PARAMETERS);
    if (method == null || !method.getDeclaringClass().equals(controllerClassNode)) {
        ClosureExpression closureExpression = (ClosureExpression) closureProperty.getInitialExpression();
        final Parameter[] parameters = closureExpression.getParameters();
        final BlockStatement newMethodCode = initializeActionParameters(controllerClassNode, closureProperty, closureProperty.getName(), parameters, source, context);
        final ArgumentListExpression closureInvocationArguments = new ArgumentListExpression();
        if (parameters != null) {
            for (Parameter p : parameters) {
                closureInvocationArguments.addExpression(new VariableExpression(p.getName()));
            }
        }
        final MethodCallExpression methodCallExpression = new MethodCallExpression(closureExpression, "call", closureInvocationArguments);
        newMethodCode.addStatement(new ExpressionStatement(applyMethodTarget(methodCallExpression, Closure.class, Object.class)));
        final MethodNode methodNode = new MethodNode(closureProperty.getName(), Modifier.PUBLIC, new ClassNode(Object.class), ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, newMethodCode);
        wrapMethodBodyWithExceptionHandling(controllerClassNode, methodNode);
        annotateActionMethod(controllerClassNode, parameters, methodNode);
        controllerClassNode.addMethod(methodNode);
    }
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) RequestParameter(grails.web.RequestParameter) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 92 with MethodCallExpression

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

the class TestMixinTransformation method addJunitRuleFields.

protected void addJunitRuleFields(ClassNode classNode) {
    if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
        return;
    }
    ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
    FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
    boolean spockTest = isSpockTest(classNode);
    FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
    AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
    if (spockTest) {
        // @ClassRule must be added to @Shared field in spock
        FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
        spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
        spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
        addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
    } else {
        staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
        staticRuleFieldNode.addAnnotation(classRuleAnnotation);
    }
    FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
    ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
    if (spockTest) {
        addSpockFieldMetadata(ruleFieldNode, 0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) TestRule(org.junit.rules.TestRule) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassRule(org.junit.ClassRule) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 93 with MethodCallExpression

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

the class Verifier method visitConstructor.

public void visitConstructor(ConstructorNode node) {
    CodeVisitorSupport checkSuper = new CodeVisitorSupport() {

        boolean firstMethodCall = true;

        String type = null;

        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!firstMethodCall)
                return;
            firstMethodCall = false;
            String name = call.getMethodAsString();
            // the name might be null if the method name is a GString for example
            if (name == null)
                return;
            if (!name.equals("super") && !name.equals("this"))
                return;
            type = name;
            call.getArguments().visit(this);
            type = null;
        }

        public void visitConstructorCallExpression(ConstructorCallExpression call) {
            if (!call.isSpecialCall())
                return;
            type = call.getText();
            call.getArguments().visit(this);
            type = null;
        }

        public void visitVariableExpression(VariableExpression expression) {
            if (type == null)
                return;
            String name = expression.getName();
            if (!name.equals("this") && !name.equals("super"))
                return;
            throw new RuntimeParserException("cannot reference " + name + " inside of " + type + "(....) before supertype constructor has been called", expression);
        }
    };
    Statement s = node.getCode();
    if (s == null) {
        return;
    } else {
        s.visit(new VerifierCodeVisitor(this));
    }
    s.visit(checkSuper);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

Example 94 with MethodCallExpression

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

the class BinaryExpressionHelper method evaluateArrayAssignmentWithOperator.

protected void evaluateArrayAssignmentWithOperator(String method, BinaryExpression expression, BinaryExpression leftBinExpr) {
    CompileStack compileStack = getController().getCompileStack();
    AsmClassGenerator acg = getController().getAcg();
    OperandStack os = getController().getOperandStack();
    // e.g. x[a] += b
    // to avoid loading x and a twice we transform the expression to use
    // ExpressionAsVariableSlot
    // -> subscript=a, receiver=x, receiver[subscript]+b, =, receiver[subscript]
    // -> subscript=a, receiver=x, receiver#getAt(subscript)#plus(b), =, receiver#putAt(subscript)
    // -> subscript=a, receiver=x, receiver#putAt(subscript, receiver#getAt(subscript)#plus(b))
    // the result of x[a] += b is x[a]+b, thus:
    // -> subscript=a, receiver=x, receiver#putAt(subscript, ret=receiver#getAt(subscript)#plus(b)), ret
    ExpressionAsVariableSlot subscript = new ExpressionAsVariableSlot(controller, leftBinExpr.getRightExpression(), "subscript");
    ExpressionAsVariableSlot receiver = new ExpressionAsVariableSlot(controller, leftBinExpr.getLeftExpression(), "receiver");
    MethodCallExpression getAt = new MethodCallExpression(receiver, "getAt", new ArgumentListExpression(subscript));
    MethodCallExpression operation = new MethodCallExpression(getAt, method, expression.getRightExpression());
    ExpressionAsVariableSlot ret = new ExpressionAsVariableSlot(controller, operation, "ret");
    MethodCallExpression putAt = new MethodCallExpression(receiver, "putAt", new ArgumentListExpression(subscript, ret));
    putAt.visit(acg);
    os.pop();
    os.load(ret.getType(), ret.getIndex());
    compileStack.removeVar(ret.getIndex());
    compileStack.removeVar(subscript.getIndex());
    compileStack.removeVar(receiver.getIndex());
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AsmClassGenerator(org.codehaus.groovy.classgen.AsmClassGenerator)

Example 95 with MethodCallExpression

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

the class StatementWriter method writeForInLoop.

protected void writeForInLoop(ForStatement loop) {
    controller.getAcg().onLineNumber(loop, "visitForLoop");
    writeStatementLabel(loop);
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
    // Declare the loop counter.
    BytecodeVariable variable = compileStack.defineVariable(loop.getVariable(), false);
    // Then get the iterator and generate the loop control
    MethodCallExpression iterator = new MethodCallExpression(loop.getCollectionExpression(), "iterator", new ArgumentListExpression());
    iterator.visit(controller.getAcg());
    operandStack.doGroovyCast(ClassHelper.Iterator_TYPE);
    final int iteratorIdx = compileStack.defineTemporaryVariable("iterator", ClassHelper.Iterator_TYPE, true);
    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();
    mv.visitLabel(continueLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorHasNext(mv);
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    mv.visitJumpInsn(IFEQ, breakLabel);
    mv.visitVarInsn(ALOAD, iteratorIdx);
    writeIteratorNext(mv);
    operandStack.push(ClassHelper.OBJECT_TYPE);
    operandStack.storeVar(variable);
    // Generate the loop body
    loop.getLoopBlock().visit(controller.getAcg());
    mv.visitJumpInsn(GOTO, continueLabel);
    mv.visitLabel(breakLabel);
    compileStack.pop();
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Label(org.objectweb.asm.Label) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)131 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)89 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)81 Expression (org.codehaus.groovy.ast.expr.Expression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)73 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)58 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)51 ClassNode (org.codehaus.groovy.ast.ClassNode)43 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)42 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)40 MethodNode (org.codehaus.groovy.ast.MethodNode)36 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)36 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)36 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)35 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)34 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)31 FieldNode (org.codehaus.groovy.ast.FieldNode)26 Parameter (org.codehaus.groovy.ast.Parameter)23