use of org.apache.tapestry5.plastic.WhenCallback in project tapestry-5 by apache.
the class ChainBuilderImpl method implementMethod.
private void implementMethod(PlasticClass plasticClass, final Method method, final PlasticField commandsField) {
plasticClass.introduceMethod(method).changeImplementation(new InstructionBuilderCallback() {
@Override
public void doBuild(InstructionBuilder builder) {
builder.loadThis().getField(commandsField).iterateArray(new InstructionBuilderCallback() {
@Override
public void doBuild(InstructionBuilder builder) {
// The command is on the stack; add the elements and invoke the method.
builder.loadArguments().invoke(method);
Class returnType = method.getReturnType();
if (returnType == void.class)
return;
final boolean wide = returnType == long.class || returnType == double.class;
if (wide)
builder.dupeWide();
else
builder.dupe();
if (returnType == float.class) {
builder.loadConstant(0f).compareSpecial("float");
}
if (returnType == long.class) {
builder.loadConstant(0l).compareSpecial("long");
}
if (returnType == double.class) {
builder.loadConstant(0d).compareSpecial("double");
}
Condition condition = returnType.isPrimitive() ? Condition.NON_ZERO : Condition.NON_NULL;
builder.when(condition, new WhenCallback() {
@Override
public void ifTrue(InstructionBuilder builder) {
builder.returnResult();
}
@Override
public void ifFalse(InstructionBuilder builder) {
if (wide)
builder.popWide();
else
builder.pop();
}
});
}
});
builder.returnDefaultValue();
}
});
}
use of org.apache.tapestry5.plastic.WhenCallback in project tapestry-5 by apache.
the class InstructionBuilderImpl method when.
@Override
public InstructionBuilder when(Condition condition, final WhenCallback callback) {
check();
assert condition != null;
assert callback != null;
Label ifFalseLabel = new Label();
Label endIfLabel = new Label();
v.visitJumpInsn(conditionToOpcode.get(condition), ifFalseLabel);
callback.ifTrue(this);
v.visitJumpInsn(GOTO, endIfLabel);
v.visitLabel(ifFalseLabel);
callback.ifFalse(this);
v.visitLabel(endIfLabel);
return this;
}
Aggregations