Search in sources :

Example 1 with Block

use of org.powermock.api.mockito.repackaged.cglib.core.Block in project powermock by powermock.

the class UndeclaredThrowableTransformer method begin_method.

public CodeEmitter begin_method(int access, final Signature sig, final Type[] exceptions) {
    CodeEmitter e = super.begin_method(access, sig, exceptions);
    if (TypeUtils.isAbstract(access) || sig.equals(Constants.SIG_STATIC)) {
        return e;
    }
    return new CodeEmitter(e) {

        private Block handler;

        /* init */
        {
            handler = begin_block();
        }

        public void visitMaxs(int maxStack, int maxLocals) {
            handler.end();
            EmitUtils.wrap_undeclared_throwable(this, handler, exceptions, wrapper);
            super.visitMaxs(maxStack, maxLocals);
        }
    };
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Block(org.powermock.api.mockito.repackaged.cglib.core.Block)

Example 2 with Block

use of org.powermock.api.mockito.repackaged.cglib.core.Block in project powermock by powermock.

the class BulkBeanEmitter method generateSet.

private void generateSet(final Class target, final Method[] setters) {
    // setPropertyValues
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SET_PROPERTY_VALUES, null);
    if (setters.length > 0) {
        Local index = e.make_local(Type.INT_TYPE);
        e.push(0);
        e.store_local(index);
        e.load_arg(0);
        e.checkcast(Type.getType(target));
        e.load_arg(1);
        Block handler = e.begin_block();
        int lastIndex = 0;
        for (int i = 0; i < setters.length; i++) {
            if (setters[i] != null) {
                MethodInfo setter = ReflectUtils.getMethodInfo(setters[i]);
                int diff = i - lastIndex;
                if (diff > 0) {
                    e.iinc(index, diff);
                    lastIndex = i;
                }
                e.dup2();
                e.aaload(i);
                e.unbox(setter.getSignature().getArgumentTypes()[0]);
                e.invoke(setter);
            }
        }
        handler.end();
        e.return_value();
        e.catch_exception(handler, Constants.TYPE_THROWABLE);
        e.new_instance(BULK_BEAN_EXCEPTION);
        e.dup_x1();
        e.swap();
        e.load_local(index);
        e.invoke_constructor(BULK_BEAN_EXCEPTION, CSTRUCT_EXCEPTION);
        e.athrow();
    } else {
        e.return_value();
    }
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Local(org.powermock.api.mockito.repackaged.cglib.core.Local) Block(org.powermock.api.mockito.repackaged.cglib.core.Block) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 3 with Block

use of org.powermock.api.mockito.repackaged.cglib.core.Block in project powermock by powermock.

the class FastClassEmitter method invokeSwitchHelper.

private static void invokeSwitchHelper(final CodeEmitter e, List members, final int arg, final Type base) {
    final List info = CollectionUtils.transform(members, MethodInfoTransformer.getInstance());
    final Label illegalArg = e.make_label();
    Block block = e.begin_block();
    e.process_switch(getIntRange(info.size()), new ProcessSwitchCallback() {

        public void processCase(int key, Label end) {
            MethodInfo method = (MethodInfo) info.get(key);
            Type[] types = method.getSignature().getArgumentTypes();
            for (int i = 0; i < types.length; i++) {
                e.load_arg(arg);
                e.aaload(i);
                e.unbox(types[i]);
            }
            // TODO: change method lookup process so MethodInfo will already reference base
            // instead of superclass when superclass method is inaccessible
            e.invoke(method, base);
            if (!TypeUtils.isConstructor(method)) {
                e.box(method.getSignature().getReturnType());
            }
            e.return_value();
        }

        public void processDefault() {
            e.goTo(illegalArg);
        }
    });
    block.end();
    EmitUtils.wrap_throwable(block, INVOCATION_TARGET_EXCEPTION);
    e.mark(illegalArg);
    e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Cannot find matching method/constructor");
}
Also used : ProcessSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback) Label(org.powermock.api.mockito.repackaged.asm.Label) Block(org.powermock.api.mockito.repackaged.cglib.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 4 with Block

use of org.powermock.api.mockito.repackaged.cglib.core.Block in project powermock by powermock.

the class InvocationHandlerGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        Signature impl = context.getImplSignature(method);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, impl.getName(), METHOD, null);
        CodeEmitter e = context.beginMethod(ce, method);
        Block handler = e.begin_block();
        context.emitCallback(e, context.getIndex(method));
        e.load_this();
        e.getfield(impl.getName());
        e.create_arg_array();
        e.invoke_interface(INVOCATION_HANDLER, INVOKE);
        e.unbox(method.getSignature().getReturnType());
        e.return_value();
        handler.end();
        EmitUtils.wrap_undeclared_throwable(e, handler, method.getExceptionTypes(), UNDECLARED_THROWABLE_EXCEPTION);
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Iterator(java.util.Iterator) Block(org.powermock.api.mockito.repackaged.cglib.core.Block) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Aggregations

Block (org.powermock.api.mockito.repackaged.cglib.core.Block)4 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)3 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)3 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Label (org.powermock.api.mockito.repackaged.asm.Label)1 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)1 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)1 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)1