.finish();
}
}
private void generateSetter(ClassStructureBuilder<?> classBuilder, String property, BlockBuilder<?> setMethod) {
MetaMethod getterMethod = bindable.getBeanDescriptor().getReadMethodForProperty(property);
MetaMethod setterMethod = bindable.getBeanDescriptor().getWriteMethodForProperty(property);
if (setterMethod != null && !setterMethod.isFinal()) {
setMethod
.append(
If.cond(Stmt.loadVariable("property").invoke("equals", property))
.append(
Stmt.declareVariable("oldValue", setterMethod.getParameters()[0].getType().asBoxed(),
Stmt.loadVariable("this").invoke(getterMethod)))
.append(
Stmt.loadVariable("target").invoke(
setterMethod.getName(),
Cast.to(setterMethod.getParameters()[0].getType().asBoxed(),
Stmt.invokeStatic(Convert.class, "toModelValue",
setterMethod.getParameters()[0].getType().asBoxed().asClass(),
Stmt.loadVariable("value"),
Stmt.loadVariable("converters").invoke("get", Variable.get("property"))))))
.append(
Stmt.loadVariable("propertyChangeHandlerSupport").invoke(
"notifyHandlers",
Stmt.newObject(PropertyChangeEvent.class, Stmt.loadVariable("property"), Stmt
.loadVariable("oldValue"), Stmt.loadVariable("value"))))
.append(Stmt.returnVoid())
.finish()
);
MetaClass paramType = setterMethod.getParameters()[0].getType();
Statement callSetterOnTarget = Stmt.loadClassMember("target").invoke(setterMethod.getName(),
Cast.to(paramType, Stmt.loadVariable(property)));
// If the set method we are proxying returns a value, capture that value into a local variable
Statement returnValueOfSetter = EmptyStatement.INSTANCE;
if (!setterMethod.getReturnType().equals(MetaClassFactory.get(void.class))) {
callSetterOnTarget = Stmt.declareFinalVariable("returnValueOfSetter", setterMethod.getReturnType(), callSetterOnTarget);
returnValueOfSetter = Stmt.nestedCall(Refs.get("returnValueOfSetter")).returnValue();
}
classBuilder.publicMethod(setterMethod.getReturnType(), setterMethod.getName(),
Parameter.of(paramType, property))
.append(
Stmt.declareVariable("oldValue", Object.class, Stmt.loadClassMember("target").invoke(
getterMethod.getName())))
.append(callSetterOnTarget)