.append(
Stmt.returnVoid())
.finish()
);
MetaClass paramType = setterMethod.getParameters()[0].getType();
Statement callSetterOnTarget = 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, target().invoke(getterMethod.getName())))
.append(callSetterOnTarget)
.append(Stmt.declareVariable("widget", Widget.class, field("bindings").invoke("get", property)))
.append(
If.instanceOf(Variable.get("widget"), HasValue.class)
.append(
Stmt.castTo(HasValue.class, Stmt.loadVariable("widget")).invoke(
"setValue",
Stmt.invokeStatic(Convert.class, "toWidgetValue",
Variable.get("widget"),
paramType.asBoxed().asClass(),
Stmt.castTo(paramType.asBoxed(), Stmt.loadVariable(property)),
field("converters").invoke("get", property)),
true))
.finish()
.elseif_(
Bool.instanceOf(Variable.get("widget"), HasText.class))
.append(
Stmt.castTo(HasText.class, Stmt.loadVariable("widget"))
.invoke(
"setText",
Stmt.castTo(String.class,
Stmt.invokeStatic(Convert.class, "toWidgetValue", String.class,
paramType.asBoxed().asClass(),
Stmt.castTo(paramType.asBoxed(), Stmt.loadVariable(property)),
field("converters").invoke("get", property)
)
)
)
)