private class ArrayVisitor extends JModVisitor {
// @Override
public void endVisit(JBinaryOperation x, Context ctx) {
if (x.getOp() == JBinaryOperator.ASG && x.getLhs() instanceof JArrayRef) {
JArrayRef arrayRef = (JArrayRef) x.getLhs();
if (arrayRef.getType() instanceof JNullType) {
// will generate a null pointer exception instead
return;
}
JArrayType arrayType = (JArrayType) arrayRef.getInstance().getType();
JType elementType = arrayType.getElementType();
// see if we need to do a checked store
// primitives and (effectively) final are statically correct
if (elementType instanceof JReferenceType
&& (!((JReferenceType) elementType).isFinal())
|| elementType != x.getRhs().getType()) {
// replace this assignment with a call to setCheck()
JMethodCall call = new JMethodCall(program, x.getSourceInfo(), null,
setCheckMethod);
call.getArgs().add(arrayRef.getInstance());
call.getArgs().add(arrayRef.getIndexExpr());
call.getArgs().add(x.getRhs());
ctx.replaceMe(call);
}
}
}