Search in sources :

Example 51 with ConstructorCallExpression

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

the class NotYetImplementedASTTransformation method throwAssertionFailedError.

private Statement throwAssertionFailedError(AnnotationNode annotationNode) {
    ThrowStatement throwStatement = new ThrowStatement(new ConstructorCallExpression(ASSERTION_FAILED_ERROR_TYPE, new ArgumentListExpression(new ConstantExpression("Method is marked with @NotYetImplemented but passes unexpectedly"))));
    throwStatement.setSourcePosition(annotationNode);
    return throwStatement;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 52 with ConstructorCallExpression

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

the class NewifyASTTransformation method transform.

public Expression transform(Expression expr) {
    if (expr == null)
        return null;
    if (expr instanceof MethodCallExpression && candidate == null) {
        MethodCallExpression mce = (MethodCallExpression) expr;
        Expression args = transform(mce.getArguments());
        if (isNewifyCandidate(mce)) {
            Expression transformed = transformMethodCall(mce, args);
            transformed.setSourcePosition(mce);
            return transformed;
        }
        Expression method = transform(mce.getMethod());
        Expression object = transform(mce.getObjectExpression());
        MethodCallExpression transformed = callX(object, method, args);
        transformed.setSourcePosition(mce);
        return transformed;
    } else if (expr instanceof ClosureExpression) {
        ClosureExpression ce = (ClosureExpression) expr;
        ce.getCode().visit(this);
    } else if (expr instanceof ConstructorCallExpression) {
        ConstructorCallExpression cce = (ConstructorCallExpression) expr;
        if (cce.isUsingAnonymousInnerClass()) {
            cce.getType().visitContents(this);
        }
    } else if (expr instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) expr;
        if (de == candidate || auto) {
            candidate = null;
            Expression left = de.getLeftExpression();
            Expression right = transform(de.getRightExpression());
            DeclarationExpression newDecl = new DeclarationExpression(left, de.getOperation(), right);
            newDecl.addAnnotations(de.getAnnotations());
            return newDecl;
        }
        return de;
    }
    return expr.transformExpression(this);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 53 with ConstructorCallExpression

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

the class NewifyASTTransformation method transformMethodCall.

private Expression transformMethodCall(MethodCallExpression mce, Expression args) {
    ClassNode classType;
    if (isNewMethodStyle(mce)) {
        classType = mce.getObjectExpression().getType();
    } else {
        classType = findMatchingCandidateClass(mce);
    }
    if (classType != null) {
        return new ConstructorCallExpression(classType, args);
    }
    // set the args as they might have gotten Newify transformed GROOVY-3491
    mce.setArguments(args);
    return mce;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression)

Example 54 with ConstructorCallExpression

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

the class DelegateAsyncTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    if (parent instanceof ClassNode) {
        Expression value = annotationNode.getMember("value");
        if (value instanceof ClassExpression) {
            ClassNode targetApi = value.getType().getPlainNodeReference();
            ClassNode classNode = (ClassNode) parent;
            final String fieldName = '$' + Introspector.decapitalize(targetApi.getNameWithoutPackage());
            FieldNode fieldNode = classNode.getField(fieldName);
            if (fieldNode == null) {
                fieldNode = new FieldNode(fieldName, Modifier.PRIVATE, targetApi, classNode, new ConstructorCallExpression(targetApi, NO_ARGS));
                classNode.addField(fieldNode);
            }
            applyDelegateAsyncTransform(classNode, targetApi, fieldName);
        }
    } else if (parent instanceof FieldNode) {
        FieldNode fieldNode = (FieldNode) parent;
        ClassNode targetApi = fieldNode.getType().getPlainNodeReference();
        ClassNode classNode = fieldNode.getOwner();
        applyDelegateAsyncTransform(classNode, targetApi, fieldNode.getName());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 55 with ConstructorCallExpression

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

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)

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