Guest User

Untitled

a guest
Oct 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. Monitor proxyMonitor = (Monitor) Proxy.newProxyInstance(MonitorProxyTest.class.getClassLoader()
  2. , new Class[]{Monitor.class}
  3. , new LogMonitor(new LMonitor()));
  4. String result = proxyMonitor.display();
  5. assertThat("start display", is(result));
  6.  
  7. // public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
  8.  
  9.  
  10. public interface Monitor {
  11. public String display();
  12. }
  13.  
  14. public class LMonitor implements Monitor {
  15. @Override
  16. public String display() {
  17. return "display";
  18. }
  19. }
  20.  
  21. public class LogMonitor implements InvocationHandler {
  22.  
  23. private final Monitor monitor;
  24.  
  25. public LogMonitor(Monitor monitor) {
  26. this.monitor = monitor;
  27. }
  28.  
  29. @Override
  30. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  31. Object ret = method.invoke(monitor, args);
  32. return "start " + ret;
  33. }
  34. }
Add Comment
Please, Sign In to add comment