Search in sources :

Example 11 with VariableExpression

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

the class ImmutableASTTransformation method doAddConstructor.

private static void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
        if ("args".equals(p.getName())) {
            argsParam = p;
            break;
        }
    }
    if (argsParam != null) {
        final Parameter arg = argsParam;
        ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {

            @Override
            protected SourceUnit getSourceUnit() {
                return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
                super.visitVariableExpression(expression);
                if ("args".equals(expression.getName())) {
                    expression.setAccessedVariable(arg);
                }
            }
        };
        variableExpressionFix.visitConstructor(constructorNode);
    }
}
Also used : ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 12 with VariableExpression

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

the class NewifyASTTransformation method determineClasses.

/** allow non-strict mode in scripts because parsing not complete at that point */
private ListExpression determineClasses(Expression expr, boolean searchSourceUnit) {
    ListExpression list = new ListExpression();
    if (expr instanceof ClassExpression) {
        list.addExpression(expr);
    } else if (expr instanceof VariableExpression && searchSourceUnit) {
        VariableExpression ve = (VariableExpression) expr;
        ClassNode fromSourceUnit = getSourceUnitClass(ve);
        if (fromSourceUnit != null) {
            ClassExpression found = classX(fromSourceUnit);
            found.setSourcePosition(ve);
            list.addExpression(found);
        } else {
            addError(BASE_BAD_PARAM_ERROR + "an unresolvable reference to '" + ve.getName() + "'.", expr);
        }
    } else if (expr instanceof ListExpression) {
        list = (ListExpression) expr;
        final List<Expression> expressions = list.getExpressions();
        for (int i = 0; i < expressions.size(); i++) {
            Expression next = expressions.get(i);
            if (next instanceof VariableExpression && searchSourceUnit) {
                VariableExpression ve = (VariableExpression) next;
                ClassNode fromSourceUnit = getSourceUnitClass(ve);
                if (fromSourceUnit != null) {
                    ClassExpression found = classX(fromSourceUnit);
                    found.setSourcePosition(ve);
                    expressions.set(i, found);
                } else {
                    addError(BASE_BAD_PARAM_ERROR + "a list containing an unresolvable reference to '" + ve.getName() + "'.", next);
                }
            } else if (!(next instanceof ClassExpression)) {
                addError(BASE_BAD_PARAM_ERROR + "a list containing type: " + next.getType().getName() + ".", next);
            }
        }
        checkDuplicateNameClashes(list);
    } else if (expr != null) {
        addError(BASE_BAD_PARAM_ERROR + "a type: " + expr.getType().getName() + ".", expr);
    }
    return list;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 13 with VariableExpression

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

the class MarkupBuilderCodeTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression bin) {
    Expression left = bin.getLeftExpression();
    Expression right = bin.getRightExpression();
    boolean assignment = bin.getOperation().getType() == Types.ASSIGN;
    if (assignment && left instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) left;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            String varName = var.getName();
            if (!"modelTypes".equals(varName)) {
                MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
                callGetModel.setImplicitThis(true);
                callGetModel.setSourcePosition(left);
                MethodCallExpression mce = new MethodCallExpression(callGetModel, "put", new ArgumentListExpression(new ConstantExpression(varName), right));
                mce.setSourcePosition(left);
                mce.setImplicitThis(false);
                return transform(mce);
            }
        }
    }
    if (assignment && left instanceof VariableExpression && right instanceof ClosureExpression) {
        VariableExpression var = (VariableExpression) left;
        if ("modelTypes".equals(var.getName())) {
            // template declaring its expected types from model directly
            // modelTypes = {
            //  List<String> items
            //  ...
            // }
            Map<String, ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression) right);
            Expression result = new EmptyExpression();
            result.setSourcePosition(bin);
            classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
            return result;
        }
    }
    return super.transform(bin);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) 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) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression)

Example 14 with VariableExpression

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

the class MarkupBuilderCodeTransformer method transformMethodCall.

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);
}
Also used : 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) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArrayList(java.util.ArrayList) List(java.util.List) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 15 with VariableExpression

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

the class AutoNewLineTransformer method createNewLine.

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;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)174 Expression (org.codehaus.groovy.ast.expr.Expression)84 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)84 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)76 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)70 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)66 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)59 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)58 ClassNode (org.codehaus.groovy.ast.ClassNode)52 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)47 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)47 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)46 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)43 Parameter (org.codehaus.groovy.ast.Parameter)40 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)40 MethodNode (org.codehaus.groovy.ast.MethodNode)37 FieldNode (org.codehaus.groovy.ast.FieldNode)36 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)35 Statement (org.codehaus.groovy.ast.stmt.Statement)33