use of org.eclipse.emf.codegen.ecore.genmodel.GenPackage in project xtext-core by eclipse.
the class EMFGeneratorFragment2 method getGenPackagesForPackages.
protected List<GenPackage> getGenPackagesForPackages(final GenModel existingGenModel, final Collection<EPackage> packs) {
final ArrayList<GenPackage> result = CollectionLiterals.<GenPackage>newArrayList();
for (final EPackage pkg : packs) {
final Function1<GenPackage, Boolean> _function = (GenPackage it) -> {
EPackage _ecorePackage = it.getEcorePackage();
String _nsURI = null;
if (_ecorePackage != null) {
_nsURI = _ecorePackage.getNsURI();
}
String _nsURI_1 = pkg.getNsURI();
return Boolean.valueOf(Objects.equal(_nsURI, _nsURI_1));
};
boolean _exists = IterableExtensions.<GenPackage>exists(existingGenModel.getGenPackages(), _function);
boolean _not = (!_exists);
if (_not) {
result.add(GenModelUtil2.getGenPackage(pkg, existingGenModel.eResource().getResourceSet()));
}
}
final Comparator<GenPackage> _function_1 = (GenPackage o1, GenPackage o2) -> {
return EcoreUtil.getURI(o1).toString().compareTo(EcoreUtil.getURI(o2).toString());
};
Collections.<GenPackage>sort(result, _function_1);
return result;
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenPackage in project xtext-core by eclipse.
the class EMFGeneratorFragment2 method findGenPackageByNsURI.
private GenPackage findGenPackageByNsURI(final GenModel genModel, final String nsURI) {
final List<GenPackage> allGenPackages = genModel.getAllGenUsedAndStaticGenPackagesWithClassifiers();
for (final GenPackage genPackage : allGenPackages) {
{
final EPackage ecorePackage = genPackage.getEcorePackage();
if (((ecorePackage == null) || ecorePackage.eIsProxy())) {
URI _uRI = genModel.eResource().getURI();
String _plus = ((("Unresolved proxy: " + ecorePackage) + " in ") + _uRI);
throw new RuntimeException(_plus);
}
String _nsURI = ecorePackage.getNsURI();
boolean _equals = Objects.equal(nsURI, _nsURI);
if (_equals) {
return genPackage;
}
}
}
URI _uRI = genModel.eResource().getURI();
String _plus = ((("No GenPackage for NsURI " + nsURI) + " found in ") + _uRI);
throw new RuntimeException(_plus);
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenPackage in project xtext-core by eclipse.
the class EMFGeneratorFragment2 method generate.
@Override
public void generate() {
boolean _isEmpty = IterableExtensions.isEmpty(Iterables.<GeneratedMetamodel>filter(this.getGrammar().getMetamodelDeclarations(), GeneratedMetamodel.class));
if (_isEmpty) {
return;
}
try {
final Grammar clonedGrammar = this.cloneGrammarIntoNewResourceSet(this.getGrammar());
final ResourceSet workingResourceSet = clonedGrammar.eResource().getResourceSet();
final Function1<GeneratedMetamodel, EPackage> _function = (GeneratedMetamodel it) -> {
return it.getEPackage();
};
final List<EPackage> generatedPackages = IterableExtensions.<EPackage>toList(IterableExtensions.<GeneratedMetamodel, EPackage>map(Iterables.<GeneratedMetamodel>filter(clonedGrammar.getMetamodelDeclarations(), GeneratedMetamodel.class), _function));
if ((this.genModel != null)) {
this.registerUsedGenModel(workingResourceSet.getURIConverter(), clonedGrammar);
}
boolean _isEmpty_1 = generatedPackages.isEmpty();
boolean _not = (!_isEmpty_1);
if (_not) {
final Map<String, EPackage> usedEPackages = this.findAllUsedEPackages(generatedPackages);
final Map<String, EPackage> loadedEPackages = this.findEPackagesInGenPackages(usedEPackages.keySet(), workingResourceSet);
final Map<EObject, EObject> eNamedElementMapping = this.createENamedElementMapping(usedEPackages, loadedEPackages);
this.replaceReferencesInGeneratedPackages(generatedPackages, eNamedElementMapping);
final Resource ePackageResource = this.createResourceForEPackages(clonedGrammar, generatedPackages, workingResourceSet);
if ((!this.skipGenerate)) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Generating EMF model");
{
if (this.generateEdit) {
{
if (this.generateEditor) {
_builder.append(", edit");
} else {
_builder.append(" and edit");
}
}
}
}
{
if (this.generateEditor) {
_builder.append(" and editor");
}
}
_builder.append(" code");
EMFGeneratorFragment2.LOG.info(_builder);
final GenModel genModel = this.getSaveAndReconcileGenModel(clonedGrammar, generatedPackages, workingResourceSet);
genModel.reconcile();
this.doGenerate(genModel);
this.addProjectContributions(clonedGrammar, generatedPackages, workingResourceSet);
if (this.bindEPackageAndEFactory) {
for (final EPackage pkg : generatedPackages) {
{
final GenPackage genPkg = GenModelUtil2.getGenPackage(pkg, genModel.eResource().getResourceSet());
GuiceModuleAccess.BindingFactory _bindingFactory = new GuiceModuleAccess.BindingFactory();
TypeReference _typeRef = TypeReference.typeRef(genPkg.getQualifiedPackageInterfaceName());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
String _packageInterfaceName = genPkg.getPackageInterfaceName();
_builder.append(_packageInterfaceName);
_builder.append(".eINSTANCE");
}
};
GuiceModuleAccess.BindingFactory _addTypeToInstance = _bindingFactory.addTypeToInstance(_typeRef, _client);
TypeReference _typeRef_1 = TypeReference.typeRef(genPkg.getQualifiedFactoryInterfaceName());
StringConcatenationClient _client_1 = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
String _factoryInterfaceName = genPkg.getFactoryInterfaceName();
_builder.append(_factoryInterfaceName);
_builder.append(".eINSTANCE");
}
};
_addTypeToInstance.addTypeToInstance(_typeRef_1, _client_1).contributeTo(this.getLanguage().getRuntimeGenModule());
}
}
}
}
this.saveResource(ePackageResource);
}
this.updateBuildProperties();
} catch (final Throwable _t) {
if (_t instanceof Exception) {
final Exception e = (Exception) _t;
EMFGeneratorFragment2.LOG.error("Failed to execute EMF generator", e);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenPackage in project xtext-core by eclipse.
the class AbstractPortableURIsTest method doTestResource.
protected void doTestResource(String platformPath, String... packageNames) {
Resource resource = resourceSet.getResource(URI.createPlatformPluginURI(platformPath, false), true);
assertNotNull(resource);
assertEquals(1, resource.getContents().size());
EObject obj = resource.getContents().get(0);
if (obj instanceof EPackage) {
assertEquals(packageNames[0], ((EPackage) obj).getName());
} else if (obj instanceof GenModel) {
GenModel model = (GenModel) obj;
List<GenPackage> packages = Lists.newArrayList(model.getGenPackages());
assertEquals(packageNames.length, packages.size());
ListExtensions.sortInplaceBy(packages, new Functions.Function1<GenPackage, String>() {
@Override
public String apply(GenPackage p) {
return p.getEcorePackage().getName();
}
});
List<String> packageNamesList = Arrays.asList(packageNames);
Collections.sort(packageNamesList);
for (int i = 0; i < packageNamesList.size(); i++) {
assertEquals(packageNamesList.get(i), packages.get(i).getEcorePackage().getName());
}
IStatus status = model.validate();
assertTrue(printLeafs(status), status.isOK());
EObject orig = EcoreUtil.copy(obj);
((GenModel) obj).reconcile();
if (!EcoreUtil.equals(orig, obj)) {
Predicate<EStructuralFeature> ignoreContainer = new Predicate<EStructuralFeature>() {
@Override
public boolean apply(EStructuralFeature f) {
if (f instanceof EReference) {
EReference casted = (EReference) f;
return !casted.isContainment();
}
return false;
}
};
String origAsString = EmfFormatter.objToStr(orig, ignoreContainer);
String newAsString = EmfFormatter.objToStr(obj, ignoreContainer);
throw new ComparisonFailure("Reconcile changed model", origAsString, newAsString);
}
} else {
fail("Unexpected root element type: " + obj.eClass().getName());
}
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenPackage in project xtext-core by eclipse.
the class GenModelUtil2 method getGenPackage.
public static GenPackage getGenPackage(final EPackage pkg, final ResourceSet resourceSet) {
final String nsURI = pkg.getNsURI();
String location = null;
Resource _eResource = pkg.eResource();
URI _uRI = null;
if (_eResource != null) {
_uRI = _eResource.getURI();
}
boolean _tripleNotEquals = (_uRI != null);
if (_tripleNotEquals) {
location = pkg.eResource().getURI().toString();
}
final Resource genModelResource = GenModelUtil2.getGenModelResource(location, nsURI, resourceSet);
if ((genModelResource != null)) {
EList<EObject> _contents = genModelResource.getContents();
for (final EObject model : _contents) {
if ((model instanceof GenModel)) {
final GenPackage genPkg = ((GenModel) model).findGenPackage(pkg);
if ((genPkg != null)) {
genPkg.getEcorePackage().getEClassifiers();
return genPkg;
}
}
}
StringConcatenation _builder = new StringConcatenation();
_builder.append("No GenPackage for NsURI ");
_builder.append(nsURI);
_builder.append(" found in ");
URI _uRI_1 = genModelResource.getURI();
_builder.append(_uRI_1);
throw new RuntimeException(_builder.toString());
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("No GenPackage for NsURI ");
_builder_1.append(nsURI);
_builder_1.append(".");
throw new RuntimeException(_builder_1.toString());
}
Aggregations