Program: 118294
Project: 118294
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();
  }
  if (compFac != null && lowerCasedKeyword != null && 0 < lowerCasedKeyword.length()) {
    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();
          }
        }
        componentSet=new LinkedHashSet<IComponent>(filteredComponent);
      }
    }
    if ((componentSet == null || componentSet.isEmpty()) && componentNameMap != null) {
      componentSet=new LinkedHashSet<IComponent>();
      addComponentsByNameFilter(compFac,componentSet,lowerCasedKeyword);
      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++;
                }
              }
            }
          }
        }
      }
    }
  }
 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();
      }
    }
  }
  return relatedComponents;
}

Fixed version (ground truth):
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;
}