Search in sources :

Example 11 with MethodInfo

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

the class Enhancer method emitNewInstanceMultiarg.

private void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, MULTIARG_NEW_INSTANCE, null);
    e.load_arg(2);
    e.invoke_static_this(SET_THREAD_CALLBACKS);
    e.new_instance_this();
    e.dup();
    e.load_arg(0);
    EmitUtils.constructor_switch(e, constructors, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            MethodInfo constructor = (MethodInfo) key;
            Type[] types = constructor.getSignature().getArgumentTypes();
            for (int i = 0; i < types.length; i++) {
                e.load_arg(1);
                e.push(i);
                e.aaload();
                e.unbox(types[i]);
            }
            e.invoke_constructor_this(constructor.getSignature());
            e.goTo(end);
        }

        public void processDefault() {
            e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Constructor not found");
        }
    });
    e.aconst_null();
    e.invoke_static_this(SET_THREAD_CALLBACKS);
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 12 with MethodInfo

use of org.powermock.api.mockito.repackaged.cglib.core.MethodInfo 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 13 with MethodInfo

use of org.powermock.api.mockito.repackaged.cglib.core.MethodInfo 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 14 with MethodInfo

use of org.powermock.api.mockito.repackaged.cglib.core.MethodInfo 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)

Example 15 with MethodInfo

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

the class BeanMapEmitter method generatePut.

private void generatePut(Class type, final Map setters) {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, BEAN_MAP_PUT, null);
    e.load_arg(0);
    e.checkcast(Type.getType(type));
    e.load_arg(1);
    e.checkcast(Constants.TYPE_STRING);
    EmitUtils.string_switch(e, getNames(setters), Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            PropertyDescriptor pd = (PropertyDescriptor) setters.get(key);
            if (pd.getReadMethod() == null) {
                e.aconst_null();
            } else {
                MethodInfo read = ReflectUtils.getMethodInfo(pd.getReadMethod());
                e.dup();
                e.invoke(read);
                e.box(read.getSignature().getReturnType());
            }
            // move old value behind bean
            e.swap();
            // new value
            e.load_arg(2);
            MethodInfo write = ReflectUtils.getMethodInfo(pd.getWriteMethod());
            e.unbox(write.getSignature().getArgumentTypes()[0]);
            e.invoke(write);
            e.return_value();
        }

        public void processDefault() {
        // fall-through
        }
    });
    e.aconst_null();
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) PropertyDescriptor(java.beans.PropertyDescriptor) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Aggregations

MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)16 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)13 Iterator (java.util.Iterator)10 Label (org.powermock.api.mockito.repackaged.asm.Label)7 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)3 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)3 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ClassEmitter (org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter)1 ClassInfo (org.powermock.api.mockito.repackaged.cglib.core.ClassInfo)1 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)1