Search in sources :

Example 1 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement 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 2 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement 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)

Example 3 with BlockStatement

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

the class GrabAnnotationTransformation method callGrabAsStaticInitIfNeeded.

private void callGrabAsStaticInitIfNeeded(ClassNode classNode, ClassNode grapeClassNode, List<Map<String, Object>> grabMapsInit, List<Map<String, Object>> grabExcludeMaps) {
    List<Statement> grabInitializers = new ArrayList<Statement>();
    MapExpression basicArgs = new MapExpression();
    if (autoDownload != null) {
        basicArgs.addMapEntryExpression(constX(AUTO_DOWNLOAD_SETTING), constX(autoDownload));
    }
    if (disableChecksums != null) {
        basicArgs.addMapEntryExpression(constX(DISABLE_CHECKSUMS_SETTING), constX(disableChecksums));
    }
    if (systemProperties != null && !systemProperties.isEmpty()) {
        BlockStatement block = new BlockStatement();
        for (Map.Entry e : systemProperties.entrySet()) {
            block.addStatement(stmt(callX(SYSTEM_CLASSNODE, "setProperty", args(constX(e.getKey()), constX(e.getValue())))));
        }
        StaticMethodCallExpression enabled = callX(SYSTEM_CLASSNODE, "getProperty", args(constX("groovy.grape.enable"), constX("true")));
        grabInitializers.add(ifS(eqX(enabled, constX("true")), block));
    }
    if (!grabExcludeMaps.isEmpty()) {
        ListExpression list = new ListExpression();
        for (Map<String, Object> map : grabExcludeMaps) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            MapExpression inner = new MapExpression();
            for (Map.Entry<String, Object> entry : entries) {
                inner.addMapEntryExpression(constX(entry.getKey()), constX(entry.getValue()));
            }
            list.addExpression(inner);
        }
        basicArgs.addMapEntryExpression(constX("excludes"), list);
    }
    List<Expression> argList = new ArrayList<Expression>();
    argList.add(basicArgs);
    if (grabMapsInit.isEmpty())
        return;
    for (Map<String, Object> grabMap : grabMapsInit) {
        // add Grape.grab(excludeArgs, [group:group, module:module, version:version, classifier:classifier])
        // or Grape.grab([group:group, module:module, version:version, classifier:classifier])
        MapExpression dependencyArg = new MapExpression();
        for (String s : GRAB_REQUIRED) {
            dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
        }
        for (String s : GRAB_OPTIONAL) {
            if (grabMap.containsKey(s))
                dependencyArg.addMapEntryExpression(constX(s), constX(grabMap.get(s)));
        }
        argList.add(dependencyArg);
    }
    grabInitializers.add(stmt(callX(grapeClassNode, "grab", args(argList))));
    // insert at beginning so we have the classloader set up before the class is called
    classNode.addStaticInitializerStatements(grabInitializers, true);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with BlockStatement

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

the class DefaultStrategy method createBuildMethod.

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

Example 5 with BlockStatement

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

the class JavaAwareResolveVisitor method getConstructorCall.

private static Expression getConstructorCall(Statement code) {
    if (code == null)
        return null;
    if (code instanceof BlockStatement) {
        BlockStatement bs = (BlockStatement) code;
        if (bs.isEmpty())
            return null;
        return getConstructorCall(bs.getStatements().get(0));
    }
    if (!(code instanceof ExpressionStatement))
        return null;
    ExpressionStatement es = (ExpressionStatement) code;
    Expression exp = es.getExpression();
    if (!(exp instanceof ConstructorCallExpression))
        return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) exp;
    if (!cce.isSpecialCall())
        return null;
    return cce;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)282 Statement (org.codehaus.groovy.ast.stmt.Statement)119 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)107 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)105 Expression (org.codehaus.groovy.ast.expr.Expression)103 MethodNode (org.codehaus.groovy.ast.MethodNode)83 Parameter (org.codehaus.groovy.ast.Parameter)76 ClassNode (org.codehaus.groovy.ast.ClassNode)75 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)74 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)71 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)62 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)60 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)57 ArrayList (java.util.ArrayList)54 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)53 FieldNode (org.codehaus.groovy.ast.FieldNode)49 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)49 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)44 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)44 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)41