Java Code Examples for org.springframework.cglib.proxy.Enhancer#setCallback()

The following examples show how to use org.springframework.cglib.proxy.Enhancer#setCallback() . These examples are extracted from open source projects. 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 Project: syndesis   File: Recordings.java    License: Apache License 2.0 6 votes vote down vote up
static public <T> T recorder(Object object, Class<T> as) {
    if (as.isInterface()) {
        // If it's just an interface, use standard java reflect proxying
        return as.cast(Proxy.newProxyInstance(as.getClassLoader(), new Class<?>[] { as }, new RecordingInvocationHandler(object)));
    }

    // If it's a class then use gclib to implement a subclass to implement proxying
    RecordingInvocationHandler ih = new RecordingInvocationHandler(object);
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(as);
    enhancer.setInterfaces(new Class<?>[]{RecordingProxy.class});
    enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            return ih.invoke(o, method, objects);
        }
    });
    return as.cast(enhancer.create());
}
 
Example 2
@SuppressWarnings("unchecked")
static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) {
   	Enhancer enhancer = new Enhancer();
	enhancer.setInterfaces(new Class[] { iface });
	enhancer.setCallback(new MethodInterceptor() {
           @Override
           public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
           	if ("toString".equals(method.getName())) {
           		return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]";
           	}
           	return proxy.invoke(targetSpec, args);
           }
       });
	
	return (T) enhancer.create();
   }
 
Example 3
/**
 * 读取 xml 信息为 java Bean
 *
 * @param doc   xml element
 * @param clazz bean Class
 * @param <T>   泛型
 * @return 对象
 */
@SuppressWarnings("unchecked")
public static <T> T readValue(final Element doc, final Class<T> clazz) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(clazz);
	enhancer.setUseCache(true);
	enhancer.setCallback(new CssQueryMethodInterceptor(clazz, doc));
	return (T) enhancer.create();
}
 
Example 4
Source Project: java-master   File: DubboUtils.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ALL")
public static <T> T getService(Class<T> dubboServiceClass, String group, String version, String host) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(dubboServiceClass);
    enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
        ReferenceConfig<GenericService> reference = getReferenceConfig(dubboServiceClass, group, version, host);
        GenericService genericService = reference.get();
        Object result = genericService.$invoke(method.getName(), getMethodParamType(method), args);
        String resJsonStr = OMUtils.objectMapper().writeValueAsString(result);
        return OMUtils.objectMapper().readValue(resJsonStr, method.getReturnType());
    });
    Object service = enhancer.create();
    return (T) service;
}
 
Example 5
Source Project: DesignPatterns   File: ProxyFactory.java    License: Apache License 2.0 5 votes vote down vote up
public Object getProxyInstance(){
    //1.工具类
    Enhancer en = new Enhancer();
    //2.设置父类
    en.setSuperclass(target.getClass());
    //3.设置回调函数
    en.setCallback(this);
    //4.创建子类(代理对象)
    return en.create();
}
 
Example 6
@Bean
public CloseableHttpClient httpClientProxy() {
    Enhancer e = new Enhancer();
    e.setSuperclass(CloseableHttpClient.class);
    e.setCallback((MethodInterceptor) (o, method, objects, methodProxy) ->
        {
            if (method.getName().equals("execute") && objects.length > 0 && objects[0] instanceof HttpRequest) {
                serviceAuthenticationDecorator.process((HttpRequest) objects[0]);
            }
            return method.invoke(clientChooser.chooseClient(), objects);
        }
    );
    return (CloseableHttpClient) e.create();
}
 
Example 7
@Override
@SuppressWarnings("unchecked")
public T create(Throwable cause) {
	final Class<T> targetType = target.type();
	final String targetName = target.name();
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(targetType);
	enhancer.setUseCache(true);
	enhancer.setCallback(new BladeFeignFallback<>(targetType, targetName, cause));
	return (T) enhancer.create();
}
 
Example 8
private A createEnhancer(Callback cb) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(AImpl.class);

    enhancer.setCallback(cb);
    final A a = (A) enhancer.create();
    return a;
}
 
Example 9
public Object getInstance() {
	Enhancer en = new Enhancer();
	en.setSuperclass(queryWrapper.getClass());
	en.setCallback(this);
	return en.create();
}