Program: 130053
Project: 130053
Buggy source:
public void unloadUnlockedResources(){
  if (!ProxyRepositoryFactory.getInstance().isFullLogonFinished()) {
    return;
  }
  List<Resource> resourceToUnload=new ArrayList<Resource>();
  List<URI> possibleItemsURItoUnload=new ArrayList<URI>();
  EList<Resource> kaka=xmiResourceManager.resourceSet.getResources();
  for (int i=0; i < kaka.size(); i++) {
    Resource resource=xmiResourceManager.resourceSet.getResources().get(i);
    final EList<EObject> contents=resource.getContents();
    for (int j=0; j < contents.size(); j++) {
      EObject object=contents.get(j);
      if (object instanceof Property) {
        if (((Property)object).getItem() instanceof FolderItem) {
          continue;
        }
        if (((Property)object).getItem() instanceof RoutineItem) {
          RoutineItem item=(RoutineItem)((Property)object).getItem();
          if (item.isBuiltIn()) {
            continue;
          }
        }
        if (((Property)object).getItem() instanceof SQLPatternItem) {
          SQLPatternItem item=(SQLPatternItem)((Property)object).getItem();
          if (item.isSystem()) {
            continue;
          }
        }
        ERepositoryStatus status=getStatus(((Property)object).getItem());
        if ((status == ERepositoryStatus.LOCK_BY_USER) || (status == ERepositoryStatus.NOT_UP_TO_DATE)) {
          continue;
        }
        resourceToUnload.add(resource);
        if (((Property)object).getItem() != null && ((Property)object).getItem().getParent() != null && (((Property)object).getItem().getParent()) instanceof FolderItem) {
          boolean toKeepInMemorySinceDeleted=false;
          if (((Property)object).getItem().getState().isDeleted()) {
            toKeepInMemorySinceDeleted=true;
          }
 else           if (((Property)object).getItem() instanceof ConnectionItem) {
            Connection connection=((ConnectionItem)((Property)object).getItem()).getConnection();
            boolean haveTableDeleted=false;
            for (            MetadataTable table : ConnectionHelper.getTables(connection)) {
              if (SubItemHelper.isDeleted(table)) {
                haveTableDeleted=true;
                break;
              }
            }
            if (!haveTableDeleted) {
              QueriesConnection queriesConnection=connection.getQueries();
              if (queriesConnection != null) {
                for (                Query query : queriesConnection.getQuery()) {
                  if (SubItemHelper.isDeleted(query)) {
                    haveTableDeleted=true;
                    break;
                  }
                }
              }
              if (connection instanceof SAPConnection) {
                SAPConnection sapConn=(SAPConnection)connection;
                if (!haveTableDeleted) {
                  EList<SAPFunctionUnit> funtions=sapConn.getFuntions();
                  for (                  SAPFunctionUnit unit : funtions) {
                    if (SubItemHelper.isDeleted(unit)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
                if (!haveTableDeleted) {
                  EList<SAPIDocUnit> iDocs=sapConn.getIDocs();
                  for (                  SAPIDocUnit iDoc : iDocs) {
                    if (SubItemHelper.isDeleted(iDoc)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
              }
            }
            if (haveTableDeleted) {
              toKeepInMemorySinceDeleted=true;
            }
          }
          if (!toKeepInMemorySinceDeleted) {
            ((FolderItem)((Property)object).getItem().getParent()).getChildren().remove(((Property)object).getItem());
            ((Property)object).getItem().setParent(null);
          }
        }
        possibleItemsURItoUnload.add(xmiResourceManager.getItemResourceURI(resource.getURI()));
      }
    }
  }
  kaka=xmiResourceManager.resourceSet.getResources();
  for (int i=0; i < kaka.size(); i++) {
    Resource resource=kaka.get(i);
    final EList<EObject> contents=resource.getContents();
    for (int j=0; j < contents.size(); j++) {
      EObject object=contents.get(j);
      if (!(object instanceof Property)) {
        if (possibleItemsURItoUnload.contains(resource.getURI()) && !resourceToUnload.contains(resource)) {
          resourceToUnload.add(resource);
        }
      }
    }
  }
  AbstractResourceChangesService resChangeService=TDQServiceRegister.getInstance().getResourceChangeService(AbstractResourceChangesService.class);
  for (int i=0; i < resourceToUnload.size(); i++) {
    Resource resource=resourceToUnload.get(i);
    if (resource.isLoaded()) {
      if (resChangeService != null) {
        resChangeService.handleUnload(resource);
      }
      resource.unload();
    }
  }
}

Fixed version (ground truth):
public void unloadUnlockedResources(){
  if (!ProxyRepositoryFactory.getInstance().isFullLogonFinished()) {
    return;
  }
  List<Resource> resourceToUnload=new ArrayList<Resource>();
  List<URI> possibleItemsURItoUnload=new ArrayList<URI>();
  EList<Resource> kaka=xmiResourceManager.resourceSet.getResources();
  for (int i=0; i < kaka.size(); i++) {
    Resource resource=xmiResourceManager.resourceSet.getResources().get(i);
    if (resource == null) {
      continue;
    }
    final EList<EObject> contents=resource.getContents();
    for (int j=0; j < contents.size(); j++) {
      EObject object=contents.get(j);
      if (object instanceof Property) {
        if (((Property)object).getItem() instanceof FolderItem) {
          continue;
        }
        if (((Property)object).getItem() instanceof RoutineItem) {
          RoutineItem item=(RoutineItem)((Property)object).getItem();
          if (item.isBuiltIn()) {
            continue;
          }
        }
        if (((Property)object).getItem() instanceof SQLPatternItem) {
          SQLPatternItem item=(SQLPatternItem)((Property)object).getItem();
          if (item.isSystem()) {
            continue;
          }
        }
        ERepositoryStatus status=getStatus(((Property)object).getItem());
        if ((status == ERepositoryStatus.LOCK_BY_USER) || (status == ERepositoryStatus.NOT_UP_TO_DATE)) {
          continue;
        }
        resourceToUnload.add(resource);
        if (((Property)object).getItem() != null && ((Property)object).getItem().getParent() != null && (((Property)object).getItem().getParent()) instanceof FolderItem) {
          boolean toKeepInMemorySinceDeleted=false;
          if (((Property)object).getItem().getState().isDeleted()) {
            toKeepInMemorySinceDeleted=true;
          }
 else           if (((Property)object).getItem() instanceof ConnectionItem) {
            Connection connection=((ConnectionItem)((Property)object).getItem()).getConnection();
            boolean haveTableDeleted=false;
            for (            MetadataTable table : ConnectionHelper.getTables(connection)) {
              if (SubItemHelper.isDeleted(table)) {
                haveTableDeleted=true;
                break;
              }
            }
            if (!haveTableDeleted) {
              QueriesConnection queriesConnection=connection.getQueries();
              if (queriesConnection != null) {
                for (                Query query : queriesConnection.getQuery()) {
                  if (SubItemHelper.isDeleted(query)) {
                    haveTableDeleted=true;
                    break;
                  }
                }
              }
              if (connection instanceof SAPConnection) {
                SAPConnection sapConn=(SAPConnection)connection;
                if (!haveTableDeleted) {
                  EList<SAPFunctionUnit> funtions=sapConn.getFuntions();
                  for (                  SAPFunctionUnit unit : funtions) {
                    if (SubItemHelper.isDeleted(unit)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
                if (!haveTableDeleted) {
                  EList<SAPIDocUnit> iDocs=sapConn.getIDocs();
                  for (                  SAPIDocUnit iDoc : iDocs) {
                    if (SubItemHelper.isDeleted(iDoc)) {
                      haveTableDeleted=true;
                      break;
                    }
                  }
                }
              }
            }
            if (haveTableDeleted) {
              toKeepInMemorySinceDeleted=true;
            }
          }
          if (!toKeepInMemorySinceDeleted) {
            ((FolderItem)((Property)object).getItem().getParent()).getChildren().remove(((Property)object).getItem());
            ((Property)object).getItem().setParent(null);
          }
        }
        possibleItemsURItoUnload.add(xmiResourceManager.getItemResourceURI(resource.getURI()));
      }
    }
  }
  kaka=xmiResourceManager.resourceSet.getResources();
  for (int i=0; i < kaka.size(); i++) {
    Resource resource=kaka.get(i);
    final EList<EObject> contents=resource.getContents();
    for (int j=0; j < contents.size(); j++) {
      EObject object=contents.get(j);
      if (!(object instanceof Property)) {
        if (possibleItemsURItoUnload.contains(resource.getURI()) && !resourceToUnload.contains(resource)) {
          resourceToUnload.add(resource);
        }
      }
    }
  }
  AbstractResourceChangesService resChangeService=TDQServiceRegister.getInstance().getResourceChangeService(AbstractResourceChangesService.class);
  for (int i=0; i < resourceToUnload.size(); i++) {
    Resource resource=resourceToUnload.get(i);
    if (resource.isLoaded()) {
      if (resChangeService != null) {
        resChangeService.handleUnload(resource);
      }
      resource.unload();
    }
  }
}