Search in sources :

Example 61 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class TraitComposer method doCreateSuperForwarder.

/**
     * Creates a method to dispatch to "super traits" in a "stackable" fashion. The generated method looks like this:
     * <p>
     * <code>ReturnType trait$super$method(Class clazz, Arg1 arg1, Arg2 arg2, ...) {
     *     if (SomeTrait.is(A) { return SomeOtherTrait$Trait$Helper.method(this, arg1, arg2) }
     *     super.method(arg1,arg2)
     * }</code>
     * </p>
     * @param targetNode
     * @param forwarderMethod
     * @param interfacesToGenerateForwarderFor
     * @param genericsSpec
     */
private static void doCreateSuperForwarder(ClassNode targetNode, MethodNode forwarderMethod, ClassNode[] interfacesToGenerateForwarderFor, Map<String, ClassNode> genericsSpec) {
    Parameter[] parameters = forwarderMethod.getParameters();
    Parameter[] superForwarderParams = new Parameter[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        ClassNode originType = parameter.getOriginType();
        superForwarderParams[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, originType), parameter.getName());
    }
    for (int i = 0; i < interfacesToGenerateForwarderFor.length; i++) {
        final ClassNode current = interfacesToGenerateForwarderFor[i];
        final ClassNode next = i < interfacesToGenerateForwarderFor.length - 1 ? interfacesToGenerateForwarderFor[i + 1] : null;
        String forwarderName = Traits.getSuperTraitMethodName(current, forwarderMethod.getName());
        if (targetNode.getDeclaredMethod(forwarderName, superForwarderParams) == null) {
            ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, forwarderMethod.getReturnType());
            Statement delegate = next == null ? createSuperFallback(forwarderMethod, returnType) : createDelegatingForwarder(forwarderMethod, next);
            MethodNode methodNode = targetNode.addMethod(forwarderName, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, returnType, superForwarderParams, ClassNode.EMPTY_ARRAY, delegate);
            methodNode.setGenericsTypes(forwarderMethod.getGenericsTypes());
        }
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) 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) Parameter(org.codehaus.groovy.ast.Parameter)

Example 62 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class StatementWriter method visitExpressionOrStatement.

private void visitExpressionOrStatement(Object o) {
    if (o == EmptyExpression.INSTANCE)
        return;
    if (o instanceof Expression) {
        Expression expr = (Expression) o;
        int mark = controller.getOperandStack().getStackLength();
        expr.visit(controller.getAcg());
        controller.getOperandStack().popDownTo(mark);
    } else {
        ((Statement) o).visit(controller.getAcg());
    }
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) 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) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Example 63 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class BindableASTTransformation method wrapSetterMethod.

/*
     * Wrap an existing setter.
     */
private static void wrapSetterMethod(ClassNode classNode, String propertyName) {
    String getterName = "get" + MetaClassHelper.capitalize(propertyName);
    MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
    if (setter != null) {
        // Get the existing code block
        Statement code = setter.getCode();
        Expression oldValue = varX("$oldValue");
        Expression newValue = varX("$newValue");
        BlockStatement block = new BlockStatement();
        // create a local variable to hold the old value from the getter
        block.addStatement(declS(oldValue, callThisX(getterName)));
        // call the existing block, which will presumably set the value properly
        block.addStatement(code);
        // get the new value to emit in the event
        block.addStatement(declS(newValue, callThisX(getterName)));
        // add the firePropertyChange method call
        block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
        // replace the existing code block with our new one
        setter.setCode(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 64 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class BindableASTTransformation method createListenerSetter.

private void createListenerSetter(ClassNode classNode, PropertyNode propertyNode) {
    String setterName = "set" + MetaClassHelper.capitalize(propertyNode.getName());
    if (classNode.getMethods(setterName).isEmpty()) {
        Statement setterBlock = createBindableStatement(propertyNode, fieldX(propertyNode.getField()));
        // create method void <setter>(<type> fieldName)
        createSetterMethod(classNode, propertyNode, setterName, setterBlock);
    } else {
        wrapSetterMethod(classNode, propertyNode.getName());
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 65 with Statement

use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.

the class VetoableASTTransformation method wrapSetterMethod.

/**
     * Wrap an existing setter.
     */
private static void wrapSetterMethod(ClassNode classNode, boolean bindable, String propertyName) {
    String getterName = "get" + MetaClassHelper.capitalize(propertyName);
    MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
    if (setter != null) {
        // Get the existing code block
        Statement code = setter.getCode();
        Expression oldValue = varX("$oldValue");
        Expression newValue = varX("$newValue");
        Expression proposedValue = varX(setter.getParameters()[0].getName());
        BlockStatement block = new BlockStatement();
        // create a local variable to hold the old value from the getter
        block.addStatement(declS(oldValue, callThisX(getterName)));
        // add the fireVetoableChange method call
        block.addStatement(stmt(callThisX("fireVetoableChange", args(constX(propertyName), oldValue, proposedValue))));
        // call the existing block, which will presumably set the value properly
        block.addStatement(code);
        if (bindable) {
            // get the new value to emit in the event
            block.addStatement(declS(newValue, callThisX(getterName)));
            // add the firePropertyChange method call
            block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
        }
        // replace the existing code block with our new one
        setter.setCode(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

Statement (org.codehaus.groovy.ast.stmt.Statement)205 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)167 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)121 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)90 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)79 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)71 Expression (org.codehaus.groovy.ast.expr.Expression)63 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)54 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)53 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)53 ClassNode (org.codehaus.groovy.ast.ClassNode)50 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)50 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)40 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)39 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)39 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)38 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)38 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)38