Search in sources :

Example 51 with ClosureExpression

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

the class StaticTypeCheckingSupport method isWithCall.

/**
     * Called on method call checks in order to determine if a method call corresponds to the
     * idiomatic o.with { ... } structure
     * @param name name of the method called
     * @param callArguments arguments of the method
     * @return true if the name is "with" and arguments consist of a single closure
     */
public static boolean isWithCall(final String name, final Expression callArguments) {
    boolean isWithCall = "with".equals(name) && callArguments instanceof ArgumentListExpression;
    if (isWithCall) {
        ArgumentListExpression argList = (ArgumentListExpression) callArguments;
        List<Expression> expressions = argList.getExpressions();
        isWithCall = expressions.size() == 1 && expressions.get(0) instanceof ClosureExpression;
    }
    return isWithCall;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 52 with ClosureExpression

use of org.codehaus.groovy.ast.expr.ClosureExpression in project gradle by gradle.

the class AstUtils method detectScriptBlock.

@Nullable
public static ScriptBlock detectScriptBlock(Statement statement) {
    MethodCallExpression methodCall = extractBareMethodCall(statement);
    if (methodCall == null) {
        return null;
    }
    String methodName = extractConstantMethodName(methodCall);
    if (methodName == null) {
        return null;
    }
    ClosureExpression closureExpression = getSingleClosureArg(methodCall);
    return closureExpression == null ? null : new ScriptBlock(methodName, closureExpression);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) Nullable(javax.annotation.Nullable)

Example 53 with ClosureExpression

use of org.codehaus.groovy.ast.expr.ClosureExpression in project gradle by gradle.

the class RulesVisitor method visitMethodCallExpression.

@Override
public void visitMethodCallExpression(MethodCallExpression call) {
    ClosureExpression closureExpression = AstUtils.getSingleClosureArg(call);
    if (closureExpression != null) {
        // path { ... }
        rewriteAction(call, extractModelPathFromMethodTarget(call), closureExpression, RuleVisitor.displayName(call));
        return;
    }
    Pair<ClassExpression, ClosureExpression> args = AstUtils.getClassAndClosureArgs(call);
    if (args != null) {
        // path(Type) { ... }
        rewriteCreator(call, extractModelPathFromMethodTarget(call), args.getRight(), args.getLeft(), RuleVisitor.displayName(call));
        return;
    }
    ClassExpression classArg = AstUtils.getClassArg(call);
    if (classArg != null) {
        // path(Type)
        String displayName = RuleVisitor.displayName(call);
        List<Statement> statements = Lists.newLinkedList();
        statements.add(new EmptyStatement());
        BlockStatement block = new BlockStatement(statements, new VariableScope());
        closureExpression = new ClosureExpression(Parameter.EMPTY_ARRAY, block);
        closureExpression.setVariableScope(block.getVariableScope());
        String modelPath = extractModelPathFromMethodTarget(call);
        rewriteCreator(call, modelPath, closureExpression, classArg, displayName);
        return;
    }
    restrict(call, INVALID_RULE_SIGNATURE);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Aggregations

ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)53 Expression (org.codehaus.groovy.ast.expr.Expression)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)35 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)28 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)23 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)20 ClassNode (org.codehaus.groovy.ast.ClassNode)19 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)18 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)17 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)17 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 Parameter (org.codehaus.groovy.ast.Parameter)16 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)14 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)11 FieldNode (org.codehaus.groovy.ast.FieldNode)10 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)10 ArrayList (java.util.ArrayList)9