Search in sources :

Example 1 with ClassInfo

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

Aggregations

HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 ClassInfo (org.powermock.api.mockito.repackaged.cglib.core.ClassInfo)1 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)1 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)1 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)1