Search in sources :

Example 41 with CodeEmitter

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

the class Enhancer method emitSetCallbacks.

private void emitSetCallbacks(ClassEmitter ce) {
    CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACKS, null);
    e.load_this();
    e.load_arg(0);
    for (int i = 0; i < callbackTypes.length; i++) {
        e.dup2();
        e.aaload(i);
        e.checkcast(callbackTypes[i]);
        e.putfield(getCallbackField(i));
    }
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)

Example 42 with CodeEmitter

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

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

the class MethodInterceptorGenerator method generateFindProxy.

public void generateFindProxy(ClassEmitter ce, final Map sigMap) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FIND_PROXY, null);
    e.load_arg(0);
    e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
    ObjectSwitchCallback callback = new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            e.getfield((String) sigMap.get(key));
            e.return_value();
        }

        public void processDefault() {
            e.aconst_null();
            e.return_value();
        }
    };
    EmitUtils.string_switch(e, (String[]) sigMap.keySet().toArray(new String[0]), Constants.SWITCH_STYLE_HASH, callback);
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 44 with CodeEmitter

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

Example 45 with CodeEmitter

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

the class BeanMapEmitter method generateKeySet.

private void generateKeySet(String[] allNames) {
    // static initializer
    declare_field(Constants.ACC_STATIC | Constants.ACC_PRIVATE, "keys", FIXED_KEY_SET, null);
    CodeEmitter e = begin_static();
    e.new_instance(FIXED_KEY_SET);
    e.dup();
    EmitUtils.push_array(e, allNames);
    e.invoke_constructor(FIXED_KEY_SET, CSTRUCT_STRING_ARRAY);
    e.putfield("keys");
    e.return_value();
    e.end_method();
    // keySet
    e = begin_method(Constants.ACC_PUBLIC, KEY_SET, null);
    e.load_this();
    e.getfield("keys");
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)

Aggregations

CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)47 Label (org.powermock.api.mockito.repackaged.asm.Label)17 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)13 Type (org.powermock.api.mockito.repackaged.asm.Type)9 Iterator (java.util.Iterator)8 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)7 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)6 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)5 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)4 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)4 PropertyDescriptor (java.beans.PropertyDescriptor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 MethodInfoTransformer (org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer)2