Java Code Examples for org.eclipse.xtext.TypeRef#getMetamodel()

The following examples show how to use org.eclipse.xtext.TypeRef#getMetamodel() . 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: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private TypeRef getOrComputeReturnType(AbstractRule rule) {
	TypeRef result = rule.getType();
	if (result == null) {
		EClassifier classifier = getClassifierFor(rule);
		if (classifier == null) {
			if (rule.getName() == null)
				return null;
			result = getTypeRef(rule.getName());
		} else
			result = getTypeRef(classifier);
		if (result.getMetamodel() == null) {
			AbstractMetamodelDeclaration bestMatch = null;
			for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
				if (decl instanceof GeneratedMetamodel && Strings.isEmpty(decl.getAlias())) {
					bestMatch = decl;
					break;
				}
			}
			if (result.getMetamodel() == null)
				result.setMetamodel(bestMatch);
		}
		rule.setType(result);
	}
	return result;
}
 
Example 2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private EClassifierInfo findOrCreateEClassifierInfo(TypeRef typeRef, String name, boolean createIfMissing) throws TransformationException {
	if (typeRef.getClassifier() != null && typeRef.getMetamodel() == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias,
				"Cannot find EPackage for type '" + typeRef.getClassifier().getName() + "'", typeRef);
	EClassifierInfo info = eClassifierInfos.getInfo(typeRef);
	if (info == null) {
		// we assumend EString for terminal rules and datatype rules, so
		// we have to do a look up in super grammar
		EDataType dataType = GrammarUtil.findEString(GrammarUtil.getGrammar(typeRef));
		if (dataType != null && typeRef.getClassifier() == dataType) {
			info = eClassifierInfos.getInfoOrNull(typeRef);
			if (info != null)
				return info;
		}
		if (createIfMissing)
			info = createEClassifierInfo(typeRef, name);
	}
	return info;
}
 
Example 3
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 4
Source File: XtextRenameStrategyProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void renameReturnType(URI targetElementURI, String newName, ResourceSet resourceSet) {
	AbstractRule rule = (AbstractRule) resourceSet.getEObject(targetElementURI, false);
	TypeRef parserRuleType = rule.getType();
	if (rule.getName().equals(parserRuleType.getClassifier().getName())
			&& parserRuleType.getMetamodel() instanceof GeneratedMetamodel) {
		parserRuleType.getClassifier().setName(newName);
	}
}
 
Example 5
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 6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private EClassifierInfo findOrCreateEClassifierInfo(AbstractRule rule) throws TransformationException {
	if (isWildcardFragment(rule)) {
		return null;
	}
	final TypeRef typeRef = getOrComputeReturnType(rule);
	if (typeRef == null) {
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create type for unnamed rule.", rule);
	}
	if (typeRef.getMetamodel() != null && typeRef.getMetamodel().getEPackage() == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create type without declared package.", typeRef);
	return findOrCreateEClassifierInfo(typeRef, rule.getName(), grammar.getRules().contains(rule));
}
 
Example 7
Source File: ElementTypeCalculator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EClassifier caseTypeRef(TypeRef object) {
	if (object.getClassifier() == null) {
		if (object.getMetamodel() == null || object.getMetamodel().getEPackage() == null)
			return null;
		String name = GrammarUtil.getTypeRefName(object);
		if (Strings.isEmpty(name))
			return null;
		EClassifierInfo info = classifierInfos.getInfo(object.getMetamodel(), name);
		if (info != null)
			object.setClassifier(info.getEClassifier());
	}
	return object.getClassifier();
}
 
Example 8
Source File: XtextLinker.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void beforeEnsureIsLinked(EObject obj, EReference ref, IDiagnosticProducer producer) {
	if (XtextPackage.eINSTANCE.getTypeRef_Classifier() == ref) {
		final TypeRef typeRef = (TypeRef) obj;
		if (typeRef.getMetamodel() == null)
			setDefaultValue(obj, XtextPackage.eINSTANCE.getTypeRef_Metamodel(), producer);
	} else
		super.beforeEnsureIsLinked(obj, ref, producer);
}
 
Example 9
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 10
Source File: EClassifierInfos.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public boolean addInfo(TypeRef typeRef, EClassifierInfo metatypeInfo) {
	if (typeRef.getMetamodel() == null || typeRef.getClassifier() == null)
		throw new NullPointerException();
	return addInfo(typeRef.getMetamodel(), typeRef.getClassifier().getName(), metatypeInfo);
}
 
Example 11
Source File: XtextRuleInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected String getTypeRefName(TypeRef typeRef) {
	String simpleName = GrammarUtil.getTypeRefName(typeRef);
	if (typeRef.getMetamodel() != null && !Strings.isEmpty(typeRef.getMetamodel().getAlias()))
		return typeRef.getMetamodel().getAlias() + "::" + simpleName;
	return simpleName;
}