日期:2014-05-18 浏览次数:20872 次
package com.essp.uas.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public abstract class ProxyFactory<T> implements InvocationHandler {
private T target;
public ProxyFactory(T target) {
super();
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object o = null;
try {
before(args);
o = method.invoke(target,args);
after(method.getName(),args);
} catch (Exception e) {
onException(e);
e.printStackTrace();
}
return o;
}
@SuppressWarnings("unchecked")
public T createProxy() {
Class<T> cls = (Class<T>) target.getClass();
if (cls.getInterfaces() != null)
return (T) Proxy.newProxyInstance(cls.getClassLoader(), cls
.getInterfaces(), this);
return target;
}
public abstract void onException(Exception ex);
public abstract void before(Object[] args);
public abstract void after(String methodName,Object[] args);
}