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;
}
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);
}
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);
}
Aggregations