/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param callback the callback to use for all methods */ public static Object create(Class type, Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(type); e.setCallback(callback); return e.create(); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param interfaces array of interfaces to implement, or null * @param callback the callback to use for all methods */ public static Object create(Class superclass, Class interfaces[], Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(superclass); e.setInterfaces(interfaces); e.setCallback(callback); return e.create(); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param interfaces array of interfaces to implement, or null * @param filter the callback filter to use when generating a new class * @param callbacks callback implementations to use for the enhanced object */ public static Object create(Class superclass, Class[] interfaces, CallbackFilter filter, Callback[] callbacks) { Enhancer e = new Enhancer(); e.setSuperclass(superclass); e.setInterfaces(interfaces); e.setCallbackFilter(filter); e.setCallbacks(callbacks); return e.create(); }
@SuppressWarnings("unchecked") public <T> T createProxy(Class<?> targetClass, MethodInterceptor interceptor) { // get all the interfaces (is there an easier way?) Set<Class<?>> interfaces = new HashSet<Class<?>>(); Class<?> clazz = targetClass; while (true) { for (Class<?> interfaze : clazz.getInterfaces()) { interfaces.add(interfaze); } if (clazz.getSuperclass() == null) { break; } clazz = clazz.getSuperclass(); } interfaces.add(InterfaceOnlyProxy.class); return (T) Enhancer.create(Object.class, interfaces.toArray(new Class<?>[interfaces.size()]), interceptor); } }
@SuppressWarnings("unchecked") public <T> T createProxy(Class<?> targetClass, MethodInterceptor interceptor) { // get all the interfaces (is there an easier way?) Set<Class<?>> interfaces = new HashSet<Class<?>>(); Class<?> clazz = targetClass; while (true) { for (Class<?> interfaze : clazz.getInterfaces()) { interfaces.add(interfaze); } if (clazz.getSuperclass() == null) { break; } clazz = clazz.getSuperclass(); } interfaces.add(InterfaceOnlyProxy.class); return (T) Enhancer.create(Object.class, interfaces.toArray(new Class<?>[interfaces.size()]), interceptor); } }
public Object getProxy(Class clazz) { //设置需要创建子类的类 enhancer.setSuperclass(clazz); enhancer.setCallbacks(new Callback[]{this, org.mockito.cglib.proxy.NoOp.INSTANCE}); //通过字节码技术动态创建子类实例 enhancer.setCallbackFilter((method) -> { if (Modifier.isPrivate(method.getModifiers())) { return 1; } return 0; }); return enhancer.create(); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param callback the callback to use for all methods */ public static Object create(Class type, Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(type); e.setCallback(callback); return e.create(); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param interfaces array of interfaces to implement, or null * @param callback the callback to use for all methods */ public static Object create(Class superclass, Class interfaces[], Callback callback) { Enhancer e = new Enhancer(); e.setSuperclass(superclass); e.setInterfaces(interfaces); e.setCallback(callback); return e.create(); }
/** * Helper method to create an intercepted object. * For finer control over the generated instance, use a new instance of <code>Enhancer</code> * instead of this static method. * @param type class to extend or interface to implement * @param interfaces array of interfaces to implement, or null * @param filter the callback filter to use when generating a new class * @param callbacks callback implementations to use for the enhanced object */ public static Object create(Class superclass, Class[] interfaces, CallbackFilter filter, Callback[] callbacks) { Enhancer e = new Enhancer(); e.setSuperclass(superclass); e.setInterfaces(interfaces); e.setCallbackFilter(filter); e.setCallbacks(callbacks); return e.create(); }