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))));
}
}
Aggregations