import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class StudyInvokeHandler implements StudyInvokeHandlerInterface{
public static void main(String[] args) {
StudyInvokeHandlerInterface studyInvokeHandler = new StudyInvokeHandler();
studyInvokeHandler = (StudyInvokeHandlerInterface) Proxy.newProxyInstance(
studyInvokeHandler.getClass().getClassLoader(),
studyInvokeHandler.getClass().getInterfaces(),
new MyInvocationHandler(studyInvokeHandler));
studyInvokeHandler.test();
}
public void test() {
System.out.println("OK");
}
}
interface StudyInvokeHandlerInterface{
public void test();
}
class MyInvocationHandler implements InvocationHandler {
private Object target = null;
public MyInvocationHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("方法执行开始");
Object result = method.invoke(target, args);
System.out.println("方法执行结束");
return result;
}
}