Java Code Examples for net.sf.cglib.proxy.Enhancer#setCallback()

The following examples show how to use net.sf.cglib.proxy.Enhancer#setCallback() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AopUtils.java    From nuls with MIT License 5 votes vote down vote up
public static final <T> T createProxy(Class<T> clazz,MethodInterceptor interceptor) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(new NulsMethodInterceptor(interceptor));

    return (T) enhancer.create();
}
 
Example 2
Source File: FixtureFactory.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
protected <T extends InteractionAwareFixture> T createFirst(Callback callback, Class<T> clazz, Class<?>[] constructorTypes, Object[] constructorArgs) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(callback);

    T result;
    if (constructorArgs != null && constructorArgs.length > 0) {
        result = (T) enhancer.create(constructorTypes, constructorArgs);
    } else {
        result = (T) enhancer.create();
    }
    return result;
}
 
Example 3
Source File: DynamicProxyPerfClient.java    From JavaDesignPattern with Apache License 2.0 5 votes vote down vote up
private static void testCglibExecution() {
  MethodInterceptor methodInterceptor = new SubjectInterceptor();
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(ConcreteSubject.class);
  enhancer.setCallback(methodInterceptor);
  ISubject subject = (ISubject) enhancer.create();
  long start = System.currentTimeMillis();
  for (int i = 0; i < execution; i++) {
    subject.action();
  }
  long stop = System.currentTimeMillis();
  LOG.info("cglib execution time : {} ms", stop - start);
}
 
Example 4
Source File: ProxyIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
    // given
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(PersonService.class);
    enhancer.setCallback((FixedValue) () -> "Hello Tom!");
    PersonService proxy = (PersonService) enhancer.create();

    // when
    String res = proxy.sayHello(null);

    // then
    assertEquals("Hello Tom!", res);
}
 
Example 5
Source File: BrpcProxy.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static <T> T getProxy(RpcClient rpcClient, Class clazz, NamingOptions namingOptions) {
    rpcClient.setServiceInterface(clazz, namingOptions);
    Enhancer en = new Enhancer();
    en.setSuperclass(clazz);
    en.setCallback(new BrpcProxy(rpcClient, clazz));
    return (T) en.create();
}
 
Example 6
Source File: MetaSpaceOOM.java    From Java-Interview with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    while (true){
        Enhancer  enhancer = new Enhancer() ;
        enhancer.setSuperclass(HeapOOM.class);
        enhancer.setUseCache(false) ;
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                return methodProxy.invoke(o,objects) ;
            }
        });
        enhancer.create() ;

    }
}
 
Example 7
Source File: CglibDynamicProxy.java    From AndroidFrame with Apache License 2.0 5 votes vote down vote up
/**
 * 创建代理对象
 * @param target 被代理的对象
 * @return
 */
public Object getProxyInstance(Object target){
    this.target = target;
    // 声明增强类实例
    Enhancer enhancer = new Enhancer();
    // 设置被代理类字节码,CGLIB根据字节码生成被代理类的子类
    enhancer.setSuperclass(this.target.getClass());
    // 设置要代理的拦截器,回调函数,即一个方法拦截   new MethodInterceptor()
    enhancer.setCallback(this);
    // 创建代理对象 实例
    return enhancer.create();
}
 
Example 8
Source File: CglibProxyHelper.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getProxyInternal(ClassLoader loader, Class<?>[] interfaces,
                                  final java.lang.reflect.InvocationHandler h) {

    Class<?> superClass = null;
    List<Class<?>> theInterfaces = new ArrayList<>();

    for (Class<?> c : interfaces) {
        if (!c.isInterface()) {
            if (superClass != null) {
                throw new IllegalArgumentException("Only a single superclass is supported");
            }
            superClass = c;
        } else {
            theInterfaces.add(c);
        }
    }
    if (superClass != null) {
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(loader);
        enhancer.setSuperclass(superClass);
        enhancer.setInterfaces(theInterfaces.toArray(new Class<?>[0]));
        enhancer.setCallback(new MethodInterceptor() {

            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
                throws Throwable {
                return h.invoke(obj, method, args);
            }

        });
        return enhancer.create();
    }
    return super.getProxyInternal(loader, interfaces, h);
}
 
Example 9
Source File: SpringLiteContext.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * 使用动态代理的方式创建对象的实例
 * Create an instance of the object using a dynamic proxy.
 */
private static Object createProxy(Class clazz, MethodInterceptor interceptor) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(interceptor);
    return enhancer.create();
}
 
Example 10
Source File: CgLibProxyClient.java    From JavaDesignPattern with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  MethodInterceptor methodInterceptor = new SubjectInterceptor();
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(ConcreteSubject.class);
  enhancer.setCallback(methodInterceptor);
  ISubject subject = (ISubject)enhancer.create();
  subject.action();
}
 
Example 11
Source File: CglibProxy.java    From sisyphus with Apache License 2.0 5 votes vote down vote up
@Override
public Object proxy() {
    Enhancer enhancer = new Enhancer();
    //目标对象类
    enhancer.setSuperclass(target.getClass());
    enhancer.setCallback(this);
    //通过字节码技术创建目标对象类的子类实例作为代理
    return enhancer.create();
}
 
Example 12
Source File: ProxyFactory.java    From Summer with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T getProxyInstance(Object target, ProxyMethodInterceptor interceptor) {
	ProxyFactory pf = new ProxyFactory(target, interceptor);
	Enhancer en = new Enhancer();
	en.setSuperclass(target.getClass());
	en.setCallback(pf);
	return (T)en.create();
}
 
Example 13
Source File: TransactionInterceptor.java    From JavaBase with MIT License 5 votes vote down vote up
public Object createProxy() {
  Enhancer enhancer = new Enhancer();
  //设置代理类为目标类的子类
  enhancer.setSuperclass(this.target.getClass());
  //设置拦截器为回调函数
  enhancer.setCallback(this);
  return enhancer.create();
}
 
Example 14
Source File: MarsBeanProxy.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 获取代理对象
 *
 * @param clazz bean的class
 * @return 对象
 */
public Object getProxy(Class<?> clazz) {
	enhancer = new Enhancer();
	// 设置需要创建子类的类
	enhancer.setSuperclass(clazz);
	enhancer.setCallback(this);
	// 通过字节码技术动态创建子类实例
	return enhancer.create();
}
 
Example 15
Source File: CglibProxy.java    From retry with Apache License 2.0 5 votes vote down vote up
/**
 * 获取代理类
 * @param clazz 类信息
 * @return 代理类结果
 */
public Object getProxy(Class clazz){
    Enhancer enhancer = new Enhancer();
    //目标对象类
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(this);
    //通过字节码技术创建目标对象类的子类实例作为代理
    return enhancer.create();
}
 
Example 16
Source File: SecureCoreAdminHandlerTest.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private CoreContainer getZkAwareCoreContainer(final CoreContainer cc) {
  Enhancer e = new Enhancer();
  e.setClassLoader(cc.getClass().getClassLoader());
  e.setSuperclass(CoreContainer.class);
  e.setCallback(new MethodInterceptor() {
    public Object intercept(Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {
      if (method.getName().equals("isZooKeeperAware")) {
        return Boolean.TRUE;
      }
      return method.invoke(cc, args);
    }
  });
  return (CoreContainer)e.create();
}
 
Example 17
Source File: Cglib2AopProxy.java    From tiny-spring with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProxy() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(advised.getTargetSource().getTargetClass());
	enhancer.setInterfaces(advised.getTargetSource().getInterfaces());
	enhancer.setCallback(new DynamicAdvisedInterceptor(advised));
	Object enhanced = enhancer.create();
	return enhanced;
}
 
Example 18
Source File: RefreshableProxy.java    From config-toolkit with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public RefreshableProxy(final T target) {
	super();
	this.target = target;
	final Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(this.target.getClass());
	enhancer.setCallback(this);
	proxy = (T) enhancer.create();
}
 
Example 19
Source File: MonitorRenderFactory.java    From gecco with MIT License 5 votes vote down vote up
@Override
public JsonRender createJsonRender() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(JsonRender.class);
	enhancer.setCallback(new RenderMointorIntercetor());
	return (JsonRender)enhancer.create();
}
 
Example 20
Source File: AopUtils.java    From nuls with MIT License 4 votes vote down vote up
public static final <T> T createProxy(Class<T> clazz,Class[] paramsClass,Object[] params,MethodInterceptor interceptor) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(new NulsMethodInterceptor(interceptor));
    return (T) enhancer.create(paramsClass,params);
}