Search in sources :

Example 21 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class IfElseTest method testLoop.

public void testLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
    Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
    Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
    IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
    classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    assertSetProperty(bean, "bar", "abc");
    System.out.println("################ Now about to invoke method");
    Object[] array = {};
    InvokerHelper.invokeMethod(bean, "ifDemo", array);
    System.out.println("################ Done");
    assertGetProperty(bean, "result", "worked");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 22 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class JavaAwareResolveVisitor method getConstructorCall.

private static Expression getConstructorCall(Statement code) {
    if (code == null)
        return null;
    if (code instanceof BlockStatement) {
        BlockStatement bs = (BlockStatement) code;
        if (bs.isEmpty())
            return null;
        return getConstructorCall(bs.getStatements().get(0));
    }
    if (!(code instanceof ExpressionStatement))
        return null;
    ExpressionStatement es = (ExpressionStatement) code;
    Expression exp = es.getExpression();
    if (!(exp instanceof ConstructorCallExpression))
        return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) exp;
    if (!cce.isSpecialCall())
        return null;
    return cce;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 23 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class AntlrParserPlugin method variableDef.

protected Statement variableDef(AST variableDef) {
    ExpressionStatement expressionStatement = new ExpressionStatement(declarationExpression(variableDef));
    configureAST(expressionStatement, variableDef);
    return expressionStatement;
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 24 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class AntlrParserPlugin method declarationExpression.

protected Expression declarationExpression(AST variableDef) {
    AST node = variableDef.getFirstChild();
    ClassNode type = null;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        // force check of modifier conflicts
        modifiers = modifiers(node, annotations, 0);
        node = node.getNextSibling();
    }
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    Expression leftExpression;
    Expression rightExpression = EmptyExpression.INSTANCE;
    AST right;
    if (isType(ASSIGN, node)) {
        node = node.getFirstChild();
        AST left = node.getFirstChild();
        ArgumentListExpression alist = new ArgumentListExpression();
        for (AST varDef = left; varDef != null; varDef = varDef.getNextSibling()) {
            assertNodeType(VARIABLE_DEF, varDef);
            DeclarationExpression de = (DeclarationExpression) declarationExpression(varDef);
            alist.addExpression(de.getVariableExpression());
        }
        leftExpression = alist;
        right = node.getNextSibling();
        if (right != null)
            rightExpression = expression(right);
    } else {
        String name = identifier(node);
        VariableExpression ve = new VariableExpression(name, type);
        ve.setModifiers(modifiers);
        leftExpression = ve;
        right = node.getNextSibling();
        if (right != null) {
            assertNodeType(ASSIGN, right);
            rightExpression = expression(right.getFirstChild());
        }
    }
    configureAST(leftExpression, node);
    Token token = makeToken(Types.ASSIGN, variableDef);
    DeclarationExpression expression = new DeclarationExpression(leftExpression, token, rightExpression);
    expression.addAnnotations(annotations);
    configureAST(expression, variableDef);
    ExpressionStatement expressionStatement = new ExpressionStatement(expression);
    configureAST(expressionStatement, variableDef);
    return expression;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) ArrayList(java.util.ArrayList) Token(org.codehaus.groovy.syntax.Token) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 25 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class TraitComposer method createDelegatingForwarder.

private static Statement createDelegatingForwarder(final MethodNode forwarderMethod, final ClassNode next) {
    // generates --> next$Trait$Helper.method(this, arg1, arg2)
    TraitHelpersTuple helpers = Traits.findHelpers(next);
    ArgumentListExpression args = new ArgumentListExpression();
    args.addExpression(new VariableExpression("this"));
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    StaticMethodCallExpression delegateCall = new StaticMethodCallExpression(helpers.getHelper(), forwarderMethod.getName(), args);
    Statement result;
    if (ClassHelper.VOID_TYPE.equals(forwarderMethod.getReturnType())) {
        BlockStatement stmt = new BlockStatement();
        stmt.addStatement(new ExpressionStatement(delegateCall));
        stmt.addStatement(new ReturnStatement(new ConstantExpression(null)));
        result = stmt;
    } else {
        result = new ReturnStatement(delegateCall);
    }
    return result;
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)123 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)79 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)59 Expression (org.codehaus.groovy.ast.expr.Expression)55 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)52 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)51 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)49 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)43 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)39 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)35 Parameter (org.codehaus.groovy.ast.Parameter)30 MethodNode (org.codehaus.groovy.ast.MethodNode)29 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)29 ClassNode (org.codehaus.groovy.ast.ClassNode)27 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArrayList (java.util.ArrayList)25 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)24