}
// For expression.name on the left-hand side, set newObject to be the
// expression and name to be the name. newObject may be null.
Expression newObject = null;
SimpleName name;
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.