Search in sources :

Example 6 with SynchronizedStatement

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

the class AntlrParserPlugin method statement.

// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
    Statement statement = null;
    int type = node.getType();
    switch(type) {
        case SLIST:
        case LITERAL_finally:
            statement = statementList(node);
            break;
        case METHOD_CALL:
            statement = methodCall(node);
            break;
        case VARIABLE_DEF:
            statement = variableDef(node);
            break;
        case LABELED_STAT:
            return labelledStatement(node);
        case LITERAL_assert:
            statement = assertStatement(node);
            break;
        case LITERAL_break:
            statement = breakStatement(node);
            break;
        case LITERAL_continue:
            statement = continueStatement(node);
            break;
        case LITERAL_if:
            statement = ifStatement(node);
            break;
        case LITERAL_for:
            statement = forStatement(node);
            break;
        case LITERAL_return:
            statement = returnStatement(node);
            break;
        case LITERAL_synchronized:
            statement = synchronizedStatement(node);
            break;
        case LITERAL_switch:
            statement = switchStatement(node);
            break;
        case LITERAL_try:
            statement = tryStatement(node);
            break;
        case LITERAL_throw:
            statement = throwStatement(node);
            break;
        case LITERAL_while:
            statement = whileStatement(node);
            break;
        default:
            statement = new ExpressionStatement(expression(node));
    }
    if (statement != null) {
        configureAST(statement, node);
    }
    return statement;
}
Also used : 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) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 7 with SynchronizedStatement

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

the class LazyASTTransformation method addDoubleCheckedLockingBody.

private static void addDoubleCheckedLockingBody(BlockStatement body, FieldNode fieldNode, Expression initExpr) {
    final Expression fieldExpr = varX(fieldNode);
    final VariableExpression localVar = varX(fieldNode.getName() + "_local");
    body.addStatement(declS(localVar, fieldExpr));
    body.addStatement(ifElseS(notNullX(localVar), returnS(localVar), new SynchronizedStatement(syncTarget(fieldNode), ifElseS(notNullX(fieldExpr), returnS(fieldExpr), returnS(assignX(fieldExpr, initExpr))))));
}
Also used : 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) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 8 with SynchronizedStatement

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

the class LazyASTTransformation method createSoftGetter.

private static void createSoftGetter(FieldNode fieldNode, Expression initExpr, ClassNode type) {
    final BlockStatement body = new BlockStatement();
    final Expression fieldExpr = varX(fieldNode);
    final Expression resExpr = varX("res", type);
    final MethodCallExpression callExpression = callX(fieldExpr, "get");
    callExpression.setSafe(true);
    body.addStatement(declS(resExpr, callExpression));
    final Statement mainIf = ifElseS(notNullX(resExpr), stmt(resExpr), block(assignS(resExpr, initExpr), assignS(fieldExpr, ctorX(SOFT_REF, resExpr)), stmt(resExpr)));
    if (fieldNode.isVolatile()) {
        body.addStatement(ifElseS(notNullX(resExpr), stmt(resExpr), new SynchronizedStatement(syncTarget(fieldNode), block(assignS(resExpr, callExpression), mainIf))));
    } else {
        body.addStatement(mainIf);
    }
    addMethod(fieldNode, body, type);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Aggregations

SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)8 Statement (org.codehaus.groovy.ast.stmt.Statement)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)4 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)4 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)4 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)4 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 MethodNode (org.codehaus.groovy.ast.MethodNode)2 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)2 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)2 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)2 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)2 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)2 ContinueStatement (org.codehaus.groovy.ast.stmt.ContinueStatement)2 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)2 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)2 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)2