Search in sources :

Example 31 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 32 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 33 with BlockStatement

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

the class ToStringASTTransformation method appendValue.

private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls) {
    final BlockStatement thenBlock = new BlockStatement();
    final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock;
    appendCommaIfNotFirst(thenBlock, result, first);
    appendPrefix(thenBlock, result, name, includeNames);
    thenBlock.addStatement(ifElseS(sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value))));
    body.addStatement(appendValue);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 34 with BlockStatement

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

the class ToStringASTTransformation method toStringPropertyName.

private static Statement toStringPropertyName(Expression result, String fName) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(appendS(result, constX(fName + ":")));
    return body;
}
Also used : BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 35 with BlockStatement

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

the class TupleConstructorASTTransformation method createConstructor.

public static void createConstructor(ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, boolean callSuper, boolean force, List<String> excludes, List<String> includes, boolean useSetters) {
    // no processing if existing constructors found
    List<ConstructorNode> constructors = cNode.getDeclaredConstructors();
    if (constructors.size() > 1 && !force)
        return;
    boolean foundEmpty = constructors.size() == 1 && constructors.get(0).getFirstStatement() == null;
    if (constructors.size() == 1 && !foundEmpty && !force)
        return;
    // HACK: JavaStubGenerator could have snuck in a constructor we don't want
    if (foundEmpty)
        constructors.remove(0);
    List<FieldNode> superList = new ArrayList<FieldNode>();
    if (includeSuperProperties) {
        superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
    }
    if (includeSuperFields) {
        superList.addAll(getSuperNonPropertyFields(cNode.getSuperClass()));
    }
    List<FieldNode> list = new ArrayList<FieldNode>();
    if (includeProperties) {
        list.addAll(getInstancePropertyFields(cNode));
    }
    if (includeFields) {
        list.addAll(getInstanceNonPropertyFields(cNode));
    }
    final List<Parameter> params = new ArrayList<Parameter>();
    final List<Expression> superParams = new ArrayList<Expression>();
    final BlockStatement body = new BlockStatement();
    for (FieldNode fNode : superList) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes))
            continue;
        params.add(createParam(fNode, name));
        boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
        if (callSuper) {
            superParams.add(varX(name));
        } else {
            if (useSetters && hasSetter) {
                body.addStatement(stmt(callThisX(getSetterName(name), varX(name))));
            } else {
                body.addStatement(assignS(propX(varX("this"), name), varX(name)));
            }
        }
    }
    if (callSuper) {
        body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams))));
    }
    for (FieldNode fNode : list) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes))
            continue;
        Parameter nextParam = createParam(fNode, name);
        params.add(nextParam);
        boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
        if (useSetters && hasSetter) {
            body.addStatement(stmt(callThisX(getSetterName(name), varX(nextParam))));
        } else {
            body.addStatement(assignS(propX(varX("this"), name), varX(nextParam)));
        }
    }
    cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params.toArray(new Parameter[params.size()]), ClassNode.EMPTY_ARRAY, body));
    // or if there is only one Map property (for backwards compatibility)
    if (params.size() > 0) {
        ClassNode firstParam = params.get(0).getType();
        if (params.size() > 1 || firstParam.equals(ClassHelper.OBJECT_TYPE)) {
            if (firstParam.equals(ClassHelper.MAP_TYPE)) {
                addMapConstructors(cNode, true, "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.");
            } else {
                ClassNode candidate = HMAP_TYPE;
                while (candidate != null) {
                    if (candidate.equals(firstParam)) {
                        addMapConstructors(cNode, true, "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.");
                        break;
                    }
                    candidate = candidate.getSuperClass();
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)199 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)85 Statement (org.codehaus.groovy.ast.stmt.Statement)81 Expression (org.codehaus.groovy.ast.expr.Expression)72 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 MethodNode (org.codehaus.groovy.ast.MethodNode)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)51 Parameter (org.codehaus.groovy.ast.Parameter)50 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)45 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)41 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)41 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)38 FieldNode (org.codehaus.groovy.ast.FieldNode)37 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)33 ArrayList (java.util.ArrayList)31 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)27 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)27 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)25