Search in sources :

Example 1 with Local

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

the class MethodInterceptorGenerator method generateStatic.

public void generateStatic(CodeEmitter e, Context context, List methods) throws Exception {
    /* generates:
           static {
             Class thisClass = Class.forName("NameOfThisClass");
             Class cls = Class.forName("java.lang.Object");
             String[] sigs = new String[]{ "toString", "()Ljava/lang/String;", ... };
             Method[] methods = cls.getDeclaredMethods();
             methods = ReflectUtils.findMethods(sigs, methods);
             METHOD_0 = methods[0];
             CGLIB$ACCESS_0 = MethodProxy.create(cls, thisClass, "()Ljava/lang/String;", "toString", "CGLIB$ACCESS_0");
             ...
           }
        */
    e.push(0);
    e.newarray();
    e.putfield(EMPTY_ARGS_NAME);
    Local thisclass = e.make_local();
    Local declaringclass = e.make_local();
    EmitUtils.load_class_this(e);
    e.store_local(thisclass);
    Map methodsByClass = CollectionUtils.bucket(methods, METHOD_TO_CLASS);
    for (Iterator i = methodsByClass.keySet().iterator(); i.hasNext(); ) {
        ClassInfo classInfo = (ClassInfo) i.next();
        List classMethods = (List) methodsByClass.get(classInfo);
        e.push(2 * classMethods.size());
        e.newarray(Constants.TYPE_STRING);
        for (int index = 0; index < classMethods.size(); index++) {
            MethodInfo method = (MethodInfo) classMethods.get(index);
            Signature sig = method.getSignature();
            e.dup();
            e.push(2 * index);
            e.push(sig.getName());
            e.aastore();
            e.dup();
            e.push(2 * index + 1);
            e.push(sig.getDescriptor());
            e.aastore();
        }
        EmitUtils.load_class(e, classInfo.getType());
        e.dup();
        e.store_local(declaringclass);
        e.invoke_virtual(Constants.TYPE_CLASS, GET_DECLARED_METHODS);
        e.invoke_static(REFLECT_UTILS, FIND_METHODS);
        for (int index = 0; index < classMethods.size(); index++) {
            MethodInfo method = (MethodInfo) classMethods.get(index);
            Signature sig = method.getSignature();
            Signature impl = context.getImplSignature(method);
            e.dup();
            e.push(index);
            e.array_load(METHOD);
            e.putfield(getMethodField(impl));
            e.load_local(declaringclass);
            e.load_local(thisclass);
            e.push(sig.getDescriptor());
            e.push(sig.getName());
            e.push(impl.getName());
            e.invoke_static(METHOD_PROXY, MAKE_PROXY);
            e.putfield(getMethodProxyField(impl));
        }
        e.pop();
    }
}
Also used : Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Iterator(java.util.Iterator) Local(org.powermock.api.mockito.repackaged.cglib.core.Local) List(java.util.List) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) HashMap(java.util.HashMap) Map(java.util.Map) ClassInfo(org.powermock.api.mockito.repackaged.cglib.core.ClassInfo)

Example 2 with Local

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

the class BulkBeanEmitter method generateGet.

private void generateGet(final Class target, final Method[] getters) {
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, GET_PROPERTY_VALUES, null);
    if (getters.length >= 0) {
        e.load_arg(0);
        e.checkcast(Type.getType(target));
        Local bean = e.make_local();
        e.store_local(bean);
        for (int i = 0; i < getters.length; i++) {
            if (getters[i] != null) {
                MethodInfo getter = ReflectUtils.getMethodInfo(getters[i]);
                e.load_arg(1);
                e.push(i);
                e.load_local(bean);
                e.invoke(getter);
                e.box(getter.getSignature().getReturnType());
                e.aastore();
            }
        }
    }
    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) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 3 with Local

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

the class Enhancer method emitBindCallbacks.

private void emitBindCallbacks(ClassEmitter ce) {
    CodeEmitter e = ce.begin_method(Constants.PRIVATE_FINAL_STATIC, BIND_CALLBACKS, null);
    Local me = e.make_local();
    e.load_arg(0);
    e.checkcast_this();
    e.store_local(me);
    Label end = e.make_label();
    e.load_local(me);
    e.getfield(BOUND_FIELD);
    e.if_jump(e.NE, end);
    e.load_local(me);
    e.push(1);
    e.putfield(BOUND_FIELD);
    e.getfield(THREAD_CALLBACKS_FIELD);
    e.invoke_virtual(THREAD_LOCAL, THREAD_LOCAL_GET);
    e.dup();
    Label found_callback = e.make_label();
    e.ifnonnull(found_callback);
    e.pop();
    e.getfield(STATIC_CALLBACKS_FIELD);
    e.dup();
    e.ifnonnull(found_callback);
    e.pop();
    e.goTo(end);
    e.mark(found_callback);
    e.checkcast(CALLBACK_ARRAY);
    e.load_local(me);
    e.swap();
    for (int i = callbackTypes.length - 1; i >= 0; i--) {
        if (i != 0) {
            e.dup2();
        }
        e.aaload(i);
        e.checkcast(callbackTypes[i]);
        e.putfield(getCallbackField(i));
    }
    e.mark(end);
    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) Local(org.powermock.api.mockito.repackaged.cglib.core.Local)

Example 4 with Local

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

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

the class InterceptFieldTransformer method addReadMethod.

private void addReadMethod(String name, Type type) {
    CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, readMethodSig(name, type.getDescriptor()), null);
    e.load_this();
    e.getfield(name);
    e.load_this();
    e.invoke_interface(ENABLED, ENABLED_GET);
    Label intercept = e.make_label();
    e.ifnonnull(intercept);
    e.return_value();
    e.mark(intercept);
    Local result = e.make_local(type);
    e.store_local(result);
    e.load_this();
    e.invoke_interface(ENABLED, ENABLED_GET);
    e.load_this();
    e.push(name);
    e.load_local(result);
    e.invoke_interface(CALLBACK, readCallbackSig(type));
    if (!TypeUtils.isPrimitive(type)) {
        e.checkcast(type);
    }
    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) Local(org.powermock.api.mockito.repackaged.cglib.core.Local)

Aggregations

Local (org.powermock.api.mockito.repackaged.cglib.core.Local)6 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)5 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)3 Label (org.powermock.api.mockito.repackaged.asm.Label)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Type (org.powermock.api.mockito.repackaged.asm.Type)1 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)1 ClassInfo (org.powermock.api.mockito.repackaged.cglib.core.ClassInfo)1 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)1