Search in sources :

Example 1 with SynchronizedStatement

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

the class AntlrParserPlugin method synchronizedStatement.

protected Statement synchronizedStatement(AST syncNode) {
    AST node = syncNode.getFirstChild();
    Expression expression = expression(node);
    Statement code = statement(node.getNextSibling());
    SynchronizedStatement synchronizedStatement = new SynchronizedStatement(expression, code);
    configureAST(synchronizedStatement, syncNode);
    return synchronizedStatement;
}
Also used : 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) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 2 with SynchronizedStatement

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

the class SynchronizedASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        if (mNode.isAbstract()) {
            addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
            return;
        }
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode);
        if (lockExpr == null)
            return;
        Statement origCode = mNode.getCode();
        Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
        mNode.setCode(newCode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 3 with SynchronizedStatement

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

the class ReturnAdder method addReturnsIfNeeded.

private Statement addReturnsIfNeeded(final Statement statement, final VariableScope scope) {
    if (statement instanceof ReturnStatement || statement instanceof ThrowStatement || statement instanceof EmptyStatement || statement instanceof BytecodeSequence) {
        return statement;
    }
    if (statement == null) {
        ReturnStatement returnStatement = new ReturnStatement(nullX());
        listener.returnStatementAdded(returnStatement);
        return returnStatement;
    }
    if (statement instanceof ExpressionStatement) {
        Expression expression = ((ExpressionStatement) statement).getExpression();
        ReturnStatement returnStatement = new ReturnStatement(expression);
        returnStatement.copyStatementLabels(statement);
        returnStatement.setSourcePosition(statement.getLineNumber() < 0 ? expression : statement);
        listener.returnStatementAdded(returnStatement);
        return returnStatement;
    }
    if (statement instanceof SynchronizedStatement) {
        SynchronizedStatement syncStatement = (SynchronizedStatement) statement;
        Statement code = addReturnsIfNeeded(syncStatement.getCode(), scope);
        if (doAdd)
            syncStatement.setCode(code);
        return syncStatement;
    }
    if (statement instanceof IfStatement) {
        IfStatement ifElseStatement = (IfStatement) statement;
        Statement ifBlock = addReturnsIfNeeded(ifElseStatement.getIfBlock(), scope);
        Statement elseBlock = addReturnsIfNeeded(ifElseStatement.getElseBlock(), scope);
        if (doAdd) {
            ifElseStatement.setIfBlock(ifBlock);
            ifElseStatement.setElseBlock(elseBlock);
        }
        return ifElseStatement;
    }
    if (statement instanceof SwitchStatement) {
        SwitchStatement switchStatement = (SwitchStatement) statement;
        Statement defaultStatement = switchStatement.getDefaultStatement();
        List<CaseStatement> caseStatements = switchStatement.getCaseStatements();
        for (Iterator<CaseStatement> it = caseStatements.iterator(); it.hasNext(); ) {
            CaseStatement caseStatement = it.next();
            Statement code = adjustSwitchCaseCode(caseStatement.getCode(), scope, // GROOVY-9896: return if no default and last case lacks break
            defaultStatement == EmptyStatement.INSTANCE && !it.hasNext());
            if (doAdd)
                caseStatement.setCode(code);
        }
        defaultStatement = adjustSwitchCaseCode(defaultStatement, scope, true);
        if (doAdd)
            switchStatement.setDefaultStatement(defaultStatement);
        return switchStatement;
    }
    if (statement instanceof TryCatchStatement) {
        TryCatchStatement tryCatchFinally = (TryCatchStatement) statement;
        boolean[] missesReturn = new boolean[1];
        new ReturnAdder(returnStatement -> missesReturn[0] = true).addReturnsIfNeeded(tryCatchFinally.getFinallyStatement(), scope);
        boolean hasFinally = !(tryCatchFinally.getFinallyStatement() instanceof EmptyStatement);
        // there is nothing to do
        if (hasFinally && !missesReturn[0])
            return tryCatchFinally;
        // add returns to try and catch blocks
        Statement tryStatement = addReturnsIfNeeded(tryCatchFinally.getTryStatement(), scope);
        if (doAdd)
            tryCatchFinally.setTryStatement(tryStatement);
        for (CatchStatement catchStatement : tryCatchFinally.getCatchStatements()) {
            Statement code = addReturnsIfNeeded(catchStatement.getCode(), scope);
            if (doAdd)
                catchStatement.setCode(code);
        }
        return tryCatchFinally;
    }
    if (statement instanceof BlockStatement) {
        BlockStatement blockStatement = (BlockStatement) statement;
        if (blockStatement.isEmpty()) {
            ReturnStatement returnStatement = new ReturnStatement(nullX());
            returnStatement.copyStatementLabels(blockStatement);
            returnStatement.setSourcePosition(blockStatement);
            listener.returnStatementAdded(returnStatement);
            return returnStatement;
        } else {
            List<Statement> statements = blockStatement.getStatements();
            int lastIndex = statements.size() - 1;
            Statement last = addReturnsIfNeeded(statements.get(lastIndex), blockStatement.getVariableScope());
            if (doAdd)
                statements.set(lastIndex, last);
            return blockStatement;
        }
    }
    List<Statement> statements = new ArrayList<>(2);
    statements.add(statement);
    ReturnStatement returnStatement = new ReturnStatement(nullX());
    listener.returnStatementAdded(returnStatement);
    statements.add(returnStatement);
    BlockStatement blockStatement = new BlockStatement(statements, new VariableScope(scope));
    blockStatement.setSourcePosition(statement);
    return blockStatement;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) VariableScope(org.codehaus.groovy.ast.VariableScope) Iterator(java.util.Iterator) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) ArrayList(java.util.ArrayList) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Objects(java.util.Objects) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) List(java.util.List) MethodNode(org.codehaus.groovy.ast.MethodNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) GeneralUtils.nullX(org.codehaus.groovy.ast.tools.GeneralUtils.nullX) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) Expression(org.codehaus.groovy.ast.expr.Expression) DefaultGroovyMethods.last(org.codehaus.groovy.runtime.DefaultGroovyMethods.last) Statement(org.codehaus.groovy.ast.stmt.Statement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) Expression(org.codehaus.groovy.ast.expr.Expression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 4 with SynchronizedStatement

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

the class SynchronizedASTTransformation method visit.

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        if (mNode.isAbstract()) {
            addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
            return;
        }
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode);
        if (lockExpr == null)
            return;
        Statement origCode = mNode.getCode();
        Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
        mNode.setCode(newCode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 5 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)

Aggregations

SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)10 Statement (org.codehaus.groovy.ast.stmt.Statement)8 Expression (org.codehaus.groovy.ast.expr.Expression)6 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)5 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)5 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)5 MethodNode (org.codehaus.groovy.ast.MethodNode)3 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)3 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)3 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)3 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)3 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)3 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)3 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)3 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)3 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)3 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)3 TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)3 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)2