Search in sources :

Example 6 with MethodInfo

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

the class Enhancer method emitMethods.

private void emitMethods(final ClassEmitter ce, List methods, List actualMethods) {
    CallbackGenerator[] generators = CallbackInfo.getGenerators(callbackTypes);
    Map groups = new HashMap();
    final Map indexes = new HashMap();
    final Map originalModifiers = new HashMap();
    final Map positions = CollectionUtils.getIndexMap(methods);
    Iterator it1 = methods.iterator();
    Iterator it2 = (actualMethods != null) ? actualMethods.iterator() : null;
    while (it1.hasNext()) {
        MethodInfo method = (MethodInfo) it1.next();
        Method actualMethod = (it2 != null) ? (Method) it2.next() : null;
        int index = filter.accept(actualMethod, actualMethods);
        if (index >= callbackTypes.length) {
            throw new IllegalArgumentException("Callback filter returned an index that is too large: " + index);
        }
        originalModifiers.put(method, new Integer((actualMethod != null) ? actualMethod.getModifiers() : method.getModifiers()));
        indexes.put(method, new Integer(index));
        List group = (List) groups.get(generators[index]);
        if (group == null) {
            groups.put(generators[index], group = new ArrayList(methods.size()));
        }
        group.add(method);
    }
    Set seenGen = new HashSet();
    CodeEmitter se = ce.getStaticHook();
    se.new_instance(THREAD_LOCAL);
    se.dup();
    se.invoke_constructor(THREAD_LOCAL, CSTRUCT_NULL);
    se.putfield(THREAD_CALLBACKS_FIELD);
    final Object[] state = new Object[1];
    CallbackGenerator.Context context = new CallbackGenerator.Context() {

        public ClassLoader getClassLoader() {
            return Enhancer.this.getClassLoader();
        }

        public int getOriginalModifiers(MethodInfo method) {
            return ((Integer) originalModifiers.get(method)).intValue();
        }

        public int getIndex(MethodInfo method) {
            return ((Integer) indexes.get(method)).intValue();
        }

        public void emitCallback(CodeEmitter e, int index) {
            emitCurrentCallback(e, index);
        }

        public Signature getImplSignature(MethodInfo method) {
            return rename(method.getSignature(), ((Integer) positions.get(method)).intValue());
        }

        public CodeEmitter beginMethod(ClassEmitter ce, MethodInfo method) {
            CodeEmitter e = EmitUtils.begin_method(ce, method);
            if (!interceptDuringConstruction && !TypeUtils.isAbstract(method.getModifiers())) {
                Label constructed = e.make_label();
                e.load_this();
                e.getfield(CONSTRUCTED_FIELD);
                e.if_jump(e.NE, constructed);
                e.load_this();
                e.load_args();
                e.super_invoke();
                e.return_value();
                e.mark(constructed);
            }
            return e;
        }
    };
    for (int i = 0; i < callbackTypes.length; i++) {
        CallbackGenerator gen = generators[i];
        if (!seenGen.contains(gen)) {
            seenGen.add(gen);
            final List fmethods = (List) groups.get(gen);
            if (fmethods != null) {
                try {
                    gen.generate(ce, context, fmethods);
                    gen.generateStatic(se, context, fmethods);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new CodeGenerationException(x);
                }
            }
        }
    }
    se.return_value();
    se.end_method();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassEmitter(org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter) Label(org.powermock.api.mockito.repackaged.asm.Label) Method(java.lang.reflect.Method) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) Iterator(java.util.Iterator) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 7 with MethodInfo

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

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

the class NoOpGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        if (TypeUtils.isProtected(context.getOriginalModifiers(method)) && TypeUtils.isPublic(method.getModifiers())) {
            CodeEmitter e = EmitUtils.begin_method(ce, method);
            e.load_this();
            e.load_args();
            e.super_invoke();
            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 9 with MethodInfo

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

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

the class DispatcherGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        if (!TypeUtils.isProtected(method.getModifiers())) {
            CodeEmitter e = context.beginMethod(ce, method);
            context.emitCallback(e, context.getIndex(method));
            if (proxyRef) {
                e.load_this();
                e.invoke_interface(PROXY_REF_DISPATCHER, PROXY_REF_LOAD_OBJECT);
            } else {
                e.invoke_interface(DISPATCHER, LOAD_OBJECT);
            }
            e.checkcast(method.getClassInfo().getType());
            e.load_args();
            e.invoke(method);
            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)

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