org.springframework.cglib.proxy.MethodInterceptor Java Examples

The following examples show how to use org.springframework.cglib.proxy.MethodInterceptor. 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: EnhancerUtil.java    From specification-arg-resolver with 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 File: DubboUtils.java    From java-master with 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 File: HttpClientProxyConfig.java    From api-layer with 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 File: MockHelper.java    From COLA with 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 File: LazyUtil.java    From spring-cloud-gcp with 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 File: MockHelper.java    From COLA with 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;
}