Search in sources :

Example 1 with MethodInfo

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

the class FixedValueGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        CodeEmitter e = context.beginMethod(ce, method);
        context.emitCallback(e, context.getIndex(method));
        e.invoke_interface(FIXED_VALUE, LOAD_OBJECT);
        e.unbox_or_zero(e.getReturnType());
        e.return_value();
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Iterator(java.util.Iterator) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 2 with MethodInfo

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

the class InvocationHandlerGenerator method generateStatic.

public void generateStatic(CodeEmitter e, Context context, List methods) {
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        EmitUtils.load_method(e, method);
        e.putfield(context.getImplSignature(method).getName());
    }
}
Also used : Iterator(java.util.Iterator) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 3 with MethodInfo

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

the class LazyLoaderGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    Set indexes = new HashSet();
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        if (TypeUtils.isProtected(method.getModifiers())) {
        // ignore protected methods
        } else {
            int index = context.getIndex(method);
            indexes.add(new Integer(index));
            CodeEmitter e = context.beginMethod(ce, method);
            e.load_this();
            e.dup();
            e.invoke_virtual_this(loadMethod(index));
            e.checkcast(method.getClassInfo().getType());
            e.load_args();
            e.invoke(method);
            e.return_value();
            e.end_method();
        }
    }
    for (Iterator it = indexes.iterator(); it.hasNext(); ) {
        int index = ((Integer) it.next()).intValue();
        String delegate = "CGLIB$LAZY_LOADER_" + index;
        ce.declare_field(Constants.ACC_PRIVATE, delegate, Constants.TYPE_OBJECT, null);
        CodeEmitter e = ce.begin_method(Constants.ACC_PRIVATE | Constants.ACC_SYNCHRONIZED | Constants.ACC_FINAL, loadMethod(index), null);
        e.load_this();
        e.getfield(delegate);
        e.dup();
        Label end = e.make_label();
        e.ifnonnull(end);
        e.pop();
        e.load_this();
        context.emitCallback(e, index);
        e.invoke_interface(LAZY_LOADER, LOAD_OBJECT);
        e.dup_x1();
        e.putfield(delegate);
        e.mark(end);
        e.return_value();
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) HashSet(java.util.HashSet) Set(java.util.Set) Iterator(java.util.Iterator) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) HashSet(java.util.HashSet)

Example 4 with MethodInfo

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

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

the class MethodInterceptorGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    Map sigMap = new HashMap();
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        Signature sig = method.getSignature();
        Signature impl = context.getImplSignature(method);
        String methodField = getMethodField(impl);
        String methodProxyField = getMethodProxyField(impl);
        sigMap.put(sig.toString(), methodProxyField);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodField, METHOD, null);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodProxyField, METHOD_PROXY, null);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, EMPTY_ARGS_NAME, Constants.TYPE_OBJECT_ARRAY, null);
        CodeEmitter e;
        // access method
        e = ce.begin_method(Constants.ACC_FINAL, impl, method.getExceptionTypes());
        superHelper(e, method);
        e.return_value();
        e.end_method();
        // around method
        e = context.beginMethod(ce, method);
        Label nullInterceptor = e.make_label();
        context.emitCallback(e, context.getIndex(method));
        e.dup();
        e.ifnull(nullInterceptor);
        e.load_this();
        e.getfield(methodField);
        if (sig.getArgumentTypes().length == 0) {
            e.getfield(EMPTY_ARGS_NAME);
        } else {
            e.create_arg_array();
        }
        e.getfield(methodProxyField);
        e.invoke_interface(METHOD_INTERCEPTOR, INTERCEPT);
        e.unbox_or_zero(sig.getReturnType());
        e.return_value();
        e.mark(nullInterceptor);
        superHelper(e, method);
        e.return_value();
        e.end_method();
    }
    generateFindProxy(ce, sigMap);
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) HashMap(java.util.HashMap) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Iterator(java.util.Iterator) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) HashMap(java.util.HashMap) Map(java.util.Map)

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