Search in sources :

Example 61 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.

the class InnerClassVisitor method insertThis0ToSuperCall.

private void insertThis0ToSuperCall(final ConstructorCallExpression call, final ClassNode cn) {
    // calculate outer class which we need for this$0
    ClassNode parent = classNode;
    int level = 0;
    for (; parent != null && parent != cn.getOuterClass(); parent = parent.getOuterClass()) {
        level++;
    }
    // if constructor call is not in outer class, don't pass 'this' implicitly. Return.
    if (parent == null)
        return;
    //add this parameter to node
    Expression argsExp = call.getArguments();
    if (argsExp instanceof TupleExpression) {
        TupleExpression argsListExp = (TupleExpression) argsExp;
        Expression this0 = VariableExpression.THIS_EXPRESSION;
        for (int i = 0; i != level; ++i) this0 = new PropertyExpression(this0, "this$0");
        argsListExp.getExpressions().add(0, this0);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 62 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.

the class StaticImportVisitor method transformConstructorCallExpression.

protected Expression transformConstructorCallExpression(ConstructorCallExpression cce) {
    inSpecialConstructorCall = cce.isSpecialCall();
    Expression expression = cce.getArguments();
    if (expression instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) expression;
        if (tuple.getExpressions().size() == 1) {
            expression = tuple.getExpression(0);
            if (expression instanceof NamedArgumentListExpression) {
                NamedArgumentListExpression namedArgs = (NamedArgumentListExpression) expression;
                List<MapEntryExpression> entryExpressions = namedArgs.getMapEntryExpressions();
                for (int i = 0; i < entryExpressions.size(); i++) {
                    entryExpressions.set(i, (MapEntryExpression) transformMapEntryExpression(entryExpressions.get(i), cce.getType()));
                }
            }
        }
    }
    Expression ret = cce.transformExpression(this);
    inSpecialConstructorCall = false;
    return ret;
}
Also used : MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression)

Example 63 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.

the class StaticImportVisitor method transform.

public Expression transform(Expression exp) {
    if (exp == null)
        return null;
    if (exp.getClass() == VariableExpression.class) {
        return transformVariableExpression((VariableExpression) exp);
    }
    if (exp.getClass() == BinaryExpression.class) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp.getClass() == PropertyExpression.class) {
        return transformPropertyExpression((PropertyExpression) exp);
    }
    if (exp.getClass() == MethodCallExpression.class) {
        return transformMethodCallExpression((MethodCallExpression) exp);
    }
    if (exp.getClass() == ClosureExpression.class) {
        return transformClosureExpression((ClosureExpression) exp);
    }
    if (exp.getClass() == ConstructorCallExpression.class) {
        return transformConstructorCallExpression((ConstructorCallExpression) exp);
    }
    if (exp.getClass() == ArgumentListExpression.class) {
        Expression result = exp.transformExpression(this);
        if (inPropertyExpression) {
            foundArgs = result;
        }
        return result;
    }
    if (exp instanceof ConstantExpression) {
        Expression result = exp.transformExpression(this);
        if (inPropertyExpression) {
            foundConstant = result;
        }
        if (inAnnotation && exp instanceof AnnotationConstantExpression) {
            ConstantExpression ce = (ConstantExpression) result;
            if (ce.getValue() instanceof AnnotationNode) {
                // replicate a little bit of AnnotationVisitor here
                // because we can't wait until later to do this
                AnnotationNode an = (AnnotationNode) ce.getValue();
                Map<String, Expression> attributes = an.getMembers();
                for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
                    Expression attrExpr = transform(entry.getValue());
                    entry.setValue(attrExpr);
                }
            }
        }
        return result;
    }
    return exp.transformExpression(this);
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) Map(java.util.Map)

Example 64 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.

the class JavaStubGenerator method printSpecialConstructorArgs.

private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) {
    // Select a constructor from our class, or super-class which is legal to call,
    // then write out an invoke w/nulls using casts to avoid ambiguous crapo
    Parameter[] params = selectAccessibleConstructorFromSuper(node);
    if (params != null) {
        out.print("super (");
        for (int i = 0; i < params.length; i++) {
            printDefaultValue(out, params[i].getType());
            if (i + 1 < params.length) {
                out.print(", ");
            }
        }
        out.println(");");
        return;
    }
    // Otherwise try the older method based on the constructor's call expression
    Expression arguments = constrCall.getArguments();
    if (constrCall.isSuperCall()) {
        out.print("super(");
    } else {
        out.print("this(");
    }
    // Else try to render some arguments
    if (arguments instanceof ArgumentListExpression) {
        ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments;
        List<Expression> args = argumentListExpression.getExpressions();
        for (Expression arg : args) {
            if (arg instanceof ConstantExpression) {
                ConstantExpression expression = (ConstantExpression) arg;
                Object o = expression.getValue();
                if (o instanceof String) {
                    out.print("(String)null");
                } else {
                    out.print(expression.getText());
                }
            } else {
                ClassNode type = getConstructorArgumentType(arg, node);
                printDefaultValue(out, type);
            }
            if (arg != args.get(args.size() - 1)) {
                out.print(", ");
            }
        }
    }
    out.println(");");
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 65 with ConstructorCallExpression

use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy by apache.

the class JavaStubGenerator method getConstructorCallExpression.

private static ConstructorCallExpression getConstructorCallExpression(ConstructorNode constructorNode) {
    Statement code = constructorNode.getCode();
    if (!(code instanceof BlockStatement))
        return null;
    BlockStatement block = (BlockStatement) code;
    List stats = block.getStatements();
    if (stats == null || stats.isEmpty())
        return null;
    Statement stat = (Statement) stats.get(0);
    if (!(stat instanceof ExpressionStatement))
        return null;
    Expression expr = ((ExpressionStatement) stat).getExpression();
    if (!(expr instanceof ConstructorCallExpression))
        return null;
    return (ConstructorCallExpression) expr;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)68 Expression (org.codehaus.groovy.ast.expr.Expression)45 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)44 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)37 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)32 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)30 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)28 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)25 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)25 ClassNode (org.codehaus.groovy.ast.ClassNode)24 Statement (org.codehaus.groovy.ast.stmt.Statement)24 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)21 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)18 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)16 ArrayList (java.util.ArrayList)15 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)15 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)15 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)13 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)10