/** * Calls a particular callback function. The function must previously have been checked using * checkCallback(). * * @param sCallback Name of callback * @throws OmException If the callback throws an exception or there was an error calling it */ public void callback(String sCallback) throws OmException { if (!sCheckedCallbacks.contains(sCallback)) throw new OmDeveloperException( "Error running callback " + sCallback + "(): checkCallback was not called"); try { Method m = getClass().getMethod(sCallback, new Class[0]); m.invoke(this, new Object[0]); } catch (InvocationTargetException e) { if (e.getCause() instanceof OmException) throw (OmException) e.getCause(); else throw new OmException("Exception in callback " + sCallback + "()", e.getCause()); } catch (Exception e) { throw new OmUnexpectedException(e); } }
/** * Checks that a callback method is present and correctly defined. * * <p>Having checked it, you can call it using callback(). * * @param sCallback Name of callback method * @throws OmDeveloperException If the method doesn't exist or is defined incorrectly */ public void checkCallback(String sCallback) throws OmDeveloperException { try { Method m = getClass().getMethod(sCallback, new Class[0]); if (m.getReturnType() != void.class) throw new OmDeveloperException("Callback method " + sCallback + "() must return void"); if (!Modifier.isPublic(m.getModifiers())) throw new OmDeveloperException("Callback method " + sCallback + "() must be public"); if (Modifier.isStatic(m.getModifiers())) throw new OmDeveloperException("Callback method " + sCallback + "() may not be static"); if (Modifier.isAbstract(m.getModifiers())) throw new OmDeveloperException("Callback method " + sCallback + "() may not be abstract"); sCheckedCallbacks.add(sCallback); } catch (NoSuchMethodException e) { throw new OmDeveloperException("Callback method " + sCallback + "() does not exist"); } }