String resourcesFieldName = transformation.getResourcesFieldName();
String writeMethodName = transformation.newMemberName("write", fieldName);
BodyBuilder builder = new BodyBuilder();
builder.begin();
builder.addln(
"%s.persistFieldChange(\"%s\", ($w) $1);",
resourcesFieldName,
logicalFieldName);
builder.addln("%s = $1;", fieldName);
builder.end();
transformation.addMethod(new TransformMethodSignature(Modifier.PRIVATE, "void", writeMethodName,
new String[]
{fieldType}, null), builder.toString());
transformation.replaceWriteAccess(fieldName, writeMethodName);
builder.clear();
builder.begin();
// Check to see if there's a recorded change for this component, this field.
builder.addln("if (%s.hasFieldChange(\"%s\"))", resourcesFieldName, logicalFieldName);
String wrapperType = TransformUtils.getWrapperTypeName(fieldType);
// Get the value, cast it to the correct type (or wrapper type)
builder.add(
" %s = ((%s) %s.getFieldChange(\"%s\"))",
fieldName,
wrapperType,
resourcesFieldName,
logicalFieldName);
// For primtive types, add in the method call to unwrap the wrapper type to a primitive type
String unwrapMethodName = TransformUtils.getUnwrapperMethodName(fieldType);
if (unwrapMethodName == null)
builder.addln(";");
else
builder.addln(".%s();", unwrapMethodName);
builder.end();
transformation.extendMethod(
TransformConstants.CONTAINING_PAGE_DID_ATTACH_SIGNATURE,
builder.toString());
transformation.claimField(fieldName, annotation);
}