Java Code Examples for org.springframework.cglib.proxy.MethodInterceptor

The following examples show how to use org.springframework.cglib.proxy.MethodInterceptor. 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: specification-arg-resolver   Source File: EnhancerUtil.java    License: Apache License 2.0 6 votes vote down vote up
@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 2
Source Project: java-master   Source 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 3
Source Project: api-layer   Source File: HttpClientProxyConfig.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@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 4
Source Project: COLA   Source File: MockHelper.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Class createMockClass(Class clazz){
    Enhancer enhancer = new Enhancer();
    enhancer.setUseFactory(true);
    enhancer.setNamingPolicy(ColaNamingPolicy.INSTANCE);
    enhancer.setSerialVersionUID(42L);
    enhancer.setSuperclass(clazz);
    enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class});
    return enhancer.createClass();
}
 
Example 5
Source Project: spring-cloud-gcp   Source File: LazyUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static Class<?> getEnhancedTypeFor(Class<?> type) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(type);
	enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);

	return enhancer.createClass();
}
 
Example 6
Source Project: COLA   Source File: MockHelper.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Object createMockFor(Class clazz, MethodInterceptor interceptor){
    Class proxyCls = createMockClass(clazz);
    Factory proxy = (Factory)newInstance(proxyCls);
    proxy.setCallbacks(new Callback[] {interceptor});
    return proxy;
}