Search in sources :

Example 81 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 82 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 83 with BlockStatement

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

the class TupleConstructorASTTransformation method processArgsBlock.

private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
    BlockStatement block = new BlockStatement();
    for (PropertyNode pNode : cNode.getProperties()) {
        if (pNode.isStatic())
            continue;
        // if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
        Statement ifStatement = ifS(callX(namedArgs, "containsKey", constX(pNode.getName())), assignS(varX(pNode), propX(namedArgs, pNode.getName())));
        block.addStatement(ifStatement);
    }
    block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
    return block;
}
Also used : PropertyNode(org.codehaus.groovy.ast.PropertyNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 84 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)

Example 85 with BlockStatement

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

the class ImmutableASTTransformation method createCopyWith.

private void createCopyWith(final ClassNode cNode, final List<PropertyNode> pList) {
    BlockStatement body = new BlockStatement();
    body.addStatement(ifS(orX(equalsNullX(varX("map", ClassHelper.MAP_TYPE)), eqX(callX(varX("map", HASHMAP_TYPE), "size"), constX(0))), returnS(varX("this", cNode))));
    body.addStatement(declS(varX("dirty", ClassHelper.boolean_TYPE), ConstantExpression.PRIM_FALSE));
    body.addStatement(declS(varX("construct", HASHMAP_TYPE), ctorX(HASHMAP_TYPE)));
    // Check for each property
    for (final PropertyNode pNode : pList) {
        body.addStatement(createCheckForProperty(pNode));
    }
    body.addStatement(returnS(ternaryX(isTrueX(varX("dirty", ClassHelper.boolean_TYPE)), ctorX(cNode, args(varX("construct", HASHMAP_TYPE))), varX("this", cNode))));
    final ClassNode clonedNode = cNode.getPlainNodeReference();
    cNode.addMethod(COPY_WITH_METHOD, ACC_PUBLIC | ACC_FINAL, clonedNode, params(new Parameter(new ClassNode(Map.class), "map")), null, body);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap)

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