JExpression instance = ((JArrayRef) x.getLhs()).getInstance();
if (instance.getType() instanceof JNullType) {
// will generate a null pointer exception instead
return;
}
JArrayType lhsArrayType = (JArrayType) instance.getType();
JType elementType = lhsArrayType.getElementType();
// primitives are statically correct
if (!(elementType instanceof JReferenceType)) {
return;
}
// element type being final means the assignment is statically correct
if (((JReferenceType) elementType).isFinal()) {
return;
}
/*
* For every instantiated array type that could -in theory- be the
* runtime type of the lhs, we must record a cast from the rhs to the
* prospective element type of the lhs.
*/
JTypeOracle typeOracle = program.typeOracle;
JType rhsType = x.getRhs().getType();
assert (rhsType instanceof JReferenceType);
JReferenceType refRhsType = (JReferenceType) rhsType;
for (Iterator it = instantiatedArrayTypes.iterator(); it.hasNext();) {
JArrayType arrayType = (JArrayType) it.next();
if (typeOracle.canTheoreticallyCast(arrayType, lhsArrayType)) {
JType itElementType = arrayType.getElementType();
if (itElementType instanceof JReferenceType) {
recordCastInternal((JReferenceType) itElementType, refRhsType);
}
}
}