Search in sources :

Example 1 with MacroContext

use of org.codehaus.groovy.macro.runtime.MacroContext in project groovy by apache.

the class MacroCallTransformingVisitor method visitMethodCallExpression.

@Override
public void visitMethodCallExpression(MethodCallExpression call) {
    super.visitMethodCallExpression(call);
    final List<Expression> callArguments;
    if (call.getArguments() instanceof TupleExpression) {
        callArguments = ((TupleExpression) call.getArguments()).getExpressions();
    } else {
        callArguments = Collections.singletonList(call.getArguments());
    }
    List<MethodNode> macroMethods = findMacroMethods(call.getMethodAsString(), callArguments);
    if (macroMethods.isEmpty()) {
        // Early return to avoid macro context and arguments creation
        return;
    }
    MacroContext macroContext = new MacroContext(unit, sourceUnit, call);
    Object[] macroArguments = new Object[callArguments.size() + 1];
    macroArguments[0] = macroContext;
    System.arraycopy(callArguments.toArray(), 0, macroArguments, 1, callArguments.size());
    for (MethodNode macroMethodNode : macroMethods) {
        if (!(macroMethodNode instanceof ExtensionMethodNode)) {
            throw new IllegalStateException(macroMethodNode + " is not an instance of ExtensionMethodNode");
        }
        if (tryMacroMethod(call, (ExtensionMethodNode) macroMethodNode, macroArguments)) {
            break;
        }
    }
}
Also used : ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MacroContext(org.codehaus.groovy.macro.runtime.MacroContext) ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode)

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)1 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)1 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)1 Expression (org.codehaus.groovy.ast.expr.Expression)1 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)1 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)1 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)1 MacroContext (org.codehaus.groovy.macro.runtime.MacroContext)1 ExtensionMethodNode (org.codehaus.groovy.transform.stc.ExtensionMethodNode)1