1 package examples.workshop; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 import java.lang.reflect.Proxy; 6 7 8 public class MyRunnable implements Runnable, InvocationHandler { 9 private final Runnable runnable; 10 11 public MyRunnable(Runnable runnable) { 12 this.runnable = runnable; 13 } 14 15 public static Runnable wrap(Runnable runnable) { 16 return (Runnable) Proxy.newProxyInstance(runnable.getClass().getClassLoader(), 17 new Class[] { Runnable.class }, 18 new MyRunnable(runnable)); 19 } 20 21 22 @Override 23 public void run() { 24 System.out.println("Calling real run"); 25 runnable.run(); 26 } 27 28 public void inspect() { 29 System.out.println("Calling real run"); 30 runnable.run(); 31 } 32 33 @Override 34 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 35 System.out.println("call cal cal"); 36 return method.invoke(this, args); 37 } 38 }