private void addListener(Object o){
synchronized(_listeners){
_listeners.add(o);
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Set listeners;
synchronized (_listeners) {
listeners = new HashSet(_listeners);
}
return _type.callListeners(method, args, listeners);
}
}
}
//Interface
interface MyInterface {
int addTwo(int a, int b);
}
/*
* CallBackType,
*/
abstract class CallBackType {
public static final CallBackType RETURN_LAST = new CallBackType() {
public Object callListeners(Method meth, Object[] methArgs,Set listeners)throws Throwable
{
Object last = null;
for (Iterator i=listeners.iterator(); i.hasNext(); ) {
Object listener = (Object)i.next();
try {
last = meth.invoke(listener, methArgs);
} catch(InvocationTargetException e) {
throw e.getTargetException();
}
}
if (last == null) {
// Nobody listening ...
return PrimitiveUtil.getBasicValue(meth.getReturnType());
}
return last;
}
};
public abstract Object callListeners(Method meth, Object[] methArgs,Set listeners)
throws Throwable;
}