import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class BaseProxy implements InvocationHandler{
protected Object target;
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public BaseProxy(){
}
public BaseProxy(Object obj){
this.target = obj;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO 自动生成方法存根
String methodName = method.getName();
this.doBefore(methodName);
Object object = method.invoke(target,args);
this.doAfter(methodName);
return object;
}
public void doBefore(){
}
public void doBefore(String methodName){
System.out.println("begin:"+methodName);
}
public void doAfter(){
}
public void doAfter(String methodName){
System.out.println("end:"+methodName);
}
public Object getObject(){
Class[] interfaces=target.getClass().getInterfaces();
if(interfaces==null || interfaces.length==0){
interfaces=new Class[1];
interfaces[0]=target.getClass();
}
return Proxy.newProxyInstance(target.getClass().getClassLoader(),interfaces,this);
}
protected InvocationHandler getHandler(Object obj){
return new BaseProxy(obj);
}
public Object getObject(Object obj){
Class[] interfaces=obj.getClass().getInterfaces();
if(interfaces==null || interfaces.length==0){
interfaces=new Class[1];
interfaces[0]=obj.getClass();
}
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),interfaces,this.getHandler(obj));
}
public static void main(String[] args) throws Throwable{
System.out.println("run");
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class BaseProxy implements InvocationHandler{
protected Object target;
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public BaseProxy(){
}
public BaseProxy(Object obj){
this.target = obj;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO 自动生成方法存根
String methodName = method.getName();
this.doBefore(methodName);
Object object = method.invoke(target,args);
this.doAfter(methodName);
return object;
}
public void doBefore(){
}
public void doBefore(String methodName){
System.out.println("begin:"+methodName);
}
public void doAfter(){
}
public void doAfter(String methodName){
System.out.println("end:"+methodName);
}
public Object getObject(){
Class[] interfaces=target.getClass().getInterfaces();
if(interfaces==null || interfaces.length==0){
interfaces=new Class[1];
interfaces[0]=target.getClass();
}
return Proxy.newProxyInstance(target.getClass().getClassLoader(),interfaces,this);
}
protected InvocationHandler getHandler(Object obj){
return new BaseProxy(obj);
}
public Object getObject(Object obj){
Class[] interfaces=obj.getClass().getInterfaces();
if(interfaces==null || interfaces.length==0){
interfaces=new Class[1];
interfaces[0]=obj.getClass();
}
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),interfaces,this.getHandler(obj));
}
public static void main(String[] args) throws Throwable{
System.out.println("run");
}
}