Search in sources :

Example 31 with TupleExpression

use of org.codehaus.groovy.ast.expr.TupleExpression 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)

Example 32 with TupleExpression

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

the class MacroGroovyMethods method getMacroArguments.

protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) {
    Expression macroCallArguments = call.getArguments();
    if (macroCallArguments == null) {
        source.addError(new SyntaxException("Call should have arguments" + '\n', call));
        return null;
    }
    if (!(macroCallArguments instanceof TupleExpression)) {
        source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments));
        return null;
    }
    TupleExpression tupleArguments = (TupleExpression) macroCallArguments;
    if (tupleArguments.getExpressions() == null) {
        source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments));
        return null;
    }
    return tupleArguments;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 33 with TupleExpression

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

the class MarkupBuilderCodeTransformer method tryTransformInclude.

private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(exp.getObjectExpression(), includeType.getMethodName(), new ArgumentListExpression(mapEntry.getValueExpression()));
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                // not a valid import type, do not modify the code
                }
            }
        }
    }
    return super.transform(exp);
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) 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) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Example 34 with TupleExpression

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

the class ContractClosureWriter method createClosureClass.

public ClassNode createClosureClass(ClassNode classNode, MethodNode methodNode, ClosureExpression expression, boolean addOldVariable, boolean addResultVariable, int mods) {
    ClassNode outerClass = getOutermostClass(classNode);
    String name = outerClass.getName() + "$" + getClosureInnerName(outerClass, classNode);
    // fetch all method parameters, and possibly add 'old' and 'result'
    ArrayList<Parameter> parametersTemp = new ArrayList<Parameter>(Arrays.asList(expression.getParameters()));
    removeParameter("old", parametersTemp);
    removeParameter("result", parametersTemp);
    if (methodNode != null && addResultVariable && !isPrimitiveVoid(methodNode.getReturnType())) {
        parametersTemp.add(new Parameter(methodNode.getReturnType(), "result"));
    }
    if (addOldVariable) {
        parametersTemp.add(new Parameter(new ClassNode(Map.class), "old"));
    }
    // contains all params of the original method
    ArrayList<Parameter> closureParameters = new ArrayList<Parameter>();
    for (Parameter param : parametersTemp) {
        Parameter closureParameter = new Parameter(param.getType().getPlainNodeReference(), param.getName());
        closureParameters.add(closureParameter);
    }
    ClassNode answer = new ClassNode(name, mods | ACC_FINAL, ClassHelper.CLOSURE_TYPE.getPlainNodeReference());
    answer.setSynthetic(true);
    answer.setSourcePosition(expression);
    MethodNode method = answer.addMethod("doCall", ACC_PUBLIC, ClassHelper.Boolean_TYPE, closureParameters.toArray(new Parameter[closureParameters.size()]), ClassNode.EMPTY_ARRAY, expression.getCode());
    method.setSourcePosition(expression);
    VariableScope varScope = expression.getVariableScope();
    if (varScope == null) {
        throw new RuntimeException("Must have a VariableScope by now! for expression: " + expression + " class: " + name);
    } else {
        method.setVariableScope(varScope.copy());
    }
    // let's add a typesafe call method
    ArgumentListExpression arguments = new ArgumentListExpression();
    for (Parameter parameter : closureParameters) {
        arguments.addExpression(varX(parameter));
    }
    MethodNode call = answer.addMethod("call", ACC_PUBLIC, ClassHelper.Boolean_TYPE, closureParameters.toArray(new Parameter[closureParameters.size()]), ClassNode.EMPTY_ARRAY, returnS(callThisX("doCall", arguments)));
    call.setSourcePosition(expression);
    call.setSynthetic(true);
    // let's make the constructor
    BlockStatement block = new BlockStatement();
    // this block does not get a source position, because we don't
    // want this synthetic constructor to show up in corbertura reports
    VariableExpression outer = varX("_outerInstance");
    outer.setSourcePosition(expression);
    block.getVariableScope().putReferencedLocalVariable(outer);
    VariableExpression thisObject = varX("_thisObject");
    thisObject.setSourcePosition(expression);
    block.getVariableScope().putReferencedLocalVariable(thisObject);
    TupleExpression conArgs = new TupleExpression(outer, thisObject);
    block.addStatement(stmt(ctorSuperX(conArgs)));
    Parameter[] consParams = params(param(ClassHelper.OBJECT_TYPE, "_outerInstance"), param(ClassHelper.OBJECT_TYPE, "_thisObject"));
    ASTNode sn = answer.addConstructor(ACC_PUBLIC, consParams, ClassNode.EMPTY_ARRAY, block);
    sn.setSourcePosition(expression);
    correctAccessedVariable(method, expression);
    return answer;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ArrayList(java.util.ArrayList) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ASTNode(org.codehaus.groovy.ast.ASTNode) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 35 with TupleExpression

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

the class TraitReceiverTransformer method createArgumentList.

private ArgumentListExpression createArgumentList(final Expression self, final Expression arguments) {
    ArgumentListExpression newArgs = new ArgumentListExpression();
    newArgs.addExpression(self);
    if (arguments instanceof TupleExpression) {
        for (Expression argument : (TupleExpression) arguments) {
            newArgs.addExpression(transform(argument));
        }
    } else {
        newArgs.addExpression(transform(arguments));
    }
    return newArgs;
}
Also used : PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

Aggregations

TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)78 Expression (org.codehaus.groovy.ast.expr.Expression)65 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)56 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)44 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)42 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)41 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)40 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)35 ClassNode (org.codehaus.groovy.ast.ClassNode)34 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)34 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)31 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)23 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)23 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)22 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)20 Parameter (org.codehaus.groovy.ast.Parameter)20 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)18 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)18 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)17