Search in sources :

Example 1 with Transformer

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

the class FastClassEmitter method emitIndexBySignature.

// TODO: support constructor indices ("<init>")
private void emitIndexBySignature(List methods) {
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SIGNATURE_GET_INDEX, null);
    List signatures = CollectionUtils.transform(methods, new Transformer() {

        public Object transform(Object obj) {
            return ReflectUtils.getSignature((Method) obj).toString();
        }
    });
    e.load_arg(0);
    e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
    signatureSwitchHelper(e, signatures);
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) MethodInfoTransformer(org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer) Transformer(org.powermock.api.mockito.repackaged.cglib.core.Transformer) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Transformer

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

the class FastClassEmitter method emitIndexByClassArray.

private void emitIndexByClassArray(List methods) {
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, METHOD_GET_INDEX, null);
    if (methods.size() > TOO_MANY_METHODS) {
        // hack for big classes
        List signatures = CollectionUtils.transform(methods, new Transformer() {

            public Object transform(Object obj) {
                String s = ReflectUtils.getSignature((Method) obj).toString();
                return s.substring(0, s.lastIndexOf(')') + 1);
            }
        });
        e.load_args();
        e.invoke_static(FAST_CLASS, GET_SIGNATURE_WITHOUT_RETURN_TYPE);
        signatureSwitchHelper(e, signatures);
    } else {
        e.load_args();
        List info = CollectionUtils.transform(methods, MethodInfoTransformer.getInstance());
        EmitUtils.method_switch(e, info, new GetIndexCallback(e, info));
    }
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) MethodInfoTransformer(org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer) Transformer(org.powermock.api.mockito.repackaged.cglib.core.Transformer) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Transformer

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

the class Enhancer method generateClass.

public void generateClass(ClassVisitor v) throws Exception {
    Class sc = (superclass == null) ? Object.class : superclass;
    if (TypeUtils.isFinal(sc.getModifiers()))
        throw new IllegalArgumentException("Cannot subclass final class " + sc);
    List constructors = new ArrayList(Arrays.asList(sc.getDeclaredConstructors()));
    filterConstructors(sc, constructors);
    // Order is very important: must add superclass, then
    // its superclass chain, then each interface and
    // its superinterfaces.
    List actualMethods = new ArrayList();
    List interfaceMethods = new ArrayList();
    final Set forcePublic = new HashSet();
    getMethods(sc, interfaces, actualMethods, interfaceMethods, forcePublic);
    List methods = CollectionUtils.transform(actualMethods, new Transformer() {

        public Object transform(Object value) {
            Method method = (Method) value;
            int modifiers = Constants.ACC_FINAL | (method.getModifiers() & ~Constants.ACC_ABSTRACT & ~Constants.ACC_NATIVE & ~Constants.ACC_SYNCHRONIZED);
            if (forcePublic.contains(MethodWrapper.create(method))) {
                modifiers = (modifiers & ~Constants.ACC_PROTECTED) | Constants.ACC_PUBLIC;
            }
            return ReflectUtils.getMethodInfo(method, modifiers);
        }
    });
    ClassEmitter e = new ClassEmitter(v);
    e.begin_class(Constants.V1_2, Constants.ACC_PUBLIC, getClassName(), Type.getType(sc), (useFactory ? TypeUtils.add(TypeUtils.getTypes(interfaces), FACTORY) : TypeUtils.getTypes(interfaces)), Constants.SOURCE_FILE);
    List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());
    e.declare_field(Constants.ACC_PRIVATE, BOUND_FIELD, Type.BOOLEAN_TYPE, null);
    if (!interceptDuringConstruction) {
        e.declare_field(Constants.ACC_PRIVATE, CONSTRUCTED_FIELD, Type.BOOLEAN_TYPE, null);
    }
    e.declare_field(Constants.PRIVATE_FINAL_STATIC, THREAD_CALLBACKS_FIELD, THREAD_LOCAL, null);
    e.declare_field(Constants.PRIVATE_FINAL_STATIC, STATIC_CALLBACKS_FIELD, CALLBACK_ARRAY, null);
    if (serialVersionUID != null) {
        e.declare_field(Constants.PRIVATE_FINAL_STATIC, Constants.SUID_FIELD_NAME, Type.LONG_TYPE, serialVersionUID);
    }
    for (int i = 0; i < callbackTypes.length; i++) {
        e.declare_field(Constants.ACC_PRIVATE, getCallbackField(i), callbackTypes[i], null);
    }
    emitMethods(e, methods, actualMethods);
    emitConstructors(e, constructorInfo);
    emitSetThreadCallbacks(e);
    emitSetStaticCallbacks(e);
    emitBindCallbacks(e);
    if (useFactory) {
        int[] keys = getCallbackKeys();
        emitNewInstanceCallbacks(e);
        emitNewInstanceCallback(e);
        emitNewInstanceMultiarg(e, constructorInfo);
        emitGetCallback(e, keys);
        emitSetCallback(e, keys);
        emitGetCallbacks(e);
        emitSetCallbacks(e);
    }
    e.end_class();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) MethodInfoTransformer(org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer) Transformer(org.powermock.api.mockito.repackaged.cglib.core.Transformer) ArrayList(java.util.ArrayList) ClassEmitter(org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 MethodInfoTransformer (org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer)3 Transformer (org.powermock.api.mockito.repackaged.cglib.core.Transformer)3 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)2 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ClassEmitter (org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter)1