import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy implements InvocationHandler {
//目标类
private Object targetObject;
public Object newProxyInstance(Object targetObject){
this.targetObject = targetObject;
//绑定
Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(),this);
return targetObject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
try {
result = method.invoke(targetObject,args);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}
public class ProxyTest {
public static void main(String[] args) {
JDKProxy jdkProxy = new JDKProxy();
PayService payService = (PayService)jdkProxy.newProxyInstance(new PayServiceImpl());
payService.callback("sadwasc");
}
}