Search in sources :

Example 1 with WhenCallback

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();
        }
    });
}
Also used : Condition(org.apache.tapestry5.plastic.Condition) WhenCallback(org.apache.tapestry5.plastic.WhenCallback) InstructionBuilder(org.apache.tapestry5.plastic.InstructionBuilder) PlasticClass(org.apache.tapestry5.plastic.PlasticClass) InstructionBuilderCallback(org.apache.tapestry5.plastic.InstructionBuilderCallback)

Example 2 with WhenCallback

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;
}
Also used : Label(org.apache.tapestry5.internal.plastic.asm.Label)

Aggregations

Label (org.apache.tapestry5.internal.plastic.asm.Label)1 Condition (org.apache.tapestry5.plastic.Condition)1 InstructionBuilder (org.apache.tapestry5.plastic.InstructionBuilder)1 InstructionBuilderCallback (org.apache.tapestry5.plastic.InstructionBuilderCallback)1 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)1 WhenCallback (org.apache.tapestry5.plastic.WhenCallback)1