import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class AutoProxy {
public static void main(String[] args) {
Class[] interfaces = {Person.class};
Person user = (Person) Proxy.newProxyInstance(AutoProxy.class.getClassLoader(), interfaces, new Invoke(new User()));
user.eat();
user.run();
}
}
class Invoke implements InvocationHandler {
private Object object;
public Invoke(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("methods before exe...." + method.getName());
Object back = method.invoke(object, args);
System.out.println("back = " + back);
System.out.println("methods after exe....");
return back;
}
}