Search in sources :

Example 26 with CodeEmitter

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

the class Enhancer method emitNewInstanceMultiarg.

private void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, MULTIARG_NEW_INSTANCE, null);
    e.load_arg(2);
    e.invoke_static_this(SET_THREAD_CALLBACKS);
    e.new_instance_this();
    e.dup();
    e.load_arg(0);
    EmitUtils.constructor_switch(e, constructors, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            MethodInfo constructor = (MethodInfo) key;
            Type[] types = constructor.getSignature().getArgumentTypes();
            for (int i = 0; i < types.length; i++) {
                e.load_arg(1);
                e.push(i);
                e.aaload();
                e.unbox(types[i]);
            }
            e.invoke_constructor_this(constructor.getSignature());
            e.goTo(end);
        }

        public void processDefault() {
            e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Constructor not found");
        }
    });
    e.aconst_null();
    e.invoke_static_this(SET_THREAD_CALLBACKS);
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) 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)

Example 27 with CodeEmitter

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

the class BulkBeanEmitter method generateSet.

private void generateSet(final Class target, final Method[] setters) {
    // setPropertyValues
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SET_PROPERTY_VALUES, null);
    if (setters.length > 0) {
        Local index = e.make_local(Type.INT_TYPE);
        e.push(0);
        e.store_local(index);
        e.load_arg(0);
        e.checkcast(Type.getType(target));
        e.load_arg(1);
        Block handler = e.begin_block();
        int lastIndex = 0;
        for (int i = 0; i < setters.length; i++) {
            if (setters[i] != null) {
                MethodInfo setter = ReflectUtils.getMethodInfo(setters[i]);
                int diff = i - lastIndex;
                if (diff > 0) {
                    e.iinc(index, diff);
                    lastIndex = i;
                }
                e.dup2();
                e.aaload(i);
                e.unbox(setter.getSignature().getArgumentTypes()[0]);
                e.invoke(setter);
            }
        }
        handler.end();
        e.return_value();
        e.catch_exception(handler, Constants.TYPE_THROWABLE);
        e.new_instance(BULK_BEAN_EXCEPTION);
        e.dup_x1();
        e.swap();
        e.load_local(index);
        e.invoke_constructor(BULK_BEAN_EXCEPTION, CSTRUCT_EXCEPTION);
        e.athrow();
    } else {
        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) Block(org.powermock.api.mockito.repackaged.cglib.core.Block) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)

Example 28 with CodeEmitter

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

the class AccessFieldTransformer method declare_field.

public void declare_field(int access, final String name, Type type, Object value) {
    super.declare_field(access, name, type, value);
    String property = TypeUtils.upperFirst(callback.getPropertyName(getClassType(), name));
    if (property != null) {
        CodeEmitter e;
        e = begin_method(Constants.ACC_PUBLIC, new Signature("get" + property, type, Constants.TYPES_EMPTY), null);
        e.load_this();
        e.getfield(name);
        e.return_value();
        e.end_method();
        e = begin_method(Constants.ACC_PUBLIC, new Signature("set" + property, Type.VOID_TYPE, new Type[] { type }), null);
        e.load_this();
        e.load_arg(0);
        e.putfield(name);
        e.return_value();
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature)

Example 29 with CodeEmitter

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

the class AddDelegateTransformer method addDelegate.

private void addDelegate(Method m) {
    Method delegate;
    try {
        delegate = delegateImpl.getMethod(m.getName(), m.getParameterTypes());
        if (!delegate.getReturnType().getName().equals(m.getReturnType().getName())) {
            throw new IllegalArgumentException("Invalid delegate signature " + delegate);
        }
    } catch (NoSuchMethodException e) {
        throw new CodeGenerationException(e);
    }
    final Signature sig = ReflectUtils.getSignature(m);
    Type[] exceptions = TypeUtils.getTypes(m.getExceptionTypes());
    CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, sig, exceptions);
    e.load_this();
    e.getfield(DELEGATE);
    e.load_args();
    e.invoke_virtual(delegateType, sig);
    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) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Method(java.lang.reflect.Method)

Example 30 with CodeEmitter

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

the class AddStaticInitTransformer method init.

protected void init() {
    if (!TypeUtils.isInterface(getAccess())) {
        CodeEmitter e = getStaticHook();
        EmitUtils.load_class_this(e);
        e.invoke(info);
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)

Aggregations

CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)47 Label (org.powermock.api.mockito.repackaged.asm.Label)17 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)13 Type (org.powermock.api.mockito.repackaged.asm.Type)9 Iterator (java.util.Iterator)8 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)7 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)6 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)5 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)4 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)4 PropertyDescriptor (java.beans.PropertyDescriptor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 MethodInfoTransformer (org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer)2