Search in sources :

Example 21 with Type

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

the class InterceptFieldTransformer method begin_method.

public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
    return new CodeEmitter(super.begin_method(access, sig, exceptions)) {

        public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            Type towner = TypeUtils.fromInternalName(owner);
            switch(opcode) {
                case Constants.GETFIELD:
                    if (filter.acceptRead(towner, name)) {
                        helper(towner, readMethodSig(name, desc));
                        return;
                    }
                    break;
                case Constants.PUTFIELD:
                    if (filter.acceptWrite(towner, name)) {
                        helper(towner, writeMethodSig(name, desc));
                        return;
                    }
                    break;
            }
            super.visitFieldInsn(opcode, owner, name, desc);
        }

        private void helper(Type owner, Signature sig) {
            invoke_virtual(owner, sig);
        }
    };
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature)

Example 22 with Type

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

the class ParallelSorterEmitter method generateConstructor.

private void generateConstructor(Object[] arrays) {
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, CSTRUCT_OBJECT_ARRAY, null);
    e.load_this();
    e.super_invoke_constructor();
    e.load_this();
    e.load_arg(0);
    e.super_putfield("a", Constants.TYPE_OBJECT_ARRAY);
    for (int i = 0; i < arrays.length; i++) {
        Type type = Type.getType(arrays[i].getClass());
        declare_field(Constants.ACC_PRIVATE, getFieldName(i), type, null);
        e.load_this();
        e.load_arg(0);
        e.push(i);
        e.aaload();
        e.checkcast(type);
        e.putfield(getFieldName(i));
    }
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 23 with Type

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

the class EmitUtils method process_array.

/**
     * Process an array on the stack. Assumes the top item on the stack
     * is an array of the specified type. For each element in the array,
     * puts the element on the stack and triggers the callback.
     * @param type the type of the array (type.isArray() must be true)
     * @param callback the callback triggered for each element
     */
public static void process_array(CodeEmitter e, Type type, ProcessArrayCallback callback) {
    Type componentType = TypeUtils.getComponentType(type);
    Local array = e.make_local();
    Local loopvar = e.make_local(Type.INT_TYPE);
    Label loopbody = e.make_label();
    Label checkloop = e.make_label();
    e.store_local(array);
    e.push(0);
    e.store_local(loopvar);
    e.goTo(checkloop);
    e.mark(loopbody);
    e.load_local(array);
    e.load_local(loopvar);
    e.array_load(componentType);
    callback.processElement(componentType);
    e.iinc(loopvar, 1);
    e.mark(checkloop);
    e.load_local(loopvar);
    e.load_local(array);
    e.arraylength();
    e.if_icmp(e.LT, loopbody);
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type) Label(org.powermock.api.mockito.repackaged.asm.Label)

Example 24 with Type

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

the class EmitUtils method process_arrays.

/**
     * Process two arrays on the stack in parallel. Assumes the top two items on the stack
     * are arrays of the specified class. The arrays must be the same length. For each pair
     * of elements in the arrays, puts the pair on the stack and triggers the callback.
     * @param type the type of the arrays (type.isArray() must be true)
     * @param callback the callback triggered for each pair of elements
     */
public static void process_arrays(CodeEmitter e, Type type, ProcessArrayCallback callback) {
    Type componentType = TypeUtils.getComponentType(type);
    Local array1 = e.make_local();
    Local array2 = e.make_local();
    Local loopvar = e.make_local(Type.INT_TYPE);
    Label loopbody = e.make_label();
    Label checkloop = e.make_label();
    e.store_local(array1);
    e.store_local(array2);
    e.push(0);
    e.store_local(loopvar);
    e.goTo(checkloop);
    e.mark(loopbody);
    e.load_local(array1);
    e.load_local(loopvar);
    e.array_load(componentType);
    e.load_local(array2);
    e.load_local(loopvar);
    e.array_load(componentType);
    callback.processElement(componentType);
    e.iinc(loopvar, 1);
    e.mark(checkloop);
    e.load_local(loopvar);
    e.load_local(array1);
    e.arraylength();
    e.if_icmp(e.LT, loopbody);
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type) Label(org.powermock.api.mockito.repackaged.asm.Label)

Example 25 with Type

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

the class InterfaceMaker method generateClass.

public void generateClass(ClassVisitor v) throws Exception {
    ClassEmitter ce = new ClassEmitter(v);
    ce.begin_class(Constants.V1_2, Constants.ACC_PUBLIC | Constants.ACC_INTERFACE, getClassName(), null, null, Constants.SOURCE_FILE);
    for (Iterator it = signatures.keySet().iterator(); it.hasNext(); ) {
        Signature sig = (Signature) it.next();
        Type[] exceptions = (Type[]) signatures.get(sig);
        ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_ABSTRACT, sig, exceptions).end_method();
    }
    ce.end_class();
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) ClassEmitter(org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter) Iterator(java.util.Iterator)

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