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();
JType elementType = arrayRef.getType();
if (elementType instanceof JNullType) {
// will generate a null pointer exception instead
return;
}
/*
* See if we need to do a checked store. Primitives and (effectively)
* final are statically correct.
*/
if (elementType instanceof JReferenceType) {
if (!((JReferenceType) elementType).isFinal()
|| !program.typeOracle.canTriviallyCast(
(JReferenceType) x.getRhs().getType(),
(JReferenceType) elementType)) {
// replace this assignment with a call to setCheck()
JMethodCall call = new JMethodCall(x.getSourceInfo(), null,
setCheckMethod);
call.addArgs(arrayRef.getInstance(), arrayRef.getIndexExpr(),
x.getRhs());
ctx.replaceMe(call);
}
}
}