org.eclipse.xtext.ReferencedMetamodel Java Examples

The following examples show how to use org.eclipse.xtext.ReferencedMetamodel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XtextGeneratorLanguage.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void validateReferencedMetamodel(final ReferencedMetamodel ref) {
  if (((ref.getEPackage() != null) && (!ref.getEPackage().eIsProxy()))) {
    return;
  }
  final EReference eref = XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE;
  final List<INode> nodes = NodeModelUtils.findNodesForFeature(ref, eref);
  String _xifexpression = null;
  boolean _isEmpty = nodes.isEmpty();
  if (_isEmpty) {
    _xifexpression = "(unknown)";
  } else {
    _xifexpression = NodeModelUtils.getTokenText(nodes.get(0));
  }
  final String refName = _xifexpression;
  final String grammarName = GrammarUtil.getGrammar(ref).getName();
  final String msg = ((((("The EPackage " + refName) + " in grammar ") + grammarName) + " could not be found. ") + "You might want to register that EPackage in your workflow file.");
  throw new IllegalStateException(msg);
}
 
Example #2
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private List<EObject> getPackage(ReferencedMetamodel context, ILeafNode text) {
	String nsUri = getMetamodelNsURI(text);
	if (nsUri == null)
		return Collections.emptyList();
	Grammar grammar = GrammarUtil.getGrammar(context);
	Set<Grammar> visitedGrammars = new HashSet<Grammar>();
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		List<EObject> result = getPackage(nsUri, usedGrammar, visitedGrammars);
		if (result != null)
			return result;
	}
	QualifiedName packageNsURI = QualifiedName.create(nsUri);
	EPackage pack = findPackageInScope(context, packageNsURI);
	if (pack == null) {
		pack = findPackageInAllDescriptions(context, packageNsURI);
		if (pack == null) {
			pack = loadEPackage(nsUri, context.eResource().getResourceSet());
		}
	}
	if (pack != null)
		return Collections.<EObject>singletonList(pack);
	return Collections.emptyList();
}
 
Example #3
Source File: XtextMetamodelReferenceHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static List<EObject> findReferencedMetamodelWithType(String typeName, List<AbstractMetamodelDeclaration> candidates) {
	AbstractMetamodelDeclaration result = null;
	for (AbstractMetamodelDeclaration metamodel : candidates) {
		if (metamodel instanceof ReferencedMetamodel) {
			EPackage pack = metamodel.getEPackage();
			if (pack != null) {
				final EClassifier classifier = pack.getEClassifier(typeName);
				if (classifier != null) {
					if (result == null)
						result = metamodel;
					else
						return Collections.emptyList();
				}
			}
		}
	}
	if (result != null)
		return Collections.<EObject>singletonList(result);
	return null;
}
 
Example #4
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void processMetamodelDeclarations(Collection<AbstractMetamodelDeclaration> declarations, ResourceSet resourceSet,
		Collection<Resource> resourcesToRemove, Collection<Resource> resourcesToUnload,
		Collection<Resource> referencedResources) {
	for (AbstractMetamodelDeclaration declaration : declarations) {
		EPackage pack = (EPackage) declaration.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
		if (pack != null && !pack.eIsProxy()) {
			Resource packResource = pack.eResource();
			if (packResource != null) {
				resourcesToRemove.add(packResource);
				if (declaration instanceof ReferencedMetamodel) {
						resourcesToUnload.add(packResource);
				}
				if (isPackageReferenced(resourceSet, pack, declarations)) {
					referencedResources.add(packResource);
				}
			}
		}
	}
}
 
Example #5
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkReferencedMetamodel(ReferencedMetamodel metamodel) throws ValueConverterException {
	if (metamodel.getEPackage() == null)
		return;
	String nsURI = metamodel.getEPackage().getNsURI();
	List<GeneratedMetamodel> allGeneratedMetamodels = getInheritedGeneratedMetamodels(metamodel);
	String text = getUsedUri(metamodel);
	for (GeneratedMetamodel generatedMetamodel : allGeneratedMetamodels) {
		EPackage generatedPackage = generatedMetamodel.getEPackage();
		if (generatedPackage != null && nsURI.equals((generatedPackage.getNsURI()))) {
			if (!text.equals(nsURI)) {
				addIssue("Metamodels that have been generated by a super grammar must be referenced by nsURI: " + nsURI,
						metamodel,
						XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE,
						INVALID_PACKAGE_REFERENCE_INHERITED,
						nsURI);
				return;
			}
			return;
		}
	}
	checkExternalPackage(metamodel, text);
}
 
Example #6
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isRuntime(ReferencedMetamodel metamodelReference) {
	ResourceSet resourceSet = metamodelReference.eResource().getResourceSet();
	if (resourceSet instanceof XtextResourceSet) {
		Object context = ((XtextResourceSet) resourceSet).getClasspathURIContext();
		boolean result = context == null || context instanceof Class || context instanceof ClassLoader;
		return result;
	}
	return false;
}
 
Example #7
Source File: GrammarUtilTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAllMetamodelDeclarations_03() throws Exception {
  this.with(XtextStandaloneSetup.class);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate g \'http://3\' as bar");
  _builder.newLine();
  _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as bar");
  _builder.newLine();
  _builder.append("startrule returns bar::startrule: name=ID;");
  _builder.newLine();
  String model = _builder.toString();
  Resource r = this.getResourceFromString(model);
  EObject _get = r.getContents().get(0);
  Grammar g = ((Grammar) _get);
  List<AbstractMetamodelDeclaration> decls = GrammarUtil.allMetamodelDeclarations(g);
  Assert.assertEquals(3, decls.size());
  AbstractMetamodelDeclaration decl = decls.get(0);
  Assert.assertTrue((decl instanceof GeneratedMetamodel));
  Assert.assertEquals("bar", decl.getAlias());
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
  decl = decls.get(1);
  Assert.assertTrue((decl instanceof ReferencedMetamodel));
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
  Assert.assertEquals("bar", decl.getAlias());
  decl = decls.get(2);
  Assert.assertTrue((decl instanceof ReferencedMetamodel));
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
  Assert.assertEquals("ecore", decl.getAlias());
  AbstractRule abstractRule = g.getRules().get(0);
  Assert.assertSame(decls.get(0), abstractRule.getType().getMetamodel());
}
 
Example #8
Source File: GrammarUtilTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAllMetamodelDeclarations_02() throws Exception {
  this.with(XtextStandaloneSetup.class);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as bar");
  _builder.newLine();
  _builder.append("generate g \'http://3\' as bar");
  _builder.newLine();
  _builder.append("startrule returns bar::startrule: name=ID;");
  _builder.newLine();
  String model = _builder.toString();
  Resource r = this.getResourceFromString(model);
  EObject _get = r.getContents().get(0);
  Grammar g = ((Grammar) _get);
  List<AbstractMetamodelDeclaration> decls = GrammarUtil.allMetamodelDeclarations(g);
  Assert.assertEquals(3, decls.size());
  AbstractMetamodelDeclaration decl = decls.get(0);
  Assert.assertTrue((decl instanceof ReferencedMetamodel));
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
  Assert.assertEquals("bar", decl.getAlias());
  decl = decls.get(1);
  Assert.assertEquals("bar", decl.getAlias());
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
  decl = decls.get(2);
  Assert.assertTrue((decl instanceof ReferencedMetamodel));
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
  Assert.assertEquals("ecore", decl.getAlias());
  AbstractRule abstractRule = g.getRules().get(0);
  Assert.assertSame(decls.get(1), abstractRule.getType().getMetamodel());
}
 
Example #9
Source File: GrammarUtilTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAllMetamodelDeclarations_01() throws Exception {
  this.with(XtextStandaloneSetup.class);
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore");
  _builder.newLine();
  _builder.append("generate g \'http://3\' as ecore");
  _builder.newLine();
  _builder.append("startrule returns ecore::startrule: name=ID;");
  _builder.newLine();
  String model = _builder.toString();
  Resource r = this.getResourceFromString(model);
  EObject _get = r.getContents().get(0);
  Grammar g = ((Grammar) _get);
  List<AbstractMetamodelDeclaration> decls = GrammarUtil.allMetamodelDeclarations(g);
  Assert.assertEquals(2, decls.size());
  AbstractMetamodelDeclaration decl = decls.get(0);
  Assert.assertTrue((decl instanceof ReferencedMetamodel));
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
  Assert.assertEquals("ecore", decl.getAlias());
  decl = decls.get(1);
  Assert.assertEquals("ecore", decl.getAlias());
  Assert.assertNotNull(decl.getEPackage());
  Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
}
 
Example #10
Source File: Xtext2EcoreTransformerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testTypesOfImplicitSuperGrammar() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("MyRule: myFeature=INT;");
  _builder.newLine();
  final String xtextGrammar = _builder.toString();
  EObject _model = this.getModel(xtextGrammar);
  final Grammar grammar = ((Grammar) _model);
  final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar);
  transformer.removeGeneratedPackages();
  transformer.transform();
  final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
  TypeRef type = rule.getType();
  Assert.assertNotNull(type);
  Assert.assertNotNull(transformer.getEClassifierInfos().getInfo(type));
  AbstractMetamodelDeclaration _get = GrammarUtil.allMetamodelDeclarations(grammar).get(1);
  final ReferencedMetamodel referenced = ((ReferencedMetamodel) _get);
  Assert.assertNotNull(referenced);
  Assert.assertEquals("ecore", referenced.getAlias());
  Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EString"));
  Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EInt"));
  EClassifierInfos parentInfos = IterableExtensions.<EClassifierInfos>head(transformer.getEClassifierInfos().getParents());
  Assert.assertNotNull(parentInfos.getInfo(referenced, "EString"));
  Assert.assertNotNull(parentInfos.getInfo(referenced, "EInt"));
}
 
Example #11
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBug_280413_01() throws Exception {
	XtextResource resource = getResourceFromStringAndExpect(
			"grammar org.foo.Bar with org.eclipse.xtext.testlanguages.SimpleExpressionsTestLanguage\n" +
			"import 'classpath:/org/eclipse/xtext/testlanguages/SimpleExpressionsTestLanguage.ecore' as mm\n" +
			"Atom returns mm::Atom: name = ID;", 1);
	assertEquals(resource.getErrors().toString(), 1, resource.getErrors().size());
	assertTrue(resource.getWarnings().toString(), resource.getWarnings().isEmpty());

	Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
	assertNotNull("diag", diag);
	assertEquals(diag.getChildren().toString(), 2, diag.getChildren().size());
	assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
	ReferencedMetamodel metamodel = (ReferencedMetamodel) diag.getChildren().get(0).getData().get(0);
	assertNotNull(metamodel);
}
 
Example #12
Source File: XtextGeneratorLanguage.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void validateAllImports(final Grammar grammar) {
  List<AbstractMetamodelDeclaration> _allMetamodelDeclarations = GrammarUtil.allMetamodelDeclarations(grammar);
  for (final AbstractMetamodelDeclaration amd : _allMetamodelDeclarations) {
    if ((amd instanceof ReferencedMetamodel)) {
      this.validateReferencedMetamodel(((ReferencedMetamodel)amd));
    }
  }
}
 
Example #13
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	if (ref == XtextPackage.eINSTANCE.getGrammar_UsedGrammars())
		return getUsedGrammar((Grammar) context, node);
	if (ref == XtextPackage.eINSTANCE.getTypeRef_Metamodel())
		return getLinkedMetaModel((TypeRef)context, ref, (ILeafNode) node);
	if (ref == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage() && context instanceof GeneratedMetamodel)
		return createPackage((GeneratedMetamodel) context, (ILeafNode) node);
	if (ref == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage() && context instanceof ReferencedMetamodel)
		return getPackage((ReferencedMetamodel)context, (ILeafNode) node);
	return super.getLinkedObjects(context, ref, node);
}
 
Example #14
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected List<GeneratedMetamodel> getInheritedGeneratedMetamodels(ReferencedMetamodel metamodel) {
	List<GeneratedMetamodel> allGeneratedMetamodels = new ArrayList<GeneratedMetamodel>();
	Grammar grammar = GrammarUtil.getGrammar(metamodel);
	Set<Grammar> visited = Sets.newHashSet();
	for (Grammar usedGrammar : grammar.getUsedGrammars())
		Iterables.addAll(allGeneratedMetamodels, getAllGeneratedMetamodels(usedGrammar, visited));
	if (allGeneratedMetamodels.isEmpty())
		return Collections.emptyList();
	return allGeneratedMetamodels;
}
 
Example #15
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String getUsedUri(ReferencedMetamodel metamodel) {
	List<INode> nodes = NodeModelUtils.findNodesForFeature(metamodel,
			XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE);
	if (nodes.size() != 1)
		throw new IllegalArgumentException();
	String text = nodes.get(0).getText();
	text = (String) valueConverter.toValue(text, "STRING", nodes.get(0));
	return text;
}
 
Example #16
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
TypeRef getTypeRef(String qualifiedName) {
	TypeRef result = XtextFactory.eINSTANCE.createTypeRef();
	String[] split = qualifiedName.split("::");
	String name = qualifiedName;
	if (split.length > 1) {
		result.setMetamodel(findMetamodel(grammar, split[0], split[1]));
		name = split[1];
	} else {
		result.setMetamodel(findDefaultMetamodel(grammar, qualifiedName));
	}
	if (result.getMetamodel() instanceof ReferencedMetamodel && result.getMetamodel().getEPackage() != null) {
		result.setClassifier(result.getMetamodel().getEPackage().getEClassifier(name));
	}
	return result;
}
 
Example #17
Source File: EcoreRefactoringParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected List<EObject> getRenamedElementsOrProxies(EObject originalTarget) {
	if (originalTarget instanceof ParserRule) {
		TypeRef returnType = ((ParserRule) originalTarget).getType();
		if (returnType != null && returnType.getClassifier() != null
				&& !Strings.isEmpty(returnType.getClassifier().getName())
				&& equal(((ParserRule) originalTarget).getName(), returnType.getClassifier().getName())
				&& returnType.getClassifier().eClass() != null && returnType.getClassifier().getEPackage() != null
				&& !Strings.isEmpty(returnType.getClassifier().getEPackage().getNsURI())) {
			String packageNsURI = returnType.getClassifier().getEPackage().getNsURI();
			QualifiedName classifierQualifiedName = QualifiedName.create(packageNsURI, returnType.getClassifier()
					.getName());
			URI platformResourceURI = findPlatformResourceURI(classifierQualifiedName, EcorePackage.Literals.ECLASS);
			if (platformResourceURI == null) {
				if (returnType.getMetamodel() instanceof ReferencedMetamodel)
					getStatus().add(ERROR, "Return type '{0}' is not indexed.", returnType.getClassifier());
			} else {
				EObject classifierProxy = EcoreFactory.eINSTANCE.create(returnType.getClassifier().eClass());
				((InternalEObject) classifierProxy).eSetProxyURI(platformResourceURI);
				if(!isDisableWarning)
					getStatus().add(WARNING, 
							"Renaming EClass '{0}' in ecore file. Please rerun the Ecore generator.", returnType.getClassifier());
				
				return singletonList(classifierProxy);
			}
		}
	}
	return null;
}
 
Example #18
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void completeParserRule(EObject model, final ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	final Grammar grammar = GrammarUtil.getGrammar(model);
	for (AbstractMetamodelDeclaration metamodelDeclaration : grammar.getMetamodelDeclarations()) {
		if (metamodelDeclaration instanceof ReferencedMetamodel) {
			ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
			EPackage ePackage = referencedMetamodel.getEPackage();
			if (ePackage != null) {
				for (EClassifier eClassifier : ePackage.getEClassifiers()) {
					if (isProposeParserRule(eClassifier, grammar)) {
						String proposal = eClassifier.getName();
						String metamodelAlias = referencedMetamodel.getAlias();
						if (metamodelAlias != null) {
							proposal = proposal + " returns " + metamodelAlias + "::" + eClassifier.getName();
						}
						proposal = proposal + ": \n;\n";
						ConfigurableCompletionProposal completionProposal = (ConfigurableCompletionProposal) createCompletionProposal(
								proposal, eClassifier.getName() + " - parser rule",
								getImage(XtextFactory.eINSTANCE.createParserRule()), context);
						if (completionProposal != null) {
							completionProposal.setCursorPosition(proposal.length() - 3);
							acceptor.accept(completionProposal);
						}
					}
				}
			}
		}
	}
}
 
Example #19
Source File: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
String text(ReferencedMetamodel object) {
	String label = "";
	if (object.getAlias() != null)
		label = " " + object.getAlias();
	if (object.getEPackage() != null) {
		if (label.length() == 0)
			label = " " + object.getEPackage().getName();
		else
			label = " " + object.getEPackage().getName() + " as" + label;
	}
	if (label.length() == 0) {
		label = " " + UNKNOWN;
	}
	return "import" + label;
}
 
Example #20
Source File: LanguageConfig.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected void validateReferencedMetamodel(ReferencedMetamodel ref) {
	if (ref.getEPackage() != null && !ref.getEPackage().eIsProxy())
		return;
	EReference eref = XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE;
	List<INode> nodes = NodeModelUtils.findNodesForFeature(ref, eref);
	String refName = nodes.isEmpty() ? "(unknown)" : NodeModelUtils.getTokenText(nodes.get(0));
	String grammarName = GrammarUtil.getGrammar(ref).getName();
	String msg = "The EPackage " + refName + " in grammar " + grammarName + " could not be found. ";
	msg += "You might want to register that EPackage in your workflow file.";
	throw new IllegalStateException(msg);
}
 
Example #21
Source File: LanguageConfig.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.3
 */
protected void validateAllImports(Grammar grammar) {
	for (AbstractMetamodelDeclaration amd : GrammarUtil.allMetamodelDeclarations(grammar))
		if (amd instanceof ReferencedMetamodel)
			validateReferencedMetamodel((ReferencedMetamodel) amd);
}
 
Example #22
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private EClassifierInfo createEClassifierInfo(TypeRef typeRef, String name) throws TransformationException {
	if (eClassifierInfos.getInfo(typeRef) != null)
		throw new IllegalArgumentException("Cannot create EClass for same type twice "
				+ typeRef.getClassifier().getName());
	//					+ GrammarUtil.getQualifiedName(typeRef));

	String classifierName = GrammarUtil.getTypeRefName(typeRef);
	if (classifierName == null)
		classifierName = name;
	if (classifierName == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot reference unnamed type.", typeRef);

	AbstractMetamodelDeclaration metaModel = typeRef.getMetamodel();
	if (metaModel == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create type for " + classifierName
				+ " because its EPackage is unknown.", typeRef);
	EPackage generatedEPackage = getGeneratedEPackage(metaModel);
	if (generatedEPackage == null) {
		throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel,
				"Cannot create type '" + classifierName + "' in alias " + typeRef.getMetamodel().getAlias(), typeRef);
	}

	EClassifier classifier = generatedEPackage.getEClassifier(classifierName);
	if (classifier == null) {
		if (GrammarUtil.containingParserRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEClass();
		} else if (GrammarUtil.containingEnumRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEEnum();
		} else {
			for (AbstractMetamodelDeclaration mmd : grammar.getMetamodelDeclarations()) {
				if (mmd instanceof ReferencedMetamodel && mmd.getEPackage() != null && mmd.getEPackage().getNsURI().equals(EcorePackage.eNS_URI)) {
					throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName, typeRef);
				}
			}
			throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName + ". If this is supposed to return EString, make sure you have imported '"+EcorePackage.eNS_URI+"'", typeRef);
		}
		classifier.setName(classifierName);
		generatedEPackage.getEClassifiers().add(classifier);
		typeRef.setClassifier(classifier);

		EClassifierInfo result;
		if (classifier instanceof EClass)
			result = EClassifierInfo.createEClassInfo((EClass) classifier, true, getGeneratedEPackageURIs(), GrammarUtil.getGrammar(typeRef));
		else // datatype or enum
			result = EClassifierInfo.createEDataTypeInfo((EDataType) classifier, true);

		if (!eClassifierInfos.addInfo(typeRef, result))
			throw new IllegalStateException("cannot add type for typeRef twice: '" + classifierName + "'");
		SourceAdapter.adapt(classifier, typeRef);
		return result;
	}
	typeRef.setClassifier(classifier);
	SourceAdapter.adapt(classifier, typeRef);
	return eClassifierInfos.getInfo(classifier);
}
 
Example #23
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private void deriveEnums(EnumRule rule) {
	EEnum returnType = (EEnum) rule.getType().getClassifier();
	if (returnType != null) {
		List<EnumLiteralDeclaration> decls = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
		for(EnumLiteralDeclaration decl : decls) {
			if (decl.getEnumLiteral() == null) {
				List<INode> nodes = NodeModelUtils.findNodesForFeature(decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL);
				if (!nodes.isEmpty()) {
					if (nodes.size() > 1)
						throw new IllegalStateException("Unexpected nodes found: " + nodes);
					INode node = nodes.get(0);
					String text = node.getText();
					EEnumLiteral literal = null;
					if (rule.getType().getMetamodel() instanceof ReferencedMetamodel) {
						literal = returnType.getEEnumLiteral(text);
					} else {
						EEnumLiteral existing = returnType.getEEnumLiteral(text);
						if (existing == null) {
							literal = EcoreFactory.eINSTANCE.createEEnumLiteral();
							int index = returnType.getELiterals().size();
							returnType.getELiterals().add(literal);
							literal.setName(text);
							literal.setValue(index);
							if (decl.getLiteral() != null) {
								literal.setLiteral(decl.getLiteral().getValue());
							} else {
								literal.setLiteral(text);
							}
						} else {
							literal = existing;
						}
						SourceAdapter.adapt(literal, decl);
					}
					if (literal == null) {
						reportError(new TransformationException(TransformationErrorCode.InvalidFeature, "Enum literal '" + text + "' does not exist.", decl));
					} else {
						decl.setEnumLiteral(literal);
					}
				}
			}
			if (decl.getLiteral() == null && decl.getEnumLiteral() != null) {
				Keyword kw = XtextFactory.eINSTANCE.createKeyword();
				kw.setValue(decl.getEnumLiteral().getLiteral());
				decl.setLiteral(kw);
			}
		}
	}
}
 
Example #24
Source File: XtextDescriptionLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public String image(ReferencedMetamodel metamodel) {
	return "import.gif"; //$NON-NLS-1$
}
 
Example #25
Source File: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
String image(ReferencedMetamodel metamodel) {
	return "import.gif";
}
 
Example #26
Source File: XtextSemanticSequencer.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Contexts:
 *     AbstractMetamodelDeclaration returns ReferencedMetamodel
 *     ReferencedMetamodel returns ReferencedMetamodel
 *
 * Constraint:
 *     (ePackage=[EPackage|STRING] alias=ValidID?)
 */
protected void sequence_ReferencedMetamodel(ISerializationContext context, ReferencedMetamodel semanticObject) {
	genericSequencer.createSequence(context, semanticObject);
}