com.github.andyshao.proxy.CglibProxyFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Useful-Tools Show documentation
Show all versions of Useful-Tools Show documentation
Some thing about the useful tools of the JDK 1.8.
The newest version!
package com.github.andyshao.proxy;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
/**
*
* Title:the proxy factory of cglib
* Descript:
* Copyright: Copryright(c) Mar 17, 2014
* Encoding:UNIX UTF-8
*
* @author Andy.Shao
*
* @param the type of target
*/
public abstract class CglibProxyFactory implements ProxyFactory {
/**
*
* Title:
* Descript:
* Copyright: Copryright(c) Mar 17, 2014
* Encoding:UNIX UTF-8
*
* @author Andy.Shao
*
*/
public class DefaultInvocationHandler implements InvocationHandler {
private final T proxied;
public DefaultInvocationHandler(T proxied) {
this.proxied = proxied;
}
@Override
public Object invoke(Object obj , Method method , Object[] args) throws Throwable {
if (CglibProxyFactory.this.proxyMethods(this.proxied, method, args)) {
return CglibProxyFactory.this.invoke(this.proxied , method , args);
}
return method.invoke(this.proxied , args);
}
}
@Override
public T apply(T target) {
return this.getProxy(target , new DefaultInvocationHandler(target));
}
@SuppressWarnings("unchecked")
public T getProxy(T target , InvocationHandler invocationHandler) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setInterfaces(ProxyFactory.allInterfaces(target));
enhancer.setCallback(invocationHandler);
return (T) enhancer.create();
}
/**
* when the method which will be invoke should be proxy.
* this method will be run.
*
* @param target the target which will be proxy
* @param method the method which will be invoke
* @param args the args of method
* @return the answer of method
* @throws Throwable andy exception when run this method
*/
protected abstract Object invoke(T target , Method method , Object[] args) throws Throwable;
/**
* the methods which will be proxied
*
* @param target the target which will be proxy
* @param method the method which will be invoke
* @param args the args of method
* @return the methods collection
*/
protected abstract boolean proxyMethods(T target , Method method , Object[] args);
}