Program: 11720
Project: 11720
Buggy source:
@Override protected Diagnostic doExport(Monitor monitor,ModelExporter.ExportData exportData) throws Exception {
  for (  Map.Entry<GenPackage,URI> entry : exportData.genPackageToArtifactURI.entrySet()) {
    GenPackage genPackage=entry.getKey();
    URI xcoreLocationURI=entry.getValue();
    IFile xcoreFile=ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(xcoreLocationURI.toPlatformString(true)));
    final IProject xcoreProject=xcoreFile.getProject();
    if (xcoreProject.isAccessible()) {
      if (!xcoreProject.hasNature(XtextProjectHelper.NATURE_ID)) {
        IProjectDescription description=xcoreProject.getDescription();
        String[] natures=description.getNatureIds();
        String[] newNatures=new String[natures.length + 1];
        System.arraycopy(natures,0,newNatures,0,natures.length);
        newNatures[natures.length]=XtextProjectHelper.NATURE_ID;
        description.setNatureIds(newNatures);
        xcoreProject.setDescription(description,null);
      }
      XcoreClasspathUpdater xcoreClasspathUpdater=new XcoreClasspathUpdater();
      IJavaProject xcoreJavaProject=JavaCore.create(xcoreProject);
      xcoreClasspathUpdater.addBundle(xcoreJavaProject,"String_Node_Str",null);
      xcoreClasspathUpdater.addBundle(xcoreJavaProject,"String_Node_Str",null);
    }
    final ResourceSet resourceSet=resourceSetProvider.get();
    resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
    GenModel inputGenModel=(GenModel)resourceSet.getEObject(EcoreUtil.getURI(getGenModel()),true);
    inputGenModel.reconcile();
    Resource inputResource=inputGenModel.eResource();
    final XtextResource outputResource=(XtextResource)resourceSet.createResource(xcoreLocationURI);
    GenModel genModel=GenModelFactory.eINSTANCE.createGenModel();
    List<EPackage> ePackageClones=new ArrayList<EPackage>();
    for (    GenPackage inputGenPackage : inputGenModel.getGenPackages()) {
      ePackageClones.add(inputGenPackage.getEcorePackage());
    }
    genModel.initialize(ePackageClones);
    inputResource.getContents().add(genModel);
    genModelInitializer.initialize(genModel,true);
    final GenPackage inputGenPackage=(GenPackage)resourceSet.getEObject(EcoreUtil.getURI(genPackage),true);
    new Object(){
      void visit(      GenBase genBase1,      GenBase genBase2){
        if (genBase1.eClass() == genBase2.eClass()) {
          for (          EAttribute eAttribute : genBase1.eClass().getEAllAttributes()) {
            if (!eAttribute.isMany() && genBase1.eIsSet(eAttribute)) {
              Object value1=genBase1.eGet(eAttribute);
              Object value2=genBase2.eGet(eAttribute);
              if (value1 == null ? value2 != null : !value1.equals(value2)) {
                EModelElement eModelElement=genBase2.getEcoreModelElement();
                if (eModelElement == null && genBase2 instanceof GenModel) {
                  eModelElement=inputGenPackage.getEcorePackage();
                }
                EcoreUtil.setAnnotation(eModelElement,GenModelPackage.eNS_URI,eAttribute.getName(),EcoreUtil.convertToString(eAttribute.getEAttributeType(),value1));
              }
              for (Iterator<EObject> i=genBase1.eContents().iterator(), j=genBase2.eContents().iterator(); i.hasNext() && j.hasNext(); ) {
                EObject content1=i.next();
                EObject content2=j.next();
                if (content1 instanceof GenBase && content2 instanceof GenBase) {
                  visit((GenBase)content1,(GenBase)content2);
                }
              }
            }
          }
        }
      }
    }
.visit(inputGenModel,genModel);
    EcoreXcoreBuilder ecoreXcoreBuilder=ecoreXcoreBuilderProvider.get();
    ecoreXcoreBuilder.initialize(inputGenModel);
    XPackage xPackage=ecoreXcoreBuilder.getXPackage(inputGenPackage.getEcorePackage());
    outputResource.getContents().add(xPackage);
    outputResource.getContents().add(inputGenModel);
    outputResource.getContents().add(inputGenPackage.getEcorePackage());
    ResourceSet additionalResources=resourceSetProvider.get();
    GenPackage ecoreGenPackage=inputGenModel.getEcoreGenPackage();
    if (ecoreGenPackage != null) {
      Resource ecoreResource=additionalResources.createResource(URI.createPlatformResourceURI("String_Node_Str",false));
      ecoreResource.getContents().add(ecoreGenPackage.getGenModel());
    }
    GenPackage xmlTypeGenPackage=inputGenModel.getXMLTypeGenPackage();
    if (xmlTypeGenPackage != null) {
      Resource xmlTypeResource=additionalResources.createResource(URI.createPlatformResourceURI("String_Node_Str",false));
      xmlTypeResource.getContents().add(xmlTypeGenPackage.getGenModel());
    }
    GenPackage xmlNamespaceGenPackage=inputGenModel.getXMLNamespaceGenPackage();
    if (xmlNamespaceGenPackage != null) {
      Resource xmlNamespaceResource=additionalResources.createResource(URI.createPlatformResourceURI("String_Node_Str",false));
      xmlNamespaceResource.getContents().add(xmlNamespaceGenPackage.getGenModel());
    }
    resourceSet.getURIConverter().getURIMap().remove(URI.createPlatformResourceURI("String_Node_Str",false));
    ecoreXcoreBuilder.link();
    genModelBuilder.buildMap(inputGenModel);
    ImportManager importManager=new ImportManager(inputGenPackage.getInterfacePackageName()){
      @Override protected boolean shouldImport(      String packageName,      String shortName,      String importName){
        return true;
      }
    }
;
    Map<EGenericType,XGenericType> genericTypeMap=ecoreXcoreBuilder.getGenericTypeMap();
    for (TreeIterator<EObject> i=inputGenPackage.getEcorePackage().eAllContents(); i.hasNext(); ) {
      EObject eObject=i.next();
      if (eObject instanceof EGenericType) {
        EGenericType eGenericType=(EGenericType)eObject;
        EClassifier eClassifier=eGenericType.getEClassifier();
        if (eClassifier != null) {
          GenClassifier genClassifier=inputGenModel.findGenClassifier(eClassifier);
          QualifiedName qualifiedName=qualifiedNameProvider.getFullyQualifiedName(genClassifier);
          String qualifiedNameValue=qualifiedNameConverter.toString(qualifiedName);
          if (!IMPLICIT_ALIASES.contains(qualifiedNameValue)) {
            importManager.addImport(qualifiedNameValue);
          }
          XGenericType xGenericType=genericTypeMap.get(eGenericType);
          IScope scope=scopeProvider.getScope(xGenericType,XcorePackage.Literals.XGENERIC_TYPE__TYPE);
          IEObjectDescription genClassifierDescription=scope.getSingleElement(qualifiedName);
          if (genClassifierDescription != null) {
            EObject resolvedGenClassifier=resourceSet.getEObject(genClassifierDescription.getEObjectURI(),true);
            if (resolvedGenClassifier != null && resolvedGenClassifier != genClassifier) {
              xGenericType.setType((GenClassifier)resolvedGenClassifier);
            }
          }
        }
      }
 else       if (eObject instanceof EReference) {
        EReference eReference=(EReference)eObject;
        XReference xReference=(XReference)mapper.getToXcoreMapping(eReference).getXcoreElement();
        EList<GenFeature> keys=xReference.getKeys();
        GenFeature opposite=xReference.getOpposite();
        if (opposite != null) {
          IScope scope=scopeProvider.getScope(xReference,XcorePackage.Literals.XREFERENCE__OPPOSITE);
          IEObjectDescription genFeatureDescription=scope.getSingleElement(QualifiedName.create(opposite.getName()));
          if (genFeatureDescription != null) {
            EObject resolvedGenFeature=resourceSet.getEObject(genFeatureDescription.getEObjectURI(),true);
            if (resolvedGenFeature != null) {
              xReference.setOpposite((GenFeature)resolvedGenFeature);
            }
          }
        }
        if (!keys.isEmpty()) {
          IScope scope=scopeProvider.getScope(xReference,XcorePackage.Literals.XREFERENCE__KEYS);
          for (ListIterator<GenFeature> k=keys.listIterator(); k.hasNext(); ) {
            GenFeature key=k.next();
            IEObjectDescription genFeatureDescription=scope.getSingleElement(QualifiedName.create(key.getName()));
            if (genFeatureDescription != null) {
              EObject resolvedGenFeature=resourceSet.getEObject(genFeatureDescription.getEObjectURI(),true);
              if (resolvedGenFeature != null) {
                k.set((GenFeature)resolvedGenFeature);
              }
            }
          }
        }
      }
 else       if (eObject instanceof EPackage) {
        i.prune();
      }
    }
    for (    String qualifiedName : importManager.getImports()) {
      XImportDirective xImportDirective=XcoreFactory.eINSTANCE.createXImportDirective();
      xImportDirective.setImportedNamespace(qualifiedName);
      xPackage.getImportDirectives().add(xImportDirective);
    }
    final Map<Object,Object> options=new HashMap<Object,Object>();
    SaveOptions.newBuilder().format().noValidation().getOptions().addTo(options);
    Job job=new Job("String_Node_Str"){
      @Override protected IStatus run(      IProgressMonitor monitor){
        try {
          Map<URI,URI> uriMap=resourceSet.getURIConverter().getURIMap();
          Map<URI,URI> copyiedURIMap=new HashMap<URI,URI>(uriMap);
          uriMap.clear();
          outputResource.save(options);
          uriMap.putAll(copyiedURIMap);
          outputResource.unload();
          outputResource.load(null);
          IParseResult parseResult=outputResource.getParseResult();
          if (parseResult != null) {
            IPreferenceValues configuration=preferencesProvider.getPreferenceValues(outputResource);
            try {
              List<TextReplacement> edits=formatter.format(outputResource,0,parseResult.getRootNode().getLength(),new FormattingPreferenceValues(configuration));
              BufferedInputStream bufferedInputStream=new BufferedInputStream(resourceSet.getURIConverter().createInputStream(outputResource.getURI()));
              byte[] input=new byte[bufferedInputStream.available()];
              bufferedInputStream.read(input);
              bufferedInputStream.close();
              String text=new String(input,outputResource.getEncoding());
              StringBuilder builder=new StringBuilder(text);
              for (int i=edits.size(); --i >= 0; ) {
                TextReplacement replacement=edits.get(i);
                builder.replace(replacement.getOffset(),replacement.getOffset() + replacement.getLength(),replacement.getText());
              }
              OutputStream outputStream=resourceSet.getURIConverter().createOutputStream(outputResource.getURI());
              outputStream.write(builder.toString().getBytes(outputResource.getEncoding()));
              outputStream.close();
            }
 catch (            Throwable throwable) {
              XcoreExporterPlugin.INSTANCE.log(throwable);
            }
          }
        }
 catch (        IOException exception) {
          return Status.CANCEL_STATUS;
        }
        return Status.OK_STATUS;
      }
    }
;
    job.schedule();
  }
  return Diagnostic.OK_INSTANCE;
}

Fixed version (ground truth):
@Override protected Diagnostic doExport(Monitor monitor,ModelExporter.ExportData exportData) throws Exception {
  for (  Map.Entry<GenPackage,URI> entry : exportData.genPackageToArtifactURI.entrySet()) {
    GenPackage genPackage=entry.getKey();
    URI xcoreLocationURI=entry.getValue();
    IFile xcoreFile=ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(xcoreLocationURI.toPlatformString(true)));
    final IProject xcoreProject=xcoreFile.getProject();
    if (xcoreProject.isAccessible()) {
      if (!xcoreProject.hasNature(XtextProjectHelper.NATURE_ID)) {
        IProjectDescription description=xcoreProject.getDescription();
        String[] natures=description.getNatureIds();
        String[] newNatures=new String[natures.length + 1];
        System.arraycopy(natures,0,newNatures,0,natures.length);
        newNatures[natures.length]=XtextProjectHelper.NATURE_ID;
        description.setNatureIds(newNatures);
        xcoreProject.setDescription(description,null);
      }
      XcoreClasspathUpdater xcoreClasspathUpdater=new XcoreClasspathUpdater();
      IJavaProject xcoreJavaProject=JavaCore.create(xcoreProject);
      xcoreClasspathUpdater.addBundle(xcoreJavaProject,"String_Node_Str",null);
      xcoreClasspathUpdater.addBundle(xcoreJavaProject,"String_Node_Str",null);
    }
    final ResourceSet resourceSet=resourceSetProvider.get();
    final Map<URI,URI> uriMap=resourceSet.getURIConverter().getURIMap();
    Registry packageRegistry=resourceSet.getPackageRegistry();
    uriMap.putAll(EcorePlugin.computePlatformURIMap(true));
    GenModel inputGenModel=(GenModel)resourceSet.getEObject(EcoreUtil.getURI(getGenModel()),true);
    inputGenModel.reconcile();
    Resource inputResource=inputGenModel.eResource();
    final XtextResource outputResource=(XtextResource)resourceSet.createResource(xcoreLocationURI);
    GenModel genModel=GenModelFactory.eINSTANCE.createGenModel();
    List<EPackage> ePackageClones=new ArrayList<EPackage>();
    for (    GenPackage inputGenPackage : inputGenModel.getGenPackages()) {
      ePackageClones.add(inputGenPackage.getEcorePackage());
    }
    genModel.initialize(ePackageClones);
    inputResource.getContents().add(genModel);
    genModelInitializer.initialize(genModel,true);
    final GenPackage inputGenPackage=(GenPackage)resourceSet.getEObject(EcoreUtil.getURI(genPackage),true);
    new Object(){
      void visit(      GenBase genBase1,      GenBase genBase2){
        if (genBase1.eClass() == genBase2.eClass()) {
          for (          EAttribute eAttribute : genBase1.eClass().getEAllAttributes()) {
            if (!eAttribute.isMany() && genBase1.eIsSet(eAttribute)) {
              Object value1=genBase1.eGet(eAttribute);
              Object value2=genBase2.eGet(eAttribute);
              if (value1 == null ? value2 != null : !value1.equals(value2)) {
                EModelElement eModelElement=genBase2.getEcoreModelElement();
                if (eModelElement == null && genBase2 instanceof GenModel) {
                  eModelElement=inputGenPackage.getEcorePackage();
                }
                EcoreUtil.setAnnotation(eModelElement,GenModelPackage.eNS_URI,eAttribute.getName(),EcoreUtil.convertToString(eAttribute.getEAttributeType(),value1));
              }
              for (Iterator<EObject> i=genBase1.eContents().iterator(), j=genBase2.eContents().iterator(); i.hasNext() && j.hasNext(); ) {
                EObject content1=i.next();
                EObject content2=j.next();
                if (content1 instanceof GenBase && content2 instanceof GenBase) {
                  visit((GenBase)content1,(GenBase)content2);
                }
              }
            }
          }
        }
      }
    }
.visit(inputGenModel,genModel);
    EcoreXcoreBuilder ecoreXcoreBuilder=ecoreXcoreBuilderProvider.get();
    ecoreXcoreBuilder.initialize(inputGenModel);
    XPackage xPackage=ecoreXcoreBuilder.getXPackage(inputGenPackage.getEcorePackage());
    outputResource.getContents().add(xPackage);
    outputResource.getContents().add(inputGenModel);
    outputResource.getContents().add(inputGenPackage.getEcorePackage());
    ResourceSet additionalResources=resourceSetProvider.get();
    GenPackage ecoreGenPackage=inputGenModel.getEcoreGenPackage();
    if (ecoreGenPackage != null) {
      URI ecoreResourceURI=URI.createPlatformResourceURI("String_Node_Str",false);
      packageRegistry.put(ecoreResourceURI.toString(),ecoreGenPackage.getEcorePackage());
      Resource ecoreResource=additionalResources.createResource(ecoreResourceURI);
      ecoreResource.getContents().add(ecoreGenPackage.getGenModel());
    }
    GenPackage xmlTypeGenPackage=inputGenModel.getXMLTypeGenPackage();
    if (xmlTypeGenPackage != null) {
      URI xmlTypeResourceURI=URI.createPlatformResourceURI("String_Node_Str",false);
      packageRegistry.put(xmlTypeResourceURI.toString(),xmlTypeGenPackage.getEcorePackage());
      Resource xmlTypeResource=additionalResources.createResource(xmlTypeResourceURI);
      xmlTypeResource.getContents().add(xmlTypeGenPackage.getGenModel());
    }
    GenPackage xmlNamespaceGenPackage=inputGenModel.getXMLNamespaceGenPackage();
    if (xmlNamespaceGenPackage != null) {
      URI xmlNamespaceResourceURI=URI.createPlatformResourceURI("String_Node_Str",false);
      packageRegistry.put(xmlNamespaceResourceURI.toString(),xmlNamespaceGenPackage.getEcorePackage());
      Resource xmlNamespaceResource=additionalResources.createResource(xmlNamespaceResourceURI);
      xmlNamespaceResource.getContents().add(xmlNamespaceGenPackage.getGenModel());
    }
    ecoreXcoreBuilder.link();
    genModelBuilder.buildMap(inputGenModel);
    ImportManager importManager=new ImportManager(inputGenPackage.getInterfacePackageName()){
      @Override protected boolean shouldImport(      String packageName,      String shortName,      String importName){
        return true;
      }
    }
;
    Map<EGenericType,XGenericType> genericTypeMap=ecoreXcoreBuilder.getGenericTypeMap();
    for (TreeIterator<EObject> i=inputGenPackage.getEcorePackage().eAllContents(); i.hasNext(); ) {
      EObject eObject=i.next();
      if (eObject instanceof EGenericType) {
        EGenericType eGenericType=(EGenericType)eObject;
        EClassifier eClassifier=eGenericType.getEClassifier();
        if (eClassifier != null) {
          GenClassifier genClassifier=inputGenModel.findGenClassifier(eClassifier);
          QualifiedName qualifiedName=qualifiedNameProvider.getFullyQualifiedName(genClassifier);
          String qualifiedNameValue=qualifiedNameConverter.toString(qualifiedName);
          if (!IMPLICIT_ALIASES.contains(qualifiedNameValue)) {
            importManager.addImport(qualifiedNameValue);
          }
          XGenericType xGenericType=genericTypeMap.get(eGenericType);
          IScope scope=scopeProvider.getScope(xGenericType,XcorePackage.Literals.XGENERIC_TYPE__TYPE);
          IEObjectDescription genClassifierDescription=scope.getSingleElement(qualifiedName);
          if (genClassifierDescription != null) {
            EObject resolvedGenClassifier=resourceSet.getEObject(genClassifierDescription.getEObjectURI(),true);
            if (resolvedGenClassifier != null && resolvedGenClassifier != genClassifier) {
              xGenericType.setType((GenClassifier)resolvedGenClassifier);
            }
          }
        }
      }
 else       if (eObject instanceof EReference) {
        EReference eReference=(EReference)eObject;
        XReference xReference=(XReference)mapper.getToXcoreMapping(eReference).getXcoreElement();
        EList<GenFeature> keys=xReference.getKeys();
        GenFeature opposite=xReference.getOpposite();
        if (opposite != null) {
          IScope scope=scopeProvider.getScope(xReference,XcorePackage.Literals.XREFERENCE__OPPOSITE);
          IEObjectDescription genFeatureDescription=scope.getSingleElement(QualifiedName.create(opposite.getName()));
          if (genFeatureDescription != null) {
            EObject resolvedGenFeature=resourceSet.getEObject(genFeatureDescription.getEObjectURI(),true);
            if (resolvedGenFeature != null) {
              xReference.setOpposite((GenFeature)resolvedGenFeature);
            }
          }
        }
        if (!keys.isEmpty()) {
          IScope scope=scopeProvider.getScope(xReference,XcorePackage.Literals.XREFERENCE__KEYS);
          for (ListIterator<GenFeature> k=keys.listIterator(); k.hasNext(); ) {
            GenFeature key=k.next();
            IEObjectDescription genFeatureDescription=scope.getSingleElement(QualifiedName.create(key.getName()));
            if (genFeatureDescription != null) {
              EObject resolvedGenFeature=resourceSet.getEObject(genFeatureDescription.getEObjectURI(),true);
              if (resolvedGenFeature != null) {
                k.set((GenFeature)resolvedGenFeature);
              }
            }
          }
        }
      }
 else       if (eObject instanceof EPackage) {
        i.prune();
      }
    }
    for (    String qualifiedName : importManager.getImports()) {
      XImportDirective xImportDirective=XcoreFactory.eINSTANCE.createXImportDirective();
      xImportDirective.setImportedNamespace(qualifiedName);
      xPackage.getImportDirectives().add(xImportDirective);
    }
    final Map<Object,Object> options=new HashMap<Object,Object>();
    SaveOptions.newBuilder().format().noValidation().getOptions().addTo(options);
    Job job=new Job("String_Node_Str"){
      @Override protected IStatus run(      IProgressMonitor monitor){
        try {
          Map<URI,URI> copyiedURIMap=new HashMap<URI,URI>(uriMap);
          uriMap.clear();
          outputResource.save(options);
          uriMap.putAll(copyiedURIMap);
          outputResource.unload();
          outputResource.load(null);
          IParseResult parseResult=outputResource.getParseResult();
          if (parseResult != null) {
            IPreferenceValues configuration=preferencesProvider.getPreferenceValues(outputResource);
            try {
              List<TextReplacement> edits=formatter.format(outputResource,0,parseResult.getRootNode().getLength(),new FormattingPreferenceValues(configuration));
              BufferedInputStream bufferedInputStream=new BufferedInputStream(resourceSet.getURIConverter().createInputStream(outputResource.getURI()));
              byte[] input=new byte[bufferedInputStream.available()];
              bufferedInputStream.read(input);
              bufferedInputStream.close();
              String text=new String(input,outputResource.getEncoding());
              StringBuilder builder=new StringBuilder(text);
              for (int i=edits.size(); --i >= 0; ) {
                TextReplacement replacement=edits.get(i);
                builder.replace(replacement.getOffset(),replacement.getOffset() + replacement.getLength(),replacement.getText());
              }
              OutputStream outputStream=resourceSet.getURIConverter().createOutputStream(outputResource.getURI());
              outputStream.write(builder.toString().getBytes(outputResource.getEncoding()));
              outputStream.close();
            }
 catch (            Throwable throwable) {
              XcoreExporterPlugin.INSTANCE.log(throwable);
            }
          }
        }
 catch (        IOException exception) {
          return Status.CANCEL_STATUS;
        }
        return Status.OK_STATUS;
      }
    }
;
    job.schedule();
  }
  return Diagnostic.OK_INSTANCE;
}