SpringJavaConfigService { public <T> List<T> getBeanDefinitions(Class<?> configFile, Project project, Class<T> type) { log.debug(String.format("Reading Java config: %s", configFile.getName())); log.debug(String.format("Reading beans of type: %s", type.getName())); List<T> beanDefinitions = new ArrayList<>(); List<Class<?>> importedFiles = getConfigImports(configFile, project); for (Class<?> importLocation : importedFiles) { beanDefinitions.addAll(getBeanDefinitions(importLocation, project, type)); } final List<Method> beanDefinitionMethods = new ArrayList<>(); ReflectionUtils.doWithMethods(configFile, method -> { if (type.equals(SpringBean.class)) { if (modelConverter.stream().noneMatch(converter -> converter.getSourceModelClass().equals(method.getReturnType()))) { beanDefinitionMethods.add(method); } } else { if (method.getReturnType().equals(type) || modelConverter.stream().anyMatch(converter -> converter.getTargetModelClass().equals(type) && converter.getSourceModelClass().equals(method.getReturnType()))) { beanDefinitionMethods.add(method); } } }, method -> method.getAnnotation(Bean.class) != null); for (Method beanDefinitionMethod : beanDefinitionMethods) { try { String beanId = beanDefinitionMethod.getName(); Bean beanAnnotation = beanDefinitionMethod.getAnnotation(Bean.class); if (beanAnnotation != null) { if (beanAnnotation.value().length > 0) { beanId = beanAnnotation.value()[0]; } else if (beanAnnotation.name().length > 0) { beanId = beanAnnotation.name()[0]; } } log.debug(String.format("Found bean definition '%s' through method: %s()", beanId, beanDefinitionMethod.getName())); Object bean = beanDefinitionMethod.invoke(configFile.newInstance()); if (bean.getClass().equals(type)) { beanDefinitions.add((T) bean); } else if (type.equals(SpringBean.class)) { SpringBeanModelConverter<Object> springBeanModelConverter = new SpringBeanModelConverter(bean.getClass()); beanDefinitions.add((T) springBeanModelConverter.convert(beanId, bean)); } else { for (ModelConverter converter : modelConverter) { if (converter.getSourceModelClass().equals(bean.getClass()) && converter.getTargetModelClass().equals(type)) { beanDefinitions.add((T) converter.convert(beanId, bean)); break; } } } } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { log.error("Failed to access bean definition method in Java config", e); } } log.debug(String.format("Found %s Java config beans of type: %s", beanDefinitions.size(), type.getName())); return beanDefinitions; }  List<Class<?>> getConfigImports(Class<?> configFile, Project project); T getBeanDefinition(Class<?> configFile, Project project, String id, Class<T> type); List<T> getBeanDefinitions(Class<?> configFile, Project project, Class<T> type); List<String> getBeanNames(Class<?> configFile, Project project, Class<?> type); void addBeanDefinition(File configFile, Project project, Object model); void removeBeanDefinition(File configFile, Project project, String id); void removeBeanDefinitions(File configFile, Project project, Class<?> type); void updateBeanDefinition(File configFile, Project project, String id, Object model); void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object model);  }
@Test public void testGetBeanDefinitions() throws Exception { List<JmsEndpointModel> endpoints = springJavaConfigService.getBeanDefinitions(GetBeanDefinitionConfig.class, project, JmsEndpointModel.class); Assert.assertEquals(endpoints.size(), 2L); Assert.assertTrue(endpoints.stream().anyMatch(endpoint -> endpoint.getDestinationName().equals("jms.inbound.queue"))); Assert.assertTrue(endpoints.stream().anyMatch(endpoint -> endpoint.getDestinationName().equals("jms.outbound.queue"))); List<JmsSyncEndpointModel> syncEndpoints = springJavaConfigService.getBeanDefinitions(GetBeanDefinitionConfig.class, project, JmsSyncEndpointModel.class); Assert.assertEquals(syncEndpoints.size(), 1L); Assert.assertEquals(syncEndpoints.get(0).getId(), "jmsSyncEndpoint"); Assert.assertTrue(syncEndpoints.stream().anyMatch(endpoint -> endpoint.getDestinationName().equals("jms.inbound.sync.queue"))); List<WebServiceClientModel> clients = springJavaConfigService.getBeanDefinitions(GetBeanDefinitionConfig.class, project, WebServiceClientModel.class); Assert.assertEquals(clients.size(), 0L); List<SpringBean> beans = springJavaConfigService.getBeanDefinitions(GetBeanDefinitionConfig.class, project, SpringBean.class); Assert.assertEquals(beans.size(), 1L); Assert.assertEquals(beans.get(0).getId(), "testActor"); }