public class RegisteredMethods { int count; Object objects[]; Method methods[]; // convenience version for no args public void handle() { handle(new Object[] { }); } public void handle(Object oargs[]) { for (int i = 0; i < count; i++) { try { //System.out.println(objects[i] + " " + args); methods[i].invoke(objects[i], oargs); } catch (Exception e) { e.printStackTrace(); } } } public void add(Object object, Method method) { if (objects == null) { objects = new Object[5]; methods = new Method[5]; } if (count == objects.length) { Object otemp[] = new Object[count << 1]; System.arraycopy(objects, 0, otemp, 0, count); objects = otemp; Method mtemp[] = new Method[count << 1]; System.arraycopy(methods, 0, mtemp, 0, count); methods = mtemp; } objects[count] = object; methods[count] = method; count++; } /** * Removes first object/method pair matched (and only the first, must be called multiple times if object is registered multiple times). * Does not shrink array afterwards, silently returns if method not found. */ public void remove(Object object, Method method) { boolean foundMethod = false; for (int i=0; i