1 8 9 package foxtrot.utils; 10 11 import java.lang.reflect.InvocationHandler ; 12 import java.lang.reflect.Method ; 13 import java.lang.reflect.Proxy ; 14 import java.util.EventListener ; 15 16 73 public class EventListenerProxy implements InvocationHandler 74 { 75 81 private static boolean working; 82 83 private EventListener listener; 84 85 88 protected EventListenerProxy(EventListener listener) 89 { 90 if (listener == null) throw new NullPointerException ("EventListener cannot be null"); 91 this.listener = listener; 92 } 93 94 103 public static EventListener create(Class listenerInterface, EventListener listener) 104 { 105 if (!listenerInterface.isInstance(listener)) throw new IllegalArgumentException ("EventListener " + listener + " must implement " + listenerInterface.getName()); 106 return (EventListener )Proxy.newProxyInstance(listenerInterface.getClassLoader(), new Class []{listenerInterface}, new EventListenerProxy(listener)); 107 } 108 109 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 110 { 111 if (working) return null; 112 113 try 114 { 115 working = true; 116 return method.invoke(listener, args); 117 } 118 finally 119 { 120 working = false; 121 } 122 } 123 } 124 | Popular Tags |