Program: 117935
Project: 117935
Buggy source:
protected static List<IComponent> getRelatedComponents(final IComponentsFactory compFac,String keyword,boolean needCheckVisible){
  Set<IComponent> componentSet=null;
  IComponentsHandler componentsHandler=compFac.getComponentsHandler();
  String lowerCasedKeyword=null;
  if (keyword != null) {
    lowerCasedKeyword=keyword.toLowerCase().trim();
  }
  boolean needSort=true;
  if (compFac != null && lowerCasedKeyword != null && 0 < lowerCasedKeyword.length()) {
    componentSet=new LinkedHashSet<IComponent>();
    Map<String,Map<String,Set<IComponent>>> componentNameMap=compFac.getComponentNameMap();
    if (componentNameMap != null) {
      Map<String,Set<IComponent>> map=componentNameMap.get(lowerCasedKeyword);
      if (map != null) {
        Collection<Set<IComponent>> componentSets=map.values();
        Iterator<Set<IComponent>> componentSetIter=componentSets.iterator();
        List<IComponent> filteredComponent=new ArrayList<IComponent>();
        while (componentSetIter.hasNext()) {
          filteredComponent.addAll(componentSetIter.next());
        }
        if (componentsHandler != null) {
          filteredComponent=componentsHandler.filterComponents(filteredComponent);
        }
        Iterator<IComponent> componentIter=filteredComponent.iterator();
        while (componentIter.hasNext()) {
          IComponent iComponent=componentIter.next();
          if (iComponent == null || !ComponentUtilities.isComponentVisible(iComponent) || iComponent.isTechnical()) {
            componentIter.remove();
          }
        }
        if (!filteredComponent.isEmpty()) {
          componentSet.addAll(filteredComponent);
        }
      }
    }
    if (componentNameMap != null) {
      addComponentsByNameFilter(compFac,componentSet,lowerCasedKeyword);
      if (!componentSet.isEmpty()) {
        componentSet=new LinkedHashSet<IComponent>(sortResultsBasedOnRecentlyUsed(new ArrayList<IComponent>(componentSet)));
        needSort=false;
      }
      boolean shouldSearchFromHelpAPI=PaletteSettingsPreferencePage.isPaletteSearchFromHelp();
      if (shouldSearchFromHelpAPI) {
        String helpKeyword=keyword;
        if (helpKeyword != null) {
          helpKeyword=helpKeyword.trim();
        }
        Set<String> componentNames=getRelatedComponentNamesFromHelp(helpKeyword);
        if (componentNames != null && 0 < componentNames.size()) {
          int limit=PaletteSettingsPreferencePage.getPaletteSearchResultLimitFromHelp();
          int i=0;
          Iterator<String> nameIter=componentNames.iterator();
          while (nameIter.hasNext()) {
            if (limit <= i) {
              break;
            }
            String componentName=nameIter.next();
            Map<String,Set<IComponent>> map=componentNameMap.get(componentName.toLowerCase());
            if (map == null) {
              continue;
            }
            Set<IComponent> findedComponents=map.get(componentName);
            if (findedComponents != null && !findedComponents.isEmpty()) {
              for (              IComponent iComponent : findedComponents) {
                if (limit <= i) {
                  break;
                }
                if (ComponentUtilities.isComponentVisible(iComponent) && !iComponent.isTechnical() && filterComponent(iComponent,componentsHandler)) {
                  componentSet.add(iComponent);
                  i++;
                }
              }
            }
          }
          if (0 < i) {
            needSort=false;
          }
        }
      }
    }
  }
 else   if (compFac != null) {
    componentSet=compFac.getComponents();
  }
  List<IComponent> relatedComponents=null;
  if (componentSet == null || componentSet.isEmpty()) {
    relatedComponents=new LinkedList<IComponent>();
  }
 else {
    relatedComponents=new LinkedList<IComponent>(componentSet);
  }
  if (compFac != null && componentsHandler != null && !relatedComponents.isEmpty()) {
    relatedComponents=componentsHandler.filterComponents(relatedComponents);
  }
  if (needCheckVisible && relatedComponents != null && !relatedComponents.isEmpty()) {
    Iterator<IComponent> iter=relatedComponents.iterator();
    while (iter.hasNext()) {
      IComponent component=iter.next();
      if (component == null || !ComponentUtilities.isComponentVisible(component) || component.isTechnical()) {
        iter.remove();
      }
    }
  }
  if (needSort) {
    relatedComponents=sortResultsBasedOnRecentlyUsed(relatedComponents);
  }
  return relatedComponents;
}

Fixed version (ground truth):
protected static List<IComponent> getRelatedComponents(final IComponentsFactory compFac,String keyword,boolean needCheckVisible){
  Collection<IComponent> componentSet=null;
  IComponentsHandler componentsHandler=compFac.getComponentsHandler();
  String lowerCasedKeyword=null;
  if (keyword != null) {
    lowerCasedKeyword=keyword.toLowerCase().trim();
  }
  boolean needSort=true;
  if (compFac != null && lowerCasedKeyword != null && 0 < lowerCasedKeyword.length()) {
    componentSet=new LinkedHashSet<IComponent>();
    Map<String,Map<String,Set<IComponent>>> componentNameMap=compFac.getComponentNameMap();
    if (componentNameMap != null) {
      Map<String,Set<IComponent>> map=componentNameMap.get(lowerCasedKeyword);
      if (map != null) {
        Collection<Set<IComponent>> componentSets=map.values();
        Iterator<Set<IComponent>> componentSetIter=componentSets.iterator();
        List<IComponent> filteredComponent=new ArrayList<IComponent>();
        while (componentSetIter.hasNext()) {
          filteredComponent.addAll(componentSetIter.next());
        }
        if (componentsHandler != null) {
          filteredComponent=componentsHandler.filterComponents(filteredComponent);
        }
        Iterator<IComponent> componentIter=filteredComponent.iterator();
        while (componentIter.hasNext()) {
          IComponent iComponent=componentIter.next();
          if (iComponent == null || !ComponentUtilities.isComponentVisible(iComponent) || iComponent.isTechnical()) {
            componentIter.remove();
          }
        }
        if (!filteredComponent.isEmpty()) {
          componentSet.addAll(filteredComponent);
        }
      }
    }
    if (componentNameMap != null) {
      addComponentsByNameFilter(compFac,componentSet,lowerCasedKeyword);
      if (!componentSet.isEmpty()) {
        componentSet=new LinkedHashSet<IComponent>(sortResultsBasedOnRecentlyUsed(new ArrayList<IComponent>(componentSet)));
        needSort=false;
      }
      boolean shouldSearchFromHelpAPI=PaletteSettingsPreferencePage.isPaletteSearchFromHelp();
      if (shouldSearchFromHelpAPI) {
        String helpKeyword=keyword;
        if (helpKeyword != null) {
          helpKeyword=helpKeyword.trim();
        }
        Set<String> componentNames=getRelatedComponentNamesFromHelp(helpKeyword);
        if (componentNames != null && 0 < componentNames.size()) {
          int limit=PaletteSettingsPreferencePage.getPaletteSearchResultLimitFromHelp();
          int i=0;
          Iterator<String> nameIter=componentNames.iterator();
          while (nameIter.hasNext()) {
            if (limit <= i) {
              break;
            }
            String componentName=nameIter.next();
            Map<String,Set<IComponent>> map=componentNameMap.get(componentName.toLowerCase());
            if (map == null) {
              continue;
            }
            Set<IComponent> findedComponents=map.get(componentName);
            if (findedComponents != null && !findedComponents.isEmpty()) {
              for (              IComponent iComponent : findedComponents) {
                if (limit <= i) {
                  break;
                }
                if (ComponentUtilities.isComponentVisible(iComponent) && !iComponent.isTechnical() && filterComponent(iComponent,componentsHandler)) {
                  componentSet.add(iComponent);
                  i++;
                }
              }
            }
          }
          if (0 < i) {
            needSort=false;
          }
        }
      }
    }
  }
 else   if (compFac != null) {
    componentSet=compFac.readComponents();
  }
  List<IComponent> relatedComponents=null;
  if (componentSet == null || componentSet.isEmpty()) {
    relatedComponents=new LinkedList<IComponent>();
  }
 else {
    relatedComponents=new LinkedList<IComponent>(componentSet);
  }
  if (compFac != null && componentsHandler != null && !relatedComponents.isEmpty()) {
    relatedComponents=componentsHandler.filterComponents(relatedComponents);
  }
  if (needCheckVisible && relatedComponents != null && !relatedComponents.isEmpty()) {
    Iterator<IComponent> iter=relatedComponents.iterator();
    while (iter.hasNext()) {
      IComponent component=iter.next();
      if (component == null || !ComponentUtilities.isComponentVisible(component) || component.isTechnical()) {
        iter.remove();
      }
    }
  }
  if (needSort) {
    relatedComponents=sortResultsBasedOnRecentlyUsed(relatedComponents);
  }
  return relatedComponents;
}