Search in sources :

Example 31 with Label

use of org.powermock.api.mockito.repackaged.asm.Label in project powermock by powermock.

the class Enhancer method emitGetCallback.

private void emitGetCallback(ClassEmitter ce, int[] keys) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACK, null);
    e.load_this();
    e.invoke_static_this(BIND_CALLBACKS);
    e.load_this();
    e.load_arg(0);
    e.process_switch(keys, new ProcessSwitchCallback() {

        public void processCase(int key, Label end) {
            e.getfield(getCallbackField(key));
            e.goTo(end);
        }

        public void processDefault() {
            // stack height
            e.pop();
            e.aconst_null();
        }
    });
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) ProcessSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback) Label(org.powermock.api.mockito.repackaged.asm.Label)

Example 32 with Label

use of org.powermock.api.mockito.repackaged.asm.Label in project powermock by powermock.

the class MethodInterceptorGenerator method generateFindProxy.

public void generateFindProxy(ClassEmitter ce, final Map sigMap) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FIND_PROXY, null);
    e.load_arg(0);
    e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
    ObjectSwitchCallback callback = new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            e.getfield((String) sigMap.get(key));
            e.return_value();
        }

        public void processDefault() {
            e.aconst_null();
            e.return_value();
        }
    };
    EmitUtils.string_switch(e, (String[]) sigMap.keySet().toArray(new String[0]), Constants.SWITCH_STYLE_HASH, callback);
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 33 with Label

use of org.powermock.api.mockito.repackaged.asm.Label in project powermock by powermock.

the class EmitUtils method member_switch_helper.

private static void member_switch_helper(final CodeEmitter e, List members, final ObjectSwitchCallback callback, boolean useName) {
    try {
        final Map cache = new HashMap();
        final ParameterTyper cached = new ParameterTyper() {

            public Type[] getParameterTypes(MethodInfo member) {
                Type[] types = (Type[]) cache.get(member);
                if (types == null) {
                    cache.put(member, types = member.getSignature().getArgumentTypes());
                }
                return types;
            }
        };
        final Label def = e.make_label();
        final Label end = e.make_label();
        if (useName) {
            e.swap();
            final Map buckets = CollectionUtils.bucket(members, new Transformer() {

                public Object transform(Object value) {
                    return ((MethodInfo) value).getSignature().getName();
                }
            });
            String[] names = (String[]) buckets.keySet().toArray(new String[buckets.size()]);
            EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

                public void processCase(Object key, Label dontUseEnd) throws Exception {
                    member_helper_size(e, (List) buckets.get(key), callback, cached, def, end);
                }

                public void processDefault() throws Exception {
                    e.goTo(def);
                }
            });
        } else {
            member_helper_size(e, members, callback, cached, def, end);
        }
        e.mark(def);
        e.pop();
        callback.processDefault();
        e.mark(end);
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new CodeGenerationException(ex);
    }
}
Also used : HashMap(java.util.HashMap) Label(org.powermock.api.mockito.repackaged.asm.Label) Type(org.powermock.api.mockito.repackaged.asm.Type) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with Label

use of org.powermock.api.mockito.repackaged.asm.Label in project powermock by powermock.

the class EmitUtils method string_switch_hash.

private static void string_switch_hash(final CodeEmitter e, final String[] strings, final ObjectSwitchCallback callback, final boolean skipEquals) throws Exception {
    final Map buckets = CollectionUtils.bucket(Arrays.asList(strings), new Transformer() {

        public Object transform(Object value) {
            return new Integer(value.hashCode());
        }
    });
    final Label def = e.make_label();
    final Label end = e.make_label();
    e.dup();
    e.invoke_virtual(Constants.TYPE_OBJECT, HASH_CODE);
    e.process_switch(getSwitchKeys(buckets), new ProcessSwitchCallback() {

        public void processCase(int key, Label ignore_end) throws Exception {
            List bucket = (List) buckets.get(new Integer(key));
            Label next = null;
            if (skipEquals && bucket.size() == 1) {
                if (skipEquals)
                    e.pop();
                callback.processCase(bucket.get(0), end);
            } else {
                for (Iterator it = bucket.iterator(); it.hasNext(); ) {
                    String string = (String) it.next();
                    if (next != null) {
                        e.mark(next);
                    }
                    if (it.hasNext()) {
                        e.dup();
                    }
                    e.push(string);
                    e.invoke_virtual(Constants.TYPE_OBJECT, EQUALS);
                    if (it.hasNext()) {
                        e.if_jump(e.EQ, next = e.make_label());
                        e.pop();
                    } else {
                        e.if_jump(e.EQ, def);
                    }
                    callback.processCase(string, end);
                }
            }
        }

        public void processDefault() {
            e.pop();
        }
    });
    e.mark(def);
    callback.processDefault();
    e.mark(end);
}
Also used : BigInteger(java.math.BigInteger) Label(org.powermock.api.mockito.repackaged.asm.Label) Iterator(java.util.Iterator) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with Label

use of org.powermock.api.mockito.repackaged.asm.Label in project powermock by powermock.

the class BeanMapEmitter method generatePut.

private void generatePut(Class type, final Map setters) {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, BEAN_MAP_PUT, null);
    e.load_arg(0);
    e.checkcast(Type.getType(type));
    e.load_arg(1);
    e.checkcast(Constants.TYPE_STRING);
    EmitUtils.string_switch(e, getNames(setters), Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            PropertyDescriptor pd = (PropertyDescriptor) setters.get(key);
            if (pd.getReadMethod() == null) {
                e.aconst_null();
            } else {
                MethodInfo read = ReflectUtils.getMethodInfo(pd.getReadMethod());
                e.dup();
                e.invoke(read);
                e.box(read.getSignature().getReturnType());
            }
            // move old value behind bean
            e.swap();
            // new value
            e.load_arg(2);
            MethodInfo write = ReflectUtils.getMethodInfo(pd.getWriteMethod());
            e.unbox(write.getSignature().getArgumentTypes()[0]);
            e.invoke(write);
            e.return_value();
        }

        public void processDefault() {
        // fall-through
        }
    });
    e.aconst_null();
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) PropertyDescriptor(java.beans.PropertyDescriptor) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Aggregations

Label (org.powermock.api.mockito.repackaged.asm.Label)36 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)17 Type (org.powermock.api.mockito.repackaged.asm.Type)9 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)8 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)7 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)5 BigInteger (java.math.BigInteger)4 Iterator (java.util.Iterator)4 PropertyDescriptor (java.beans.PropertyDescriptor)3 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BitSet (java.util.BitSet)1