Search in sources :

Example 71 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class BindableASTTransformation method wrapSetterMethod.

/*
     * Wrap an existing setter.
     */
private 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 72 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class InitializerStrategy method createBuilderMethod.

private static MethodNode createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(returnS(callX(builder, buildMethodName)));
    ClassNode returnType = makeClassSafeWithGenerics(builder, unsetGenTypes(numFields));
    return new MethodNode(builderMethodName, PUBLIC_STATIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 73 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ExternalStrategy method createBuildMethod.

private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<PropertyInfo> fields) {
    String buildMethodName = transform.getMemberStringValue(anno, "buildMethodName", "build");
    final BlockStatement body = new BlockStatement();
    Expression sourceClassInstance = initializeInstance(sourceClass, fields, body);
    body.addStatement(returnS(sourceClassInstance));
    return new MethodNode(buildMethodName, ACC_PUBLIC, sourceClass, NO_PARAMS, NO_EXCEPTIONS, body);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 74 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ClassNode method positionStmtsAfterEnumInitStmts.

public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
    MethodNode method = getOrAddStaticConstructorNode();
    Statement statement = method.getCode();
    if (statement instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) statement;
        // add given statements for explicitly declared static fields just after enum-special fields
        // are found - the $VALUES binary expression marks the end of such fields.
        List<Statement> blockStatements = block.getStatements();
        ListIterator<Statement> litr = blockStatements.listIterator();
        while (litr.hasNext()) {
            Statement stmt = litr.next();
            if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
                BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
                if (bExp.getLeftExpression() instanceof FieldExpression) {
                    FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
                    if (fExp.getFieldName().equals("$VALUES")) {
                        for (Statement tmpStmt : staticFieldStatements) {
                            litr.add(tmpStmt);
                        }
                    }
                }
            }
        }
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 75 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ClassNode method addStaticInitializerStatements.

public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
    MethodNode method = getOrAddStaticConstructorNode();
    BlockStatement block = null;
    Statement statement = method.getCode();
    if (statement == null) {
        block = new BlockStatement();
    } else if (statement instanceof BlockStatement) {
        block = (BlockStatement) statement;
    } else {
        block = new BlockStatement();
        block.addStatement(statement);
    }
    // before the other statements
    if (!fieldInit) {
        block.addStatements(staticStatements);
    } else {
        List<Statement> blockStatements = block.getStatements();
        staticStatements.addAll(blockStatements);
        blockStatements.clear();
        blockStatements.addAll(staticStatements);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)212 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)94 Statement (org.codehaus.groovy.ast.stmt.Statement)87 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)76 Expression (org.codehaus.groovy.ast.expr.Expression)75 MethodNode (org.codehaus.groovy.ast.MethodNode)64 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)62 Parameter (org.codehaus.groovy.ast.Parameter)58 ClassNode (org.codehaus.groovy.ast.ClassNode)54 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)51 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)45 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)45 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)44 FieldNode (org.codehaus.groovy.ast.FieldNode)38 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)37 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)33 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)33 ArrayList (java.util.ArrayList)32 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)28