default Governor findGovernor(String currentMethod, Method m) { Governor governor = new NullMaskingGovernor(); try { m = this.getClass().getMethod(currentMethod, new Class[] {}); Governed governed = m.getAnnotation(Governed.class); if (governed != null) { Governor fromValue = m.getAnnotation(Governed.class).value().strategy; Governor fromGovernor = m.getAnnotation(Governed.class).governor().newInstance(); if (fromGovernor.getClass() != fromValue.getClass()) { log.debug("Using specified Governor: {}", fromGovernor.getClass().getName()); governor = fromGovernor; } else { log.debug("User default Governor: " + fromValue.getClass().getName()); governor = fromValue; } } } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException e) { log.error("Error governing method: {}. Returning null just to be safe.", m.getName(), e); } return governor; }
/** * Attempts to applied the specified or default {@link Governor} to the incoming object. The * return type of the {@link Governor} should be the same as the {@code type} parameter or at * least be cast-able to that type. * * @param o - this incoming value to govern * @param type - the type to which the return value is cast * @return */ default <T extends Object> T govern(Object o, Class<T> type) { String currentMethod = Thread.currentThread().getStackTrace()[2].getMethodName(); Method m = null; Governor governor = (findGovernor(currentMethod, m)); return type.cast(governor.govern(o)); }