use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-xtend by eclipse.
the class URIsInEcoreFilesXtendTest 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.GenModel in project xtext-core by eclipse.
the class GenModelUtil2 method getGenModelResource.
public static Resource getGenModelResource(final String locationInfo, final String nsURI, final ResourceSet resourceSet) {
final URI genModelURI = EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).get(nsURI);
if ((genModelURI == null)) {
boolean _equals = Objects.equal(EcorePackage.eNS_URI, nsURI);
if (_equals) {
return null;
}
EList<Resource> _resources = resourceSet.getResources();
for (final Resource res : _resources) {
EList<EObject> _contents = res.getContents();
for (final EObject obj : _contents) {
if ((obj instanceof GenModel)) {
EList<GenPackage> _genPackages = ((GenModel) obj).getGenPackages();
for (final GenPackage genPackage : _genPackages) {
String _nSURI = genPackage.getNSURI();
boolean _equals_1 = Objects.equal(_nSURI, nsURI);
if (_equals_1) {
return genPackage.eResource();
}
}
}
}
}
StringConcatenation _builder = new StringConcatenation();
_builder.append("Could not find a GenModel for EPackage \'");
_builder.append(nsURI);
_builder.append("\'");
{
boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(locationInfo);
boolean _not = (!_isNullOrEmpty);
if (_not) {
_builder.append(" from ");
_builder.append(locationInfo);
}
}
_builder.append(".");
_builder.newLineIfNotEmpty();
_builder.append("If the missing GenModel has been generated via ");
String _simpleName = EMFGeneratorFragment2.class.getSimpleName();
_builder.append(_simpleName);
_builder.append(", make sure to run it first in the workflow.");
_builder.newLineIfNotEmpty();
_builder.append("If you have a *.genmodel-file, make sure to register it via StandaloneSetup.registerGenModelFile(String).");
_builder.newLine();
throw new RuntimeException(_builder.toString());
}
if ((resourceSet == null)) {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("There is no ResourceSet for EPackage \'");
_builder_1.append(nsURI);
_builder_1.append("\'. Please make sure the EPackage has been loaded from a .ecore file and not from the generated Java file.");
throw new RuntimeException(_builder_1.toString());
}
final Resource genModelResource = resourceSet.getResource(genModelURI, true);
if ((genModelResource == null)) {
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("Error loading GenModel ");
_builder_2.append(genModelURI);
throw new RuntimeException(_builder_2.toString());
}
EList<EObject> _contents_1 = genModelResource.getContents();
for (final EObject content : _contents_1) {
if ((content instanceof GenModel)) {
((GenModel) content).reconcile();
}
}
return genModelResource;
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.
the class EMFGeneratorFragment2 method getSaveAndReconcileGenModel.
protected GenModel getSaveAndReconcileGenModel(final Grammar grammar, final List<EPackage> packs, final ResourceSet rs) {
final GenModel genModel = this.getGenModel(rs, grammar);
genModel.initialize(packs);
EList<GenPackage> _genPackages = genModel.getGenPackages();
for (final GenPackage genPackage : _genPackages) {
{
genPackage.setBasePackage(this.getBasePackage(grammar));
if (this.suppressLoadInitialization) {
genPackage.setLoadInitialization(false);
}
if (((!this.getLanguage().getFileExtensions().isEmpty()) && packs.contains(genPackage.getEcorePackage()))) {
genPackage.setFileExtensions(IterableExtensions.join(this.getLanguage().getFileExtensions(), ","));
}
}
}
final Set<EPackage> referencedEPackages = this.getReferencedEPackages(packs);
final List<GenPackage> usedGenPackages = this.getGenPackagesForPackages(genModel, referencedEPackages);
this.reconcileMissingGenPackagesInUsedModels(usedGenPackages);
genModel.getUsedGenPackages().addAll(usedGenPackages);
this.saveResource(genModel.eResource());
new GenModelHelper().registerGenModel(genModel);
return genModel;
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.
the class EMFGeneratorFragment2 method getGenModel.
protected GenModel getGenModel(final ResourceSet rs, final Grammar grammar) {
try {
final URI genModelUri = this.getGenModelUri(grammar);
final Resource resource = rs.getResource(genModelUri, false);
if ((resource != null)) {
resource.unload();
rs.getResources().remove(resource);
}
final Resource genModelFile = rs.createResource(genModelUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
GenModel genModel = null;
boolean _exists = rs.getURIConverter().exists(genModelUri, null);
if (_exists) {
genModelFile.load(null);
boolean _hasFragment = genModelUri.hasFragment();
if (_hasFragment) {
EObject _eObject = genModelFile.getEObject(genModelUri.fragment());
genModel = ((GenModel) _eObject);
} else {
EObject _head = IterableExtensions.<EObject>head(genModelFile.getContents());
genModel = ((GenModel) _head);
}
} else {
genModel = new GenModelImpl() {
@Override
public GenPackage createGenPackage() {
return new GenPackageImpl() {
@Override
public String getSerializedPackageFilename() {
String _name = this.getName();
return (_name + ".loadinitialization_ecore");
}
};
}
};
genModel.setModelName(this.getModelName(grammar));
genModel.setModelPluginID(this.getModelPluginID());
genModel.setModelDirectory(this.getJavaModelDirectory());
if (this.generateEdit) {
genModel.setEditPluginID(this.getEditPluginID());
genModel.setEditDirectory(this.getEditDirectory());
}
if (this.generateEditor) {
genModel.setEditorPluginID(this.getEditorPluginID());
genModel.setEditorDirectory(this.getEditorDirectory());
}
genModel.setValidateModel(false);
genModel.setForceOverwrite(true);
genModel.setFacadeHelperClass(null);
genModel.setBundleManifest(true);
genModel.setUpdateClasspath(false);
genModel.setComplianceLevel(this.jdkLevel);
genModel.setRuntimeVersion(this.emfRuntimeVersion);
genModel.setRootExtendsClass("org.eclipse.emf.ecore.impl.MinimalEObjectImpl$Container");
genModel.setLineDelimiter(this.codeConfig.getLineDelimiter());
String _fileHeader = this.codeConfig.getFileHeader();
boolean _tripleNotEquals = (_fileHeader != null);
if (_tripleNotEquals) {
genModel.setCopyrightText(EMFGeneratorFragment2.trimMultiLineComment(this.codeConfig.getFileHeader()));
}
}
genModelFile.getContents().add(genModel);
return genModel;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.emf.codegen.ecore.genmodel.GenModel 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);
}
}
}
Aggregations