Search in sources :

Example 1 with Label

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

the class LazyLoaderGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    Set indexes = new HashSet();
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        if (TypeUtils.isProtected(method.getModifiers())) {
        // ignore protected methods
        } else {
            int index = context.getIndex(method);
            indexes.add(new Integer(index));
            CodeEmitter e = context.beginMethod(ce, method);
            e.load_this();
            e.dup();
            e.invoke_virtual_this(loadMethod(index));
            e.checkcast(method.getClassInfo().getType());
            e.load_args();
            e.invoke(method);
            e.return_value();
            e.end_method();
        }
    }
    for (Iterator it = indexes.iterator(); it.hasNext(); ) {
        int index = ((Integer) it.next()).intValue();
        String delegate = "CGLIB$LAZY_LOADER_" + index;
        ce.declare_field(Constants.ACC_PRIVATE, delegate, Constants.TYPE_OBJECT, null);
        CodeEmitter e = ce.begin_method(Constants.ACC_PRIVATE | Constants.ACC_SYNCHRONIZED | Constants.ACC_FINAL, loadMethod(index), null);
        e.load_this();
        e.getfield(delegate);
        e.dup();
        Label end = e.make_label();
        e.ifnonnull(end);
        e.pop();
        e.load_this();
        context.emitCallback(e, index);
        e.invoke_interface(LAZY_LOADER, LOAD_OBJECT);
        e.dup_x1();
        e.putfield(delegate);
        e.mark(end);
        e.return_value();
        e.end_method();
    }
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) HashSet(java.util.HashSet) Set(java.util.Set) Iterator(java.util.Iterator) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) HashSet(java.util.HashSet)

Example 2 with Label

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

the class MethodInterceptorGenerator method generate.

public void generate(ClassEmitter ce, Context context, List methods) {
    Map sigMap = new HashMap();
    for (Iterator it = methods.iterator(); it.hasNext(); ) {
        MethodInfo method = (MethodInfo) it.next();
        Signature sig = method.getSignature();
        Signature impl = context.getImplSignature(method);
        String methodField = getMethodField(impl);
        String methodProxyField = getMethodProxyField(impl);
        sigMap.put(sig.toString(), methodProxyField);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodField, METHOD, null);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodProxyField, METHOD_PROXY, null);
        ce.declare_field(Constants.PRIVATE_FINAL_STATIC, EMPTY_ARGS_NAME, Constants.TYPE_OBJECT_ARRAY, null);
        CodeEmitter e;
        // access method
        e = ce.begin_method(Constants.ACC_FINAL, impl, method.getExceptionTypes());
        superHelper(e, method);
        e.return_value();
        e.end_method();
        // around method
        e = context.beginMethod(ce, method);
        Label nullInterceptor = e.make_label();
        context.emitCallback(e, context.getIndex(method));
        e.dup();
        e.ifnull(nullInterceptor);
        e.load_this();
        e.getfield(methodField);
        if (sig.getArgumentTypes().length == 0) {
            e.getfield(EMPTY_ARGS_NAME);
        } else {
            e.create_arg_array();
        }
        e.getfield(methodProxyField);
        e.invoke_interface(METHOD_INTERCEPTOR, INTERCEPT);
        e.unbox_or_zero(sig.getReturnType());
        e.return_value();
        e.mark(nullInterceptor);
        superHelper(e, method);
        e.return_value();
        e.end_method();
    }
    generateFindProxy(ce, sigMap);
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) HashMap(java.util.HashMap) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature) Iterator(java.util.Iterator) Label(org.powermock.api.mockito.repackaged.asm.Label) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Label

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

the class Enhancer method emitMethods.

private void emitMethods(final ClassEmitter ce, List methods, List actualMethods) {
    CallbackGenerator[] generators = CallbackInfo.getGenerators(callbackTypes);
    Map groups = new HashMap();
    final Map indexes = new HashMap();
    final Map originalModifiers = new HashMap();
    final Map positions = CollectionUtils.getIndexMap(methods);
    Iterator it1 = methods.iterator();
    Iterator it2 = (actualMethods != null) ? actualMethods.iterator() : null;
    while (it1.hasNext()) {
        MethodInfo method = (MethodInfo) it1.next();
        Method actualMethod = (it2 != null) ? (Method) it2.next() : null;
        int index = filter.accept(actualMethod, actualMethods);
        if (index >= callbackTypes.length) {
            throw new IllegalArgumentException("Callback filter returned an index that is too large: " + index);
        }
        originalModifiers.put(method, new Integer((actualMethod != null) ? actualMethod.getModifiers() : method.getModifiers()));
        indexes.put(method, new Integer(index));
        List group = (List) groups.get(generators[index]);
        if (group == null) {
            groups.put(generators[index], group = new ArrayList(methods.size()));
        }
        group.add(method);
    }
    Set seenGen = new HashSet();
    CodeEmitter se = ce.getStaticHook();
    se.new_instance(THREAD_LOCAL);
    se.dup();
    se.invoke_constructor(THREAD_LOCAL, CSTRUCT_NULL);
    se.putfield(THREAD_CALLBACKS_FIELD);
    final Object[] state = new Object[1];
    CallbackGenerator.Context context = new CallbackGenerator.Context() {

        public ClassLoader getClassLoader() {
            return Enhancer.this.getClassLoader();
        }

        public int getOriginalModifiers(MethodInfo method) {
            return ((Integer) originalModifiers.get(method)).intValue();
        }

        public int getIndex(MethodInfo method) {
            return ((Integer) indexes.get(method)).intValue();
        }

        public void emitCallback(CodeEmitter e, int index) {
            emitCurrentCallback(e, index);
        }

        public Signature getImplSignature(MethodInfo method) {
            return rename(method.getSignature(), ((Integer) positions.get(method)).intValue());
        }

        public CodeEmitter beginMethod(ClassEmitter ce, MethodInfo method) {
            CodeEmitter e = EmitUtils.begin_method(ce, method);
            if (!interceptDuringConstruction && !TypeUtils.isAbstract(method.getModifiers())) {
                Label constructed = e.make_label();
                e.load_this();
                e.getfield(CONSTRUCTED_FIELD);
                e.if_jump(e.NE, constructed);
                e.load_this();
                e.load_args();
                e.super_invoke();
                e.return_value();
                e.mark(constructed);
            }
            return e;
        }
    };
    for (int i = 0; i < callbackTypes.length; i++) {
        CallbackGenerator gen = generators[i];
        if (!seenGen.contains(gen)) {
            seenGen.add(gen);
            final List fmethods = (List) groups.get(gen);
            if (fmethods != null) {
                try {
                    gen.generate(ce, context, fmethods);
                    gen.generateStatic(se, context, fmethods);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new CodeGenerationException(x);
                }
            }
        }
    }
    se.return_value();
    se.end_method();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassEmitter(org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter) Label(org.powermock.api.mockito.repackaged.asm.Label) Method(java.lang.reflect.Method) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) Iterator(java.util.Iterator) MethodInfo(org.powermock.api.mockito.repackaged.cglib.core.MethodInfo) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with Label

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

the class Enhancer method emitSetCallback.

private void emitSetCallback(ClassEmitter ce, int[] keys) {
    final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACK, null);
    e.load_arg(0);
    e.process_switch(keys, new ProcessSwitchCallback() {

        public void processCase(int key, Label end) {
            e.load_this();
            e.load_arg(1);
            e.checkcast(callbackTypes[key]);
            e.putfield(getCallbackField(key));
            e.goTo(end);
        }

        public void processDefault() {
        // TODO: error?
        }
    });
    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 5 with Label

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

the class FieldProviderTransformer method getByIndex.

private void getByIndex(final String[] names, final int[] indexes) throws Exception {
    final CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, PROVIDER_GET_BY_INDEX, null);
    e.load_this();
    e.load_arg(0);
    e.process_switch(indexes, new ProcessSwitchCallback() {

        public void processCase(int key, Label end) throws Exception {
            Type type = (Type) fields.get(names[key]);
            e.getfield(names[key]);
            e.box(type);
            e.return_value();
        }

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

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