Search in sources :

Example 36 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.

the class NewifyASTTransformation method isNewMethodStyle.

private boolean isNewMethodStyle(MethodCallExpression mce) {
    final Expression obj = mce.getObjectExpression();
    final Expression meth = mce.getMethod();
    return (obj instanceof ClassExpression && meth instanceof ConstantExpression && ((ConstantExpression) meth).getValue().equals("new"));
}
Also used : 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 37 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorStatementArrayOrCloneable.

private Statement createConstructorStatementArrayOrCloneable(FieldNode fNode) {
    final Expression fieldExpr = varX(fNode);
    Expression initExpr = fNode.getInitialValueExpression();
    ClassNode fieldType = fNode.getType();
    final Expression array = findArg(fNode.getName());
    final Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
    } else {
        assignInit = assignS(fieldExpr, cloneArrayOrCloneableExpr(initExpr, fieldType));
    }
    return ifElseS(equalsNullX(array), assignInit, assignS(fieldExpr, cloneArrayOrCloneableExpr(array, fieldType)));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 38 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorStatementCollection.

private Statement createConstructorStatementCollection(FieldNode fNode) {
    final Expression fieldExpr = varX(fNode);
    ClassNode fieldType = fieldExpr.getType();
    Expression initExpr = fNode.getInitialValueExpression();
    final Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
    } else {
        assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
    }
    Expression collection = findArg(fNode.getName());
    return ifElseS(equalsNullX(collection), assignInit, ifElseS(isInstanceOfX(collection, CLONEABLE_TYPE), assignS(fieldExpr, cloneCollectionExpr(cloneArrayOrCloneableExpr(collection, fieldType), fieldType)), assignS(fieldExpr, cloneCollectionExpr(collection, fieldType))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 39 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.

the class ImmutableASTTransformation method createConstructorStatementMapSpecial.

private Statement createConstructorStatementMapSpecial(FieldNode fNode) {
    final Expression fieldExpr = varX(fNode);
    final ClassNode fieldType = fieldExpr.getType();
    final Expression initExpr = fNode.getInitialValueExpression();
    final Statement assignInit;
    if (initExpr == null || (initExpr instanceof ConstantExpression && ((ConstantExpression) initExpr).isNullExpression())) {
        assignInit = assignS(fieldExpr, ConstantExpression.EMPTY_EXPRESSION);
    } else {
        assignInit = assignS(fieldExpr, cloneCollectionExpr(initExpr, fieldType));
    }
    Expression namedArgs = findArg(fNode.getName());
    Expression baseArgs = varX("args");
    return ifElseS(equalsNullX(baseArgs), assignInit, ifElseS(equalsNullX(namedArgs), ifElseS(isTrueX(callX(baseArgs, "containsKey", constX(fNode.getName()))), assignS(fieldExpr, namedArgs), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType))), ifElseS(isOneX(callX(baseArgs, "size")), assignS(fieldExpr, cloneCollectionExpr(namedArgs, fieldType)), assignS(fieldExpr, cloneCollectionExpr(baseArgs, fieldType)))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 40 with ConstantExpression

use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.

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)

Aggregations

ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)157 Expression (org.codehaus.groovy.ast.expr.Expression)92 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)88 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)72 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)67 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)55 ClassNode (org.codehaus.groovy.ast.ClassNode)51 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)43 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)42 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)41 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)39 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)37 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)36 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)35 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)33 Statement (org.codehaus.groovy.ast.stmt.Statement)33 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)31 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)29