Source:
<code>private void organizeStack() { if (iterators.size() == 0) { return; } if (iterators.size() == expectedStackSize && iterators.peek().hasNext()) { return; } if (iterators.size() == expectedStackSize) { iterators.pop(); organizeStack(); return; } final Iterator<EObject> domainIterator = iteratorToCurrentChildrenMap.get(iterators.peek()); if (domainIterator == null) { if (iterators.peek().hasNext()) { final Setting setting = iterators.peek().next(); if (includeNonValueRelatedSettings) { nonValueRelSettings.add(setting); } final Iterator<EObject> childIterator = getChildIterator(setting); iteratorToCurrentChildrenMap.put(iterators.peek(), childIterator); organizeStack(); return; } iterators.pop(); organizeStack(); return; } if (domainIterator.hasNext()) { final EObject nextDomain = domainIterator.next(); <START> final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext(); <END> final ServiceReference<EMFFormsDatabindingEMF> serviceReference = bundleContext .getServiceReference(EMFFormsDatabindingEMF.class); final EMFFormsDatabindingEMF databinding = bundleContext.getService(serviceReference); try { final Setting setting = databinding.getSetting(condition.getValueDomainModelReference(), nextDomain); final Iterator<Setting> iterator = Collections.singleton(setting).iterator(); iterators.push(iterator); } catch (final DatabindingFailedException ex) { final ServiceReference<ReportService> reportReference = bundleContext .getServiceReference(ReportService.class); final ReportService reportService = bundleContext.getService(reportReference); reportService.report(new AbstractReport(ex, String.format("Could not get the setting iterator for DMR: %s and domain model: %s", condition.getValueDomainModelReference(), nextDomain))); bundleContext.ungetService(reportReference); } bundleContext.ungetService(serviceReference); final VDomainModelReference valueDMR = EcoreUtil.copy(condition.getValueDomainModelReference()); dmrs.add(valueDMR); organizeStack(); return; } iteratorToCurrentChildrenMap.remove(iterators.peek()); organizeStack(); return; }</code><technical_language>code duplication -> extract in method call method unget services some dispose event</technical_language>

Target:
private void organizeStack() { if (iterators.size() == 0) { return; } if (iterators.size() == expectedStackSize && iterators.peek().hasNext()) { return; } if (iterators.size() == expectedStackSize) { iterators.pop(); organizeStack(); return; } final Iterator<EObject> domainIterator = iteratorToCurrentChildrenMap.get(iterators.peek()); if (domainIterator == null) { if (iterators.peek().hasNext()) { final Setting setting = iterators.peek().next(); if (includeNonValueRelatedSettings) { nonValueRelSettings.add(setting); } final Iterator<EObject> childIterator = getChildIterator(setting); iteratorToCurrentChildrenMap.put(iterators.peek(), childIterator); organizeStack(); return; } iterators.pop(); organizeStack(); return; } if (domainIterator.hasNext()) { final EObject nextDomain = domainIterator.next(); try { final Setting setting = getDatabinding().getSetting(condition.getValueDomainModelReference(), nextDomain); final Iterator<Setting> iterator = Collections.singleton(setting).iterator(); iterators.push(iterator); } catch (final DatabindingFailedException ex) { getReportService().report(new AbstractReport(ex, String.format("Could not get the setting iterator for DMR: %s and domain model: %s", condition.getValueDomainModelReference(), nextDomain))); } final VDomainModelReference valueDMR = EcoreUtil.copy(condition.getValueDomainModelReference()); dmrs.add(valueDMR); organizeStack(); return; } iteratorToCurrentChildrenMap.remove(iterators.peek()); organizeStack(); return; }