Search in sources :

Example 6 with Label

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

the class FieldProviderTransformer method setField.

private void setField(String[] names) throws Exception {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, PROVIDER_SET, null);
    e.load_this();
    e.load_arg(1);
    e.load_arg(0);
    EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            Type type = (Type) fields.get(key);
            e.unbox(type);
            e.putfield((String) key);
            e.return_value();
        }

        public void processDefault() {
            e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field name");
        }
    });
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 7 with Label

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

the class FastClassEmitter method signatureSwitchHelper.

private void signatureSwitchHelper(final CodeEmitter e, final List signatures) {
    ObjectSwitchCallback callback = new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            // TODO: remove linear indexOf
            e.push(signatures.indexOf(key));
            e.return_value();
        }

        public void processDefault() {
            e.push(-1);
            e.return_value();
        }
    };
    EmitUtils.string_switch(e, (String[]) signatures.toArray(new String[signatures.size()]), Constants.SWITCH_STYLE_HASH, callback);
}
Also used : Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 8 with Label

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

the class EmitUtils method append_string_helper.

private static void append_string_helper(CodeEmitter e, Type type, ArrayDelimiters delims, Customizer customizer, ProcessArrayCallback callback) {
    Label skip = e.make_label();
    Label end = e.make_label();
    if (TypeUtils.isPrimitive(type)) {
        switch(type.getSort()) {
            case Type.INT:
            case Type.SHORT:
            case Type.BYTE:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_INT);
                break;
            case Type.DOUBLE:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_DOUBLE);
                break;
            case Type.FLOAT:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_FLOAT);
                break;
            case Type.LONG:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_LONG);
                break;
            case Type.BOOLEAN:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_BOOLEAN);
                break;
            case Type.CHAR:
                e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_CHAR);
                break;
        }
    } else if (TypeUtils.isArray(type)) {
        e.dup();
        e.ifnull(skip);
        e.swap();
        if (delims != null && delims.before != null && !"".equals(delims.before)) {
            e.push(delims.before);
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
            e.swap();
        }
        EmitUtils.process_array(e, type, callback);
        shrinkStringBuffer(e, 2);
        if (delims != null && delims.after != null && !"".equals(delims.after)) {
            e.push(delims.after);
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
        }
    } else {
        e.dup();
        e.ifnull(skip);
        if (customizer != null) {
            customizer.customize(e, type);
        }
        e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
        e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
    }
    e.goTo(end);
    e.mark(skip);
    e.pop();
    e.push("null");
    e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
    e.mark(end);
}
Also used : Label(org.powermock.api.mockito.repackaged.asm.Label)

Example 9 with Label

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

the class EmitUtils method member_helper_type.

private static void member_helper_type(final CodeEmitter e, List members, final ObjectSwitchCallback callback, final ParameterTyper typer, final Label def, final Label end, final BitSet checked) throws Exception {
    if (members.size() == 1) {
        MethodInfo member = (MethodInfo) members.get(0);
        Type[] types = typer.getParameterTypes(member);
        // need to check classes that have not already been checked via switches
        for (int i = 0; i < types.length; i++) {
            if (checked == null || !checked.get(i)) {
                e.dup();
                e.aaload(i);
                e.invoke_virtual(Constants.TYPE_CLASS, GET_NAME);
                e.push(TypeUtils.emulateClassGetName(types[i]));
                e.invoke_virtual(Constants.TYPE_OBJECT, EQUALS);
                e.if_jump(e.EQ, def);
            }
        }
        e.pop();
        callback.processCase(member, end);
    } else {
        // choose the index that has the best chance of uniquely identifying member
        Type[] example = typer.getParameterTypes((MethodInfo) members.get(0));
        Map buckets = null;
        int index = -1;
        for (int i = 0; i < example.length; i++) {
            final int j = i;
            Map test = CollectionUtils.bucket(members, new Transformer() {

                public Object transform(Object value) {
                    return TypeUtils.emulateClassGetName(typer.getParameterTypes((MethodInfo) value)[j]);
                }
            });
            if (buckets == null || test.size() > buckets.size()) {
                buckets = test;
                index = i;
            }
        }
        if (buckets == null || buckets.size() == 1) {
            // TODO: switch by returnType
            // must have two methods with same name, types, and different return types
            e.goTo(def);
        } else {
            checked.set(index);
            e.dup();
            e.aaload(index);
            e.invoke_virtual(Constants.TYPE_CLASS, GET_NAME);
            final Map fbuckets = buckets;
            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_type(e, (List) fbuckets.get(key), callback, typer, def, end, checked);
                }

                public void processDefault() throws Exception {
                    e.goTo(def);
                }
            });
        }
    }
}
Also used : 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 10 with Label

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

the class EmitUtils method nullcmp.

/**
     * If both objects on the top of the stack are non-null, does nothing.
     * If one is null, or both are null, both are popped off and execution
     * branches to the respective label.
     * @param oneNull label to branch to if only one of the objects is null
     * @param bothNull label to branch to if both of the objects are null
     */
private static void nullcmp(CodeEmitter e, Label oneNull, Label bothNull) {
    e.dup2();
    Label nonNull = e.make_label();
    Label oneNullHelper = e.make_label();
    Label end = e.make_label();
    e.ifnonnull(nonNull);
    e.ifnonnull(oneNullHelper);
    e.pop2();
    e.goTo(bothNull);
    e.mark(nonNull);
    e.ifnull(oneNullHelper);
    e.goTo(end);
    e.mark(oneNullHelper);
    e.pop2();
    e.goTo(oneNull);
    e.mark(end);
}
Also used : Label(org.powermock.api.mockito.repackaged.asm.Label)

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