InstanceLifecycleListenerProcessor { public void processAnnotations(Bundle bundle) { Set<Method> methods = ReflectionsUtil.getMethods(InstanceLifecycleListener.class, bundle); for (Method method : methods) { InstanceLifecycleListener annotation = ReflectionsUtil.getAnnotationClassLoaderSafe(method, method.getDeclaringClass(), InstanceLifecycleListener.class); InstanceLifecycleListenerType[] types = annotation.value(); String packageName = annotation.packageName(); if (ArrayUtils.isEmpty(types)) { LOGGER.error("InstanceLifecycleListener annotation for {} is specified but its value is missing.", method.toString()); } else if (method.getParameterTypes().length != NUMBER_OF_PARAMETERS) { LOGGER.error("InstanceLifecycleListener annotation cannot be specified for method {}, because it does not have exactly " + "{} parameter", method.toString(), NUMBER_OF_PARAMETERS); } else { String paramType = method.getParameterTypes()[0].getName(); if (!packageName.isEmpty() && !paramType.equals(Object.class.getName())) { LOGGER.error("InstanceLifecycleListener annotation cannot be specified for method {}, " + "because its parameter is not of type Object.", method.toString()); } else { MotechLifecycleListener listener = new MotechLifecycleListener(method.getDeclaringClass(), method.getName(), paramType, packageName, types, Arrays.asList(paramType)); jdoListenerRegistryService.registerListener(listener); } } } }  void processAnnotations(Bundle bundle); @Autowired void setJdoListenerRegistryService(JdoListenerRegistryService jdoListenerRegistryService);  }
@Test public void shouldReturnCorrectListeners() { processor.processAnnotations(bundle); jdoListenerRegistryService.updateEntityNames(); jdoListenerRegistryService.removeInactiveListeners(ENTITY_NAMES); List<MotechLifecycleListener> listeners = jdoListenerRegistryService.getListeners(); Set<String> postCreateMethods = new HashSet<>(); postCreateMethods.add("correctListener"); Set<String> postDeleteMethods = new HashSet<>(); postDeleteMethods.add("correctListener"); postDeleteMethods.add("correctPostDeleteListener"); assertEquals(2, listeners.size()); for (MotechLifecycleListener listener : listeners) { if (!listener.getPackageName().isEmpty()) { assertEquals(Arrays.asList("org.motechproject.Test"), listener.getEntityNames()); } else { assertEquals(Sample.class.getName(), listener.getParameterType()); assertEquals(Sample.class, listener.getService()); assertEquals(postCreateMethods, listener.getMethodsByType().get(InstanceLifecycleListenerType.POST_CREATE)); assertEquals(postDeleteMethods, listener.getMethodsByType().get(InstanceLifecycleListenerType.POST_DELETE)); } } }