private Block _createAssignmentBlock(AST ast, TypeAnalyzerState state,
String fieldName, Type fieldType, int indices, boolean special) {
Block block = ast.newBlock();
// Test if the checkpoint object is not null.
IfStatement ifStatement = ast.newIfStatement();
InfixExpression testExpression = ast.newInfixExpression();
InfixExpression condition1 = ast.newInfixExpression();
condition1.setLeftOperand(ast.newSimpleName(CHECKPOINT_NAME));
condition1.setOperator(InfixExpression.Operator.NOT_EQUALS);
condition1.setRightOperand(ast.newNullLiteral());
InfixExpression condition2 = ast.newInfixExpression();
MethodInvocation getTimestamp = ast.newMethodInvocation();
getTimestamp.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
getTimestamp.setName(ast.newSimpleName("getTimestamp"));
condition2.setLeftOperand(getTimestamp);
condition2.setOperator(InfixExpression.Operator.GREATER);
condition2.setRightOperand(ast.newNumberLiteral("0"));
testExpression.setLeftOperand(condition1);
testExpression.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
testExpression.setRightOperand(condition2);
ifStatement.setExpression(testExpression);
// The "then" branch.
Block thenBranch = ast.newBlock();
// Method call to store old value.
MethodInvocation recordInvocation = ast.newMethodInvocation();
recordInvocation.setExpression(ast
.newSimpleName(_getRecordName(fieldName)));
recordInvocation.setName(ast.newSimpleName("add"));
// If there are indices, create an integer array of those indices,
// and add it as an argument.
if (indices == 0) {
recordInvocation.arguments().add(ast.newNullLiteral());
} else {
ArrayCreation arrayCreation = ast.newArrayCreation();
ArrayType arrayType = ast.newArrayType(ast
.newPrimitiveType(PrimitiveType.INT));
ArrayInitializer initializer = ast.newArrayInitializer();
for (int i = 0; i < indices; i++) {
initializer.expressions().add(ast.newSimpleName("index" + i));
}
arrayCreation.setType(arrayType);
arrayCreation.setInitializer(initializer);
recordInvocation.arguments().add(arrayCreation);
}
// If there are indices, add them ("index0", "index1", ...) after the
// field.
Expression field = ast.newSimpleName(fieldName);
if (indices > 0) {
for (int i = 0; i < indices; i++) {
ArrayAccess arrayAccess = ast.newArrayAccess();
arrayAccess.setArray(field);
arrayAccess.setIndex(ast.newSimpleName("index" + i));
field = arrayAccess;
}
}
// Set the field as the next argument.
recordInvocation.arguments().add(field);
// Get current timestamp from the checkpoint object.
MethodInvocation timestampGetter = ast.newMethodInvocation();
timestampGetter.setExpression(ast.newSimpleName(CHECKPOINT_NAME));
timestampGetter.setName(ast.newSimpleName("getTimestamp"));
// Set the timestamp as the next argument.
recordInvocation.arguments().add(timestampGetter);
// The statement of the method call.
ExpressionStatement recordStatement = ast
.newExpressionStatement(recordInvocation);
thenBranch.statements().add(recordStatement);
ifStatement.setThenStatement(thenBranch);
block.statements().add(ifStatement);
// Finally, assign the new value to the field.
Assignment assignment = ast.newAssignment();
assignment