protected ValuedFieldList convertByReplacementDef(
final ValuedFieldList valuedFieldList,
final FieldInfo[] fieldsToSetInfo,
final ClassReplacementDef replacementDef)
throws ConversionException {
final ValuedFieldList result = new ValuedFieldList();
// index of read field
int index = 0;
for (ObjectAndPersistInfo fieldValue : valuedFieldList
.getFieldValueList()) {
try {
// replacement for read field by index
final FieldReplacementDef fieldReplacementDef;
fieldReplacementDef = replacementDef
.getReplacementFieldInfo(index);
if (fieldReplacementDef == null) {
result.setFieldValue(index, fieldValue);
} else if (!fieldReplacementDef.isDeletedField()) {
/*
* set result with replacement field info, target field
* index, and field value
*/
final FieldInfo newFieldInfo = fieldReplacementDef
.getFieldInfo();
int fieldIndex = fieldReplacementDef.getIndex();
if (fieldIndex == -1) {
// unknown replacement field index
for (fieldIndex = 0; fieldIndex < fieldsToSetInfo.length// NOPMD
&& !newFieldInfo
.equals(fieldsToSetInfo[fieldIndex]); fieldIndex++)
;
if (fieldIndex == fieldsToSetInfo.length) {
throw new ConversionException(
"unexisting field in object to set fields:\n"
+ newFieldInfo + "\nin:\n"
+ Arrays.toString(fieldsToSetInfo));
}
} else {
if (fieldIndex >= fieldsToSetInfo.length) {
throw new ConversionException(
"bad new field index " + fieldIndex
+ " in:\n"
+ Arrays.toString(fieldsToSetInfo)
+ "\nfor field:\n" + newFieldInfo);
}
// this assertion do not works for
// serializable/externalizable
// if (!newFieldInfo
// .equals(fieldsToSetInfo[fieldIndex])) {
// throw new ConversionException("\n index "
// + index + ",new field:\n"
// + newFieldInfo
// + "\n mismatch with field:\n"
// + fieldsToSetInfo[fieldIndex]
// + " at index " + fieldIndex);
// }
}
result.addNewFieldAndItsValue(fieldIndex, newFieldInfo,
fieldValue);
}
} catch (ConversionException exception) {
throw new ConversionException("\nindex " + index + " in:\n"
+ valuedFieldList + "\nreplacementDef:\n"