Search in sources :

Example 1 with Signature

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

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

Example 3 with Signature

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

the class MethodProxy method create.

/**
     * For internal use by {@link Enhancer} only; see the {@link org.powermock.api.mockito.repackaged.cglib.reflect.FastMethod} class
     * for similar functionality.
     */
public static MethodProxy create(Class c1, Class c2, String desc, String name1, String name2) {
    MethodProxy proxy = new MethodProxy();
    proxy.sig1 = new Signature(name1, desc);
    proxy.sig2 = new Signature(name2, desc);
    proxy.createInfo = new CreateInfo(c1, c2);
    return proxy;
}
Also used : Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature)

Example 4 with Signature

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

the class Enhancer method emitConstructors.

private void emitConstructors(ClassEmitter ce, List constructors) {
    boolean seenNull = false;
    for (Iterator it = constructors.iterator(); it.hasNext(); ) {
        MethodInfo constructor = (MethodInfo) it.next();
        CodeEmitter e = EmitUtils.begin_method(ce, constructor, Constants.ACC_PUBLIC);
        e.load_this();
        e.dup();
        e.load_args();
        Signature sig = constructor.getSignature();
        seenNull = seenNull || sig.getDescriptor().equals("()V");
        e.super_invoke_constructor(sig);
        e.invoke_static_this(BIND_CALLBACKS);
        if (!interceptDuringConstruction) {
            e.load_this();
            e.push(1);
            e.putfield(CONSTRUCTED_FIELD);
        }
        e.return_value();
        e.end_method();
    }
    if (!classOnly && !seenNull && arguments == null)
        throw new IllegalArgumentException("Superclass has no null constructors but no arguments were given");
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Iterator(java.util.Iterator) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 5 with Signature

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

the class AccessFieldTransformer method declare_field.

public void declare_field(int access, final String name, Type type, Object value) {
    super.declare_field(access, name, type, value);
    String property = TypeUtils.upperFirst(callback.getPropertyName(getClassType(), name));
    if (property != null) {
        CodeEmitter e;
        e = begin_method(Constants.ACC_PUBLIC, new Signature("get" + property, type, Constants.TYPES_EMPTY), null);
        e.load_this();
        e.getfield(name);
        e.return_value();
        e.end_method();
        e = begin_method(Constants.ACC_PUBLIC, new Signature("set" + property, Type.VOID_TYPE, new Type[] { type }), null);
        e.load_this();
        e.load_arg(0);
        e.putfield(name);
        e.return_value();
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature)

Aggregations

Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)9 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)6 Iterator (java.util.Iterator)5 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)4 Type (org.powermock.api.mockito.repackaged.asm.Type)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Method (java.lang.reflect.Method)1 List (java.util.List)1 Label (org.powermock.api.mockito.repackaged.asm.Label)1 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)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 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)1