Java Code Examples for org.codehaus.groovy.ast.expr.MethodCallExpression#setSourcePosition()

The following examples show how to use org.codehaus.groovy.ast.expr.MethodCallExpression#setSourcePosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodCallExpressionTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static MethodCallExpression transformToMopSuperCall(final ClassNode superCallReceiver, final MethodCallExpression expr) {
    MethodNode mn = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    String mopName = MopWriter.getMopMethodName(mn, false);
    MethodNode direct = new MethodNode(
            mopName,
            ACC_PUBLIC | ACC_SYNTHETIC,
            mn.getReturnType(),
            mn.getParameters(),
            mn.getExceptions(),
            EmptyStatement.INSTANCE
    );
    direct.setDeclaringClass(superCallReceiver);
    MethodCallExpression result = new MethodCallExpression(
            new VariableExpression("this"),
            mopName,
            expr.getArguments()
    );
    result.setImplicitThis(true);
    result.setSpreadSafe(false);
    result.setSafe(false);
    result.setSourcePosition(expr);
    result.setMethodTarget(direct);
    return result;
}
 
Example 2
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 3
Source File: AutoNewLineTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Statement createNewLine(final ASTNode node) {
    MethodCallExpression mce = new MethodCallExpression(
            new VariableExpression("this"),
            "newLine",
            ArgumentListExpression.EMPTY_ARGUMENTS
    );
    mce.setImplicitThis(true);
    mce.setSourcePosition(node);
    ExpressionStatement stmt = new ExpressionStatement(mce);
    stmt.setSourcePosition(node);
    return stmt;
}
 
Example 4
Source File: BinaryExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) {
    MethodCallExpression call = callX(rightExpression, "isCase", leftExpression);
    call.setMethodTarget(bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
    call.setSourcePosition(bin);
    call.copyNodeMetaData(bin);
    Expression tExp = ternaryX(
            boolX(binX(rightExpression, Token.newSymbol("==", -1, -1), nullX())),
            binX(leftExpression, Token.newSymbol("==", -1, -1), nullX()),
            call
    );
    return tExp;
}
 
Example 5
Source File: StaticMethodCallExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
Expression transformStaticMethodCallExpression(final StaticMethodCallExpression orig) {
    MethodNode target = (MethodNode) orig.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (target != null) {
        MethodCallExpression call = new MethodCallExpression(
                new ClassExpression(orig.getOwnerType()),
                orig.getMethod(),
                orig.getArguments()
        );
        call.setMethodTarget(target);
        call.setSourcePosition(orig);
        call.copyNodeMetaData(orig);
        return transformer.transform(call);
    }
    return transformer.superTransform(orig);
}
 
Example 6
Source File: SuperCallTraitTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallExpression(final MethodCallExpression exp) {
    if (isTraitSuperPropertyExpression(exp.getObjectExpression())) {
        Expression objectExpression = exp.getObjectExpression();
        ClassNode traitReceiver = ((PropertyExpression) objectExpression).getObjectExpression().getType();

        if (traitReceiver != null) {
            // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
            ClassExpression receiver = new ClassExpression(
                    getHelper(traitReceiver)
            );
            ArgumentListExpression newArgs = new ArgumentListExpression();
            Expression arguments = exp.getArguments();
            newArgs.addExpression(new VariableExpression("this"));
            if (arguments instanceof TupleExpression) {
                List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
                for (Expression expression : expressions) {
                    newArgs.addExpression(transform(expression));
                }
            } else {
                newArgs.addExpression(transform(arguments));
            }
            MethodCallExpression result = new MethodCallExpression(
                    receiver,
                    transform(exp.getMethod()),
                    newArgs
            );
            result.setImplicitThis(false);
            result.setSpreadSafe(exp.isSpreadSafe());
            result.setSafe(exp.isSafe());
            result.setSourcePosition(exp);
            return result;
        }
    }
    return super.transform(exp);
}
 
Example 7
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression createFieldHelperCall(Expression exp, ClassNode weavedType, String propName) {
    String method = Traits.helperGetterName(new FieldNode(propName, 0, ClassHelper.OBJECT_TYPE, weavedType, null));
    MethodCallExpression mce = new MethodCallExpression(
            createFieldHelperReceiver(),
            method,
            ArgumentListExpression.EMPTY_ARGUMENTS
    );
    mce.setSourcePosition(exp instanceof PropertyExpression ? ((PropertyExpression) exp).getProperty() : exp);
    mce.setImplicitThis(false);
    return mce;
}
 
Example 8
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformFieldExpression(final FieldExpression exp) {
    FieldNode field = exp.getField();
    MethodCallExpression mce = new MethodCallExpression(
            createFieldHelperReceiver(),
            Traits.helperGetterName(field),
            ArgumentListExpression.EMPTY_ARGUMENTS
    );
    mce.setSourcePosition(exp);
    mce.setImplicitThis(false);
    markDynamicCall(mce, field, field.isStatic());
    return mce;
}
 
Example 9
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformSuperMethodCall(final MethodCallExpression call) {
    String method = call.getMethodAsString();
    if (method == null) {
        throwSuperError(call);
    }

    Expression arguments = transform(call.getArguments());
    ArgumentListExpression superCallArgs = new ArgumentListExpression();
    if (arguments instanceof ArgumentListExpression) {
        ArgumentListExpression list = (ArgumentListExpression) arguments;
        for (Expression expression : list) {
            superCallArgs.addExpression(expression);
        }
    } else {
        superCallArgs.addExpression(arguments);
    }
    MethodCallExpression transformed = new MethodCallExpression(
            weaved,
            Traits.getSuperTraitMethodName(traitClass, method),
            superCallArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(false);
    return transformed;
}
 
Example 10
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallOnThisFallBack(final MethodCallExpression call,
                                                     final Expression method, final Expression arguments) {
    MethodCallExpression transformed = new MethodCallExpression(
            weaved,
            method,
            transform(arguments)
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(false);
    return transformed;
}
 
Example 11
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallOnThisInClosure(final MethodCallExpression call) {
    MethodCallExpression transformed = new MethodCallExpression(
            (Expression) call.getReceiver(),
            call.getMethod(),
            transform(call.getArguments())
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(call.isImplicitThis());
    return transformed;
}
 
Example 12
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformPrivateMethodCallOnThis(final MethodCallExpression call,
                                                    final Expression arguments, final String methodName) {
    ArgumentListExpression newArgs = createArgumentList(arguments);
    MethodCallExpression transformed = new MethodCallExpression(
            new VariableExpression("this"),
            methodName,
            newArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(true);
    return transformed;
}
 
Example 13
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformPrivateMethodCallOnThisInClosure(final MethodCallExpression call,
                                                             final Expression arguments, final String methodName) {
    ArgumentListExpression newArgs = createArgumentList(arguments);
    MethodCallExpression transformed = new MethodCallExpression(
            new ClassExpression(traitHelperClass),
            methodName,
            newArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(true);
    return transformed;
}
 
Example 14
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected void assignToArray(final Expression parent, final Expression receiver, final Expression index, final Expression rhsValueLoader, final boolean safe) {
    ClassNode arrayType = controller.getTypeChooser().resolveType(receiver, controller.getClassNode());
    int operationType = getOperandType(arrayType.getComponentType());
    BinaryExpressionWriter bew = binExpWriter[operationType];

    if (bew.arraySet(true) && arrayType.isArray() && !safe) {
        super.assignToArray(parent, receiver, index, rhsValueLoader, safe);
    } else {
        /*
         * This code path is needed because ACG creates array access expressions
         */
        StaticTypeCheckingVisitor visitor = new StaticCompilationVisitor(controller.getSourceUnit(), controller.getClassNode());
        // GROOVY-6061
        if (rhsValueLoader instanceof VariableSlotLoader && parent instanceof BinaryExpression) {
            rhsValueLoader.putNodeMetaData(INFERRED_TYPE, controller.getTypeChooser().resolveType(parent, controller.getClassNode()));
        }
        // let's replace this assignment to a subscript operator with a method call
        // e.g. x[5] = 10
        // -> (x, [], 5), =, 10
        // -> methodCall(x, "putAt", [5, 10])
        MethodCallExpression call = callX(receiver, "putAt", args(index, rhsValueLoader));
        call.setSafe(safe);
        call.setSourcePosition(parent);
        visitor.visitMethodCallExpression(call);

        OperandStack operandStack = controller.getOperandStack();
        int height = operandStack.getStackLength();
        call.visit(controller.getAcg());
        operandStack.pop();
        operandStack.remove(operandStack.getStackLength() - height);

        // return value of assignment
        rhsValueLoader.visit(controller.getAcg());
    }
}
 
Example 15
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp instanceof MethodCallExpression) {
        return transformMethodCall((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        ClosureExpression cl = (ClosureExpression) exp;
        cl.getCode().visit(this);
        return cl;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) exp;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            MethodCallExpression callGetModel = new MethodCallExpression(
                    new VariableExpression("this"),
                    "getModel",
                    ArgumentListExpression.EMPTY_ARGUMENTS
            );
            callGetModel.setImplicitThis(true);
            callGetModel.setSourcePosition(exp);
            String varName = var.getName();
            if ("model".equals(varName) || "unescaped".equals(varName)) {
                return callGetModel;
            }
            MethodCallExpression mce = new MethodCallExpression(
                    callGetModel,
                    "get",
                    new ArgumentListExpression(new ConstantExpression(varName))
            );
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            MethodCallExpression yield = new MethodCallExpression(
                    new VariableExpression("this"),
                    "tryEscape",
                    new ArgumentListExpression(mce)
            );
            yield.setImplicitThis(true);
            yield.setSourcePosition(exp);
            yield.putNodeMetaData(TARGET_VARIABLE, varName);
            return autoEscape?yield:mce;
        }
    }
    return super.transform(exp);
}
 
Example 16
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformMethodCall(final MethodCallExpression exp) {
    String name = exp.getMethodAsString();
    if (exp.isImplicitThis() && "include".equals(name)) {
        return tryTransformInclude(exp);
    } else if (exp.isImplicitThis() && name.startsWith(":")) {
        List<Expression> args;
        if (exp.getArguments() instanceof ArgumentListExpression) {
            args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
        } else {
            args = Collections.singletonList(exp.getArguments());
        }
        Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
        MethodCallExpression call = new MethodCallExpression(
                new VariableExpression("this"),
                "methodMissing",
                newArguments
        );
        call.setImplicitThis(true);
        call.setSafe(exp.isSafe());
        call.setSpreadSafe(exp.isSpreadSafe());
        call.setSourcePosition(exp);
        return call;
    } else if (name!=null && name.startsWith("$")) {
        MethodCallExpression reformatted = new MethodCallExpression(
                exp.getObjectExpression(),
                name.substring(1),
                exp.getArguments()
        );
        reformatted.setImplicitThis(exp.isImplicitThis());
        reformatted.setSafe(exp.isSafe());
        reformatted.setSpreadSafe(exp.isSpreadSafe());
        reformatted.setSourcePosition(exp);
        // wrap in a stringOf { ... } closure call
        ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
        clos.setVariableScope(new VariableScope());
        MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"),
                "stringOf",
                clos);
        stringOf.setImplicitThis(true);
        stringOf.setSourcePosition(reformatted);
        return stringOf;
    }
    return super.transform(exp);
}