Search in sources :

Example 6 with Type

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

the class SimpleVerifier method isSubTypeOf.

protected boolean isSubTypeOf(final Value value, final Value expected) {
    Type expectedType = ((BasicValue) expected).getType();
    Type type = ((BasicValue) value).getType();
    switch(expectedType.getSort()) {
        case Type.INT:
        case Type.FLOAT:
        case Type.LONG:
        case Type.DOUBLE:
            return type == expectedType;
        case Type.ARRAY:
        case Type.OBJECT:
            if ("Lnull;".equals(type.getDescriptor())) {
                return true;
            } else if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
                return isAssignableFrom(expectedType, type);
            } else {
                return false;
            }
        default:
            throw new Error("Internal error");
    }
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 7 with Type

use of org.powermock.api.mockito.repackaged.asm.Type 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 8 with Type

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

the class BeanGenerator method generateClass.

public void generateClass(ClassVisitor v) throws Exception {
    int size = props.size();
    String[] names = (String[]) props.keySet().toArray(new String[size]);
    Type[] types = new Type[size];
    for (int i = 0; i < size; i++) {
        types[i] = (Type) props.get(names[i]);
    }
    ClassEmitter ce = new ClassEmitter(v);
    ce.begin_class(Constants.V1_2, Constants.ACC_PUBLIC, getClassName(), superclass != null ? Type.getType(superclass) : Constants.TYPE_OBJECT, null, null);
    EmitUtils.null_constructor(ce);
    EmitUtils.add_properties(ce, names, types);
    ce.end_class();
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type) ClassEmitter(org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter)

Example 9 with Type

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

the class CodeEmitter method box.

/**
     * If the argument is a primitive class, replaces the primitive value
     * on the top of the stack with the wrapped (Object) equivalent. For
     * example, char -> Character.
     * If the class is Void, a null is pushed onto the stack instead.
     * @param type the class indicating the current type of the top stack value
     */
public void box(Type type) {
    if (TypeUtils.isPrimitive(type)) {
        if (type == Type.VOID_TYPE) {
            aconst_null();
        } else {
            Type boxed = TypeUtils.getBoxedType(type);
            new_instance(boxed);
            if (type.getSize() == 2) {
                // Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
                dup_x2();
                dup_x2();
                pop();
            } else {
                // p -> po -> opo -> oop -> o
                dup_x1();
                swap();
            }
            invoke_constructor(boxed, new Signature(Constants.CONSTRUCTOR_NAME, Type.VOID_TYPE, new Type[] { type }));
        }
    }
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 10 with Type

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

the class EmitUtils method hash_array.

private static void hash_array(final CodeEmitter e, Type type, final int multiplier, final Customizer customizer) {
    Label skip = e.make_label();
    Label end = e.make_label();
    e.dup();
    e.ifnull(skip);
    EmitUtils.process_array(e, type, new ProcessArrayCallback() {

        public void processElement(Type type) {
            hash_code(e, type, multiplier, customizer);
        }
    });
    e.goTo(end);
    e.mark(skip);
    e.pop();
    e.mark(end);
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type) Label(org.powermock.api.mockito.repackaged.asm.Label)

Aggregations

Type (org.powermock.api.mockito.repackaged.asm.Type)29 Label (org.powermock.api.mockito.repackaged.asm.Label)9 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)9 List (java.util.List)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)3 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)3 TryCatchBlockNode (org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode)2 ClassEmitter (org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter)2 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)2 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)2 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 AbstractInsnNode (org.powermock.api.mockito.repackaged.asm.tree.AbstractInsnNode)1 ClassNode (org.powermock.api.mockito.repackaged.asm.tree.ClassNode)1 IincInsnNode (org.powermock.api.mockito.repackaged.asm.tree.IincInsnNode)1 InsnList (org.powermock.api.mockito.repackaged.asm.tree.InsnList)1 JumpInsnNode (org.powermock.api.mockito.repackaged.asm.tree.JumpInsnNode)1