if (leftHand instanceof FieldAccess) {
Expression object = ((FieldAccess) leftHand).getExpression();
name = ((FieldAccess) leftHand).getName();
Type type = Type.getType(object);
if (!type.getName().equals(state.getCurrentClass().getName())) {
return;
}
newObject = (Expression) ASTNode.copySubtree(ast, object);
} else if (leftHand instanceof QualifiedName) {
Name object = ((QualifiedName) leftHand).getQualifier();
name = ((QualifiedName) leftHand).getName();
Type type = Type.getType(object);
if (!type.getName().equals(state.getCurrentClass().getName())) {
return;
}
newObject = (Expression) ASTNode.copySubtree(ast, object);
} else if (leftHand instanceof SimpleName) {
name = (SimpleName) leftHand;
} else {
return; // Some unknown situation.
}
// Get the owner of the left-hand side, if it is a field.
Type owner = Type.getOwner(leftHand);
if (owner == null) { // Not a field.
return;
}
// Get the class of the owner and test the modifiers of the field.
Class ownerClass;
boolean isStatic;
try {
ownerClass = owner.toClass(state.getClassLoader());
Field field = ownerClass.getDeclaredField(name.getIdentifier());
int modifiers = field.getModifiers();
if (!java.lang.reflect.Modifier.isPrivate(modifiers)) {
return; // Not handling non-private fields or final fields.
}
if (java.lang.reflect.Modifier.isFinal(modifiers)) {
if (!field.getType().isArray()) {
return;
}
}
isStatic = java.lang.reflect.Modifier.isStatic(modifiers);
} catch (ClassNotFoundException e) {
throw new ASTClassNotFoundException(owner.getName());
} catch (NoSuchFieldException e) {
// The field is not defined in this class.
return;
}
if (isStatic && !HANDLE_STATIC_FIELDS) {
return;
}
// The new method invocation to replace the assignment.
MethodInvocation invocation = ast.newMethodInvocation();
// Set the expression and name of the method invocation.
if (newObject != null) {
invocation.setExpression(newObject);
}
SimpleName newName = ast.newSimpleName(_getAssignMethodName(name
.getIdentifier(), isSpecial));
invocation.setName(newName);
// If the field is static, add the checkpoint object as the first
// argument.
if (isStatic) {
invocation.arguments().add(ast.newSimpleName(CHECKPOINT_NAME));
}
// Add an operator, if necessary.
Type type = Type.getType(node);
if (isSpecial && _assignOperators.containsKey(type.getName())) {
int i = 0;
String[] operators = _assignOperators.get(type.getName());
String operator;
if (node instanceof Assignment) {
operator = ((Assignment) node).getOperator().toString();
} else if (node instanceof PrefixExpression) {
operator = ((PrefixExpression) node).getOperator().toString();
} else {
operator = ((PostfixExpression) node).getOperator().toString();
}
for (; i < operators.length; i++) {
if (operators[i].equals(operator)) {
break;
}
}
if (node instanceof PrefixExpression) {
i += 2;
}
invocation.arguments().add(
ast.newNumberLiteral(Integer.toString(i)));
}
// Add all the indices into the argument list.
invocation.arguments().addAll(indices);
// Add the right-hand side expression to the argument list.
Type rightHandType = Type.getType(rightHand);
if (!isSpecial && type.isPrimitive() && !type.equals(rightHandType)) {
// Require an explicit conversion.
CastExpression castExpression = ast.newCastExpression();
castExpression.setType(createType(ast, type.getName()));