Search in sources :

Example 56 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.

the class DefaultStrategy method initializeInstance.

private static Expression initializeInstance(ClassNode buildee, List<FieldNode> fields, BlockStatement body) {
    Expression instance = varX("_the" + buildee.getNameWithoutPackage(), buildee);
    body.addStatement(declS(instance, ctorX(buildee)));
    for (FieldNode field : fields) {
        body.addStatement(stmt(assignX(propX(instance, field.getName()), varX(field))));
    }
    return instance;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) Expression(org.codehaus.groovy.ast.expr.Expression)

Example 57 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.

the class ExternalStrategy method initializeInstance.

private static Expression initializeInstance(ClassNode sourceClass, List<PropertyInfo> props, BlockStatement body) {
    Expression instance = varX("_the" + sourceClass.getNameWithoutPackage(), sourceClass);
    body.addStatement(declS(instance, ctorX(sourceClass)));
    for (PropertyInfo prop : props) {
        body.addStatement(stmt(assignX(propX(instance, prop.getName()), varX(prop.getName().equals("class") ? "clazz" : prop.getName(), newClass(prop.getType())))));
    }
    return instance;
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression)

Example 58 with Expression

use of org.codehaus.groovy.ast.expr.Expression 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 59 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.

the class ConstructorCallTransformer method transformConstructorCall.

Expression transformConstructorCall(final ConstructorCallExpression expr) {
    ConstructorNode node = (ConstructorNode) expr.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
    if (node == null)
        return expr;
    if (node.getParameters().length == 1 && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(node.getParameters()[0].getType(), ClassHelper.MAP_TYPE) && node.getCode() == StaticTypeCheckingVisitor.GENERATED_EMPTY_STATEMENT) {
        Expression arguments = expr.getArguments();
        if (arguments instanceof TupleExpression) {
            TupleExpression tupleExpression = (TupleExpression) arguments;
            List<Expression> expressions = tupleExpression.getExpressions();
            if (expressions.size() == 1) {
                Expression expression = expressions.get(0);
                if (expression instanceof MapExpression) {
                    MapExpression map = (MapExpression) expression;
                    // check that the node doesn't belong to the list of declared constructors
                    ClassNode declaringClass = node.getDeclaringClass();
                    for (ConstructorNode constructorNode : declaringClass.getDeclaredConstructors()) {
                        if (constructorNode == node) {
                            return staticCompilationTransformer.superTransform(expr);
                        }
                    }
                    // replace this call with a call to <init>() + appropriate setters
                    // for example, foo(x:1, y:2) is replaced with:
                    // { def tmp = new Foo(); tmp.x = 1; tmp.y = 2; return tmp }()
                    MapStyleConstructorCall result = new MapStyleConstructorCall(staticCompilationTransformer, declaringClass, map, expr);
                    return result;
                }
            }
        }
    }
    return staticCompilationTransformer.superTransform(expr);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ClassNode(org.codehaus.groovy.ast.ClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) Expression(org.codehaus.groovy.ast.expr.Expression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 60 with Expression

use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.

the class ListExpressionTransformer method transformRegularConstructor.

private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
    // can be replaced with a direct constructor call
    List<Expression> transformedArgs = transformArguments(expr);
    ConstructorCallExpression cce = new ConstructorCallExpression(target.getDeclaringClass(), new ArgumentListExpression(transformedArgs));
    cce.setSourcePosition(expr);
    cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
    return cce;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Aggregations

Expression (org.codehaus.groovy.ast.expr.Expression)369 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)257 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)244 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)187 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)187 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)152 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)151 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)149 ClassNode (org.codehaus.groovy.ast.ClassNode)146 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)134 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)130 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)113 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)103 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)95 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)84 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)78 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)63 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)63 Statement (org.codehaus.groovy.ast.stmt.Statement)63 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)61