Search in sources :

Example 11 with TryCatchStatement

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

the class ReadWriteLockASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    final boolean isWriteLock;
    if (READ_LOCK_TYPE.equals(node.getClassNode())) {
        isWriteLock = false;
    } else if (WRITE_LOCK_TYPE.equals(node.getClassNode())) {
        isWriteLock = true;
    } else {
        throw new GroovyBugError("Internal error: expecting [" + READ_LOCK_TYPE.getName() + ", " + WRITE_LOCK_TYPE.getName() + "]" + " but got: " + node.getClassNode().getName());
    }
    String myTypeName = "@" + node.getClassNode().getNameWithoutPackage();
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode.isStatic(), myTypeName);
        if (lockExpr == null)
            return;
        // get lock type
        final Expression lockType;
        if (isWriteLock) {
            lockType = callX(varX(lockExpr, LOCK_TYPE), "writeLock");
        } else {
            lockType = callX(varX(lockExpr, LOCK_TYPE), "readLock");
        }
        Expression acquireLock = callX(lockType, "lock");
        Expression releaseLock = callX(lockType, "unlock");
        Statement originalCode = mNode.getCode();
        mNode.setCode(block(stmt(acquireLock), new TryCatchStatement(originalCode, stmt(releaseLock))));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement)

Aggregations

TryCatchStatement (org.codehaus.groovy.ast.stmt.TryCatchStatement)11 CatchStatement (org.codehaus.groovy.ast.stmt.CatchStatement)8 Statement (org.codehaus.groovy.ast.stmt.Statement)8 ClassNode (org.codehaus.groovy.ast.ClassNode)6 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)6 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)6 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)6 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)4 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)4 ThrowStatement (org.codehaus.groovy.ast.stmt.ThrowStatement)4 Expression (org.codehaus.groovy.ast.expr.Expression)3 AssertStatement (org.codehaus.groovy.ast.stmt.AssertStatement)3 BreakStatement (org.codehaus.groovy.ast.stmt.BreakStatement)3 CaseStatement (org.codehaus.groovy.ast.stmt.CaseStatement)3 ContinueStatement (org.codehaus.groovy.ast.stmt.ContinueStatement)3 ForStatement (org.codehaus.groovy.ast.stmt.ForStatement)3 SwitchStatement (org.codehaus.groovy.ast.stmt.SwitchStatement)3 SynchronizedStatement (org.codehaus.groovy.ast.stmt.SynchronizedStatement)3 WhileStatement (org.codehaus.groovy.ast.stmt.WhileStatement)3 HashMap (java.util.HashMap)2