Search in sources :

Example 1 with IfStatement

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

the class FinalVariableAnalyzer method visitIfElse.

@Override
public void visitIfElse(final IfStatement ifElse) {
    visitStatement(ifElse);
    ifElse.getBooleanExpression().visit(this);
    Map<Variable, VariableState> ifState = pushState();
    ifElse.getIfBlock().visit(this);
    popState();
    Statement elseBlock = ifElse.getElseBlock();
    Map<Variable, VariableState> elseState = pushState();
    if (elseBlock instanceof EmptyStatement) {
        // dispatching to EmptyStatement will not call back visitor,
        // must call our visitEmptyStatement explicitly
        visitEmptyStatement((EmptyStatement) elseBlock);
    } else {
        elseBlock.visit(this);
    }
    popState();
    // merge if/else branches
    Map<Variable, VariableState> curState = getState();
    Set<Variable> allVars = new HashSet<Variable>();
    allVars.addAll(curState.keySet());
    allVars.addAll(ifState.keySet());
    allVars.addAll(elseState.keySet());
    for (Variable var : allVars) {
        VariableState beforeValue = curState.get(var);
        VariableState ifValue = ifState.get(var);
        VariableState elseValue = elseState.get(var);
        // merge if and else values
        VariableState mergedIfElse;
        mergedIfElse = ifValue != null && elseValue != null && ifValue.isFinal && elseValue.isFinal ? VariableState.is_final : VariableState.is_var;
        if (beforeValue == null) {
            curState.put(var, mergedIfElse);
        } else {
            if (beforeValue == VariableState.is_uninitialized) {
                curState.put(var, mergedIfElse);
            } else if (ifValue != null || elseValue != null) {
                curState.put(var, VariableState.is_var);
            }
        }
    }
}
Also used : Variable(org.codehaus.groovy.ast.Variable) Statement(org.codehaus.groovy.ast.stmt.Statement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) HashSet(java.util.HashSet)

Example 2 with IfStatement

use of org.codehaus.groovy.ast.stmt.IfStatement 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 3 with IfStatement

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

the class AntlrParserPlugin method ifStatement.

protected Statement ifStatement(AST ifNode) {
    AST node = ifNode.getFirstChild();
    assertNodeType(EXPR, node);
    BooleanExpression booleanExpression = booleanExpression(node);
    node = node.getNextSibling();
    Statement ifBlock = statement(node);
    Statement elseBlock = EmptyStatement.INSTANCE;
    node = node.getNextSibling();
    if (node != null) {
        elseBlock = statement(node);
    }
    IfStatement ifStatement = new IfStatement(booleanExpression, ifBlock, elseBlock);
    configureAST(ifStatement, ifNode);
    return ifStatement;
}
Also used : IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AST(antlr.collections.AST) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Example 4 with IfStatement

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

the class StaticTypeCheckingVisitor method visitIfElse.

@Override
public void visitIfElse(final IfStatement ifElse) {
    Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
    try {
        // create a new temporary element in the if-then-else type info
        typeCheckingContext.pushTemporaryTypeInfo();
        visitStatement(ifElse);
        ifElse.getBooleanExpression().visit(this);
        ifElse.getIfBlock().visit(this);
        // pop if-then-else temporary type info
        typeCheckingContext.popTemporaryTypeInfo();
        // GROOVY-6099: restore assignment info as before the if branch
        restoreTypeBeforeConditional();
        Statement elseBlock = ifElse.getElseBlock();
        if (elseBlock instanceof EmptyStatement) {
            // dispatching to EmptyStatement will not call back visitor,
            // must call our visitEmptyStatement explicitly
            visitEmptyStatement((EmptyStatement) elseBlock);
        } else {
            elseBlock.visit(this);
        }
    } finally {
        popAssignmentTracking(oldTracker);
    }
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 5 with IfStatement

use of org.codehaus.groovy.ast.stmt.IfStatement in project grails-core by grails.

the class AbstractGrailsArtefactTransformer method populateAutowiredApiLookupMethod.

protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiProperty, String methodName, BlockStatement methodBody) {
    addApiLookupFieldAndSetter(classNode, implementationNode, apiProperty, null);
    VariableExpression apiVar = new VariableExpression(apiProperty, implementationNode);
    BlockStatement ifBlock = new BlockStatement();
    ArgumentListExpression arguments = new ArgumentListExpression();
    arguments.addExpression(new ConstantExpression("Method on class [" + classNode + "] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly."));
    ifBlock.addStatement(new ThrowStatement(new ConstructorCallExpression(new ClassNode(IllegalStateException.class), arguments)));
    BlockStatement elseBlock = new BlockStatement();
    elseBlock.addStatement(new ReturnStatement(apiVar));
    methodBody.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(apiVar, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION)), ifBlock, elseBlock));
    MethodNode methodNode = new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode, ZERO_PARAMETERS, null, methodBody);
    return methodNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Aggregations

IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)19 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)14 Statement (org.codehaus.groovy.ast.stmt.Statement)14 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)13 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)13 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)11 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)11 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)11 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)10 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)10 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)10 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)9 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)9 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)8 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)7 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)7 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)7 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)6