Program: 120507
Project: 120507
Buggy source:
private static void sortClasspath(Set<String> jobModuleList,IProcess process) throws CoreException, BusinessException {
  IClasspathEntry[] entries=javaProject.getRawClasspath();
  IClasspathEntry jreClasspathEntry=JavaCore.newContainerEntry(new Path("String_Node_Str"));
  IClasspathEntry classpathEntry=JavaCore.newSourceEntry(javaProject.getPath().append(JavaUtils.JAVA_SRC_DIRECTORY));
  boolean changesDone=false;
  if (!ArrayUtils.contains(entries,jreClasspathEntry)) {
    entries=(IClasspathEntry[])ArrayUtils.add(entries,jreClasspathEntry);
    changesDone=true;
  }
  if (!ArrayUtils.contains(entries,classpathEntry)) {
    IClasspathEntry source=null;
    for (    IClasspathEntry entry : entries) {
      if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        source=entry;
        break;
      }
    }
    if (source != null) {
      entries=(IClasspathEntry[])ArrayUtils.remove(entries,ArrayUtils.indexOf(entries,source));
    }
    entries=(IClasspathEntry[])ArrayUtils.add(entries,classpathEntry);
    changesDone=true;
  }
  Set<String> listModulesReallyNeeded=ModuleNameExtractor.extractFileName(jobModuleList);
  Set<String> listModulesNeededByProcess=new HashSet<String>();
  if (listModulesReallyNeeded != null && listModulesReallyNeeded.size() > 0) {
    for (    String jobModule : listModulesReallyNeeded) {
      listModulesNeededByProcess.add(jobModule);
    }
  }
  Set<String> optionalJarsOnlyForRoutines=new HashSet<String>();
  if (listModulesReallyNeeded == null) {
    listModulesReallyNeeded=new HashSet<String>();
  }
  for (  ModuleNeeded moduleNeeded : ModulesNeededProvider.getModulesNeededForRoutines(ERepositoryObjectType.ROUTINES)) {
    optionalJarsOnlyForRoutines.add(moduleNeeded.getModuleName());
  }
  for (  ModuleNeeded moduleNeeded : ModulesNeededProvider.getModulesNeededForRoutines(ERepositoryObjectType.PIG_UDF)) {
    optionalJarsOnlyForRoutines.add(moduleNeeded.getModuleName());
  }
  optionalJarsOnlyForRoutines.removeAll(listModulesReallyNeeded);
  for (  String jar : optionalJarsOnlyForRoutines) {
    listModulesReallyNeeded.add(jar);
  }
  addLog4jToJarList(listModulesReallyNeeded);
  File libDir=getJavaProjectLibFolder();
  if ((libDir != null) && (libDir.isDirectory())) {
    Set<String> jarsNeedRetrieve=new HashSet<String>(listModulesReallyNeeded);
    for (    File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
      jarsNeedRetrieve.remove(externalLib.getName());
    }
    List<IClasspathEntry> entriesToRemove=new ArrayList<IClasspathEntry>();
    for (    IClasspathEntry entry : entries) {
      if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        boolean found=false;
        for (        File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
          if (entry.getPath().toPortableString().endsWith(externalLib.getName())) {
            found=true;
            break;
          }
        }
        if (!found) {
          entriesToRemove.add(entry);
        }
      }
    }
    for (    IClasspathEntry entry : entriesToRemove) {
      entries=(IClasspathEntry[])ArrayUtils.remove(entries,ArrayUtils.indexOf(entries,entry));
      changesDone=true;
    }
    if (!jarsNeedRetrieve.isEmpty()) {
      Set<String> originalConexts=new HashSet<String>();
      Iterator<String> iterator=jarsNeedRetrieve.iterator();
      while (iterator.hasNext()) {
        String jarNeedRetrieve=iterator.next();
        if (ContextParameterUtils.isContainContextParam(jarNeedRetrieve) && process instanceof IProcess2) {
          iterator.remove();
          IContext lastRunContext=((IProcess2)process).getLastRunContext();
          if (lastRunContext != null) {
            String contextValue=ContextParameterUtils.parseScriptContextCode(jarNeedRetrieve,lastRunContext);
            if (contextValue != null) {
              originalConexts.add(new File(contextValue).getName());
            }
          }
        }
      }
      jarsNeedRetrieve.addAll(originalConexts);
      ILibraryManagerService repositoryBundleService=CorePlugin.getDefault().getRepositoryBundleService();
      repositoryBundleService.retrieve(jarsNeedRetrieve,libDir.getAbsolutePath());
      if (process instanceof IProcess2) {
        ILibrariesService moduleService=CorePlugin.getDefault().getLibrariesService();
        moduleService.resetModulesNeeded();
        ((IProcess2)process).checkProcess();
      }
    }
    for (    File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
      if (externalLib.isFile() && listModulesReallyNeeded.contains(externalLib.getName())) {
        IClasspathEntry newEntry=JavaCore.newLibraryEntry(new Path(externalLib.getAbsolutePath()),null,null);
        if (!ArrayUtils.contains(entries,newEntry)) {
          entries=(IClasspathEntry[])ArrayUtils.add(entries,newEntry);
          changesDone=true;
        }
      }
    }
  }
  String missingJars=null;
  Set<String> missingJarsForRoutinesOnly=new HashSet<String>();
  Set<String> missingJarsForProcessOnly=new HashSet<String>();
  int exchange=2;
  for (  String jar : listModulesReallyNeeded) {
    int index=indexOfEntry(entries,jar);
    if (index < 0) {
      if (ContextParameterUtils.isContainContextParam(jar)) {
        continue;
      }
      if (listModulesNeededByProcess.contains(jar)) {
        missingJarsForProcessOnly.add(jar);
      }
 else {
        missingJarsForRoutinesOnly.add(jar);
      }
      if (missingJars == null) {
        missingJars=Messages.getString("String_Node_Str") + jar;
      }
 else {
        missingJars=missingJars + "String_Node_Str" + jar;
      }
    }
 else {
      if (index != exchange) {
        IClasspathEntry entry=entries[index];
        IClasspathEntry first=entries[exchange];
        entries[index]=first;
        entries[exchange]=entry;
        changesDone=true;
      }
      exchange++;
    }
  }
  if (changesDone) {
    javaProject.setRawClasspath(entries,null);
    javaProject.setOutputLocation(javaProject.getPath().append(JavaUtils.JAVA_CLASSES_DIRECTORY),null);
  }
  if (missingJars != null) {
    handleMissingJarsForProcess(missingJarsForRoutinesOnly,missingJarsForProcessOnly,missingJars);
  }
}

Fixed version (ground truth):
private static void sortClasspath(Set<String> jobModuleList,IProcess process) throws CoreException, BusinessException {
  IClasspathEntry[] entries=javaProject.getRawClasspath();
  IClasspathEntry jreClasspathEntry=JavaCore.newContainerEntry(new Path("String_Node_Str"));
  IClasspathEntry classpathEntry=JavaCore.newSourceEntry(javaProject.getPath().append(JavaUtils.JAVA_SRC_DIRECTORY));
  boolean changesDone=false;
  if (!ArrayUtils.contains(entries,jreClasspathEntry)) {
    entries=(IClasspathEntry[])ArrayUtils.add(entries,jreClasspathEntry);
    changesDone=true;
  }
  if (!ArrayUtils.contains(entries,classpathEntry)) {
    IClasspathEntry source=null;
    for (    IClasspathEntry entry : entries) {
      if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        source=entry;
        break;
      }
    }
    if (source != null) {
      entries=(IClasspathEntry[])ArrayUtils.remove(entries,ArrayUtils.indexOf(entries,source));
    }
    entries=(IClasspathEntry[])ArrayUtils.add(entries,classpathEntry);
    changesDone=true;
  }
  Set<String> listModulesReallyNeeded=ModuleNameExtractor.extractFileName(jobModuleList);
  Set<String> listModulesNeededByProcess=new HashSet<String>();
  if (listModulesReallyNeeded != null && listModulesReallyNeeded.size() > 0) {
    for (    String jobModule : listModulesReallyNeeded) {
      listModulesNeededByProcess.add(jobModule);
    }
  }
  Set<String> optionalJarsOnlyForRoutines=new HashSet<String>();
  if (listModulesReallyNeeded == null) {
    listModulesReallyNeeded=new HashSet<String>();
  }
  for (  ModuleNeeded moduleNeeded : ModulesNeededProvider.getModulesNeededForRoutines(ERepositoryObjectType.ROUTINES)) {
    optionalJarsOnlyForRoutines.add(moduleNeeded.getModuleName());
  }
  for (  ModuleNeeded moduleNeeded : ModulesNeededProvider.getModulesNeededForRoutines(ERepositoryObjectType.PIG_UDF)) {
    optionalJarsOnlyForRoutines.add(moduleNeeded.getModuleName());
  }
  optionalJarsOnlyForRoutines.removeAll(listModulesReallyNeeded);
  for (  String jar : optionalJarsOnlyForRoutines) {
    listModulesReallyNeeded.add(jar);
  }
  addLog4jToJarList(listModulesReallyNeeded);
  File libDir=getJavaProjectLibFolder();
  if ((libDir != null) && (libDir.isDirectory())) {
    Set<String> jarsNeedRetrieve=new HashSet<String>(listModulesReallyNeeded);
    for (    File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
      jarsNeedRetrieve.remove(externalLib.getName());
    }
    List<IClasspathEntry> entriesToRemove=new ArrayList<IClasspathEntry>();
    for (    IClasspathEntry entry : entries) {
      if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        boolean found=false;
        for (        File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
          if (entry.getPath().toPortableString().endsWith(externalLib.getName())) {
            found=true;
            break;
          }
        }
        if (!found) {
          entriesToRemove.add(entry);
        }
      }
    }
    for (    IClasspathEntry entry : entriesToRemove) {
      entries=(IClasspathEntry[])ArrayUtils.remove(entries,ArrayUtils.indexOf(entries,entry));
      changesDone=true;
    }
    if (!jarsNeedRetrieve.isEmpty()) {
      Set<String> originalConexts=new HashSet<String>();
      Iterator<String> iterator=jarsNeedRetrieve.iterator();
      while (iterator.hasNext()) {
        String jarNeedRetrieve=iterator.next();
        if (ContextParameterUtils.isContainContextParam(jarNeedRetrieve) && process instanceof IProcess2) {
          iterator.remove();
          IContext lastRunContext=((IProcess2)process).getLastRunContext();
          if (lastRunContext != null) {
            String contextValue=ContextParameterUtils.parseScriptContextCode(jarNeedRetrieve,lastRunContext);
            if (contextValue != null) {
              originalConexts.add(new File(contextValue).getName());
            }
          }
 else {
            IContextManager contextManager=process.getContextManager();
            if (contextManager != null) {
              String contextValue=ContextParameterUtils.parseScriptContextCode(jarNeedRetrieve,contextManager.getDefaultContext());
              if (contextValue != null) {
                originalConexts.add(new File(contextValue).getName());
              }
            }
          }
        }
      }
      jarsNeedRetrieve.addAll(originalConexts);
      ILibraryManagerService repositoryBundleService=CorePlugin.getDefault().getRepositoryBundleService();
      repositoryBundleService.retrieve(jarsNeedRetrieve,libDir.getAbsolutePath());
      if (process instanceof IProcess2) {
        ILibrariesService moduleService=CorePlugin.getDefault().getLibrariesService();
        moduleService.resetModulesNeeded();
        ((IProcess2)process).checkProcess();
      }
    }
    for (    File externalLib : libDir.listFiles(FilesUtils.getAcceptJARFilesFilter())) {
      if (externalLib.isFile() && listModulesReallyNeeded.contains(externalLib.getName())) {
        IClasspathEntry newEntry=JavaCore.newLibraryEntry(new Path(externalLib.getAbsolutePath()),null,null);
        if (!ArrayUtils.contains(entries,newEntry)) {
          entries=(IClasspathEntry[])ArrayUtils.add(entries,newEntry);
          changesDone=true;
        }
      }
    }
  }
  String missingJars=null;
  Set<String> missingJarsForRoutinesOnly=new HashSet<String>();
  Set<String> missingJarsForProcessOnly=new HashSet<String>();
  int exchange=2;
  for (  String jar : listModulesReallyNeeded) {
    int index=indexOfEntry(entries,jar);
    if (index < 0) {
      if (ContextParameterUtils.isContainContextParam(jar)) {
        continue;
      }
      if (listModulesNeededByProcess.contains(jar)) {
        missingJarsForProcessOnly.add(jar);
      }
 else {
        missingJarsForRoutinesOnly.add(jar);
      }
      if (missingJars == null) {
        missingJars=Messages.getString("String_Node_Str") + jar;
      }
 else {
        missingJars=missingJars + "String_Node_Str" + jar;
      }
    }
 else {
      if (index != exchange) {
        IClasspathEntry entry=entries[index];
        IClasspathEntry first=entries[exchange];
        entries[index]=first;
        entries[exchange]=entry;
        changesDone=true;
      }
      exchange++;
    }
  }
  if (changesDone) {
    javaProject.setRawClasspath(entries,null);
    javaProject.setOutputLocation(javaProject.getPath().append(JavaUtils.JAVA_CLASSES_DIRECTORY),null);
  }
  if (missingJars != null) {
    handleMissingJarsForProcess(missingJarsForRoutinesOnly,missingJarsForProcessOnly,missingJars);
  }
}