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

The following examples show how to use org.eclipse.xtext.TypeRef#setClassifier() . 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: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testCheckRuleCallInUnorderedGroup_05() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	typeRef.setClassifier(EcorePackage.Literals.EOBJECT);
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setType(typeRef);
	ruleCall.setRule(parserRule);
	Assignment assignment = XtextFactory.eINSTANCE.createAssignment();
	assignment.setTerminal(ruleCall);
	unorderedGroup.getElements().add(assignment);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 2
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
TypeRef getTypeRef(EClassifier classifier) {
	TypeRef result = XtextFactory.eINSTANCE.createTypeRef();
	result.setClassifier(classifier);
	EPackage pack = classifier.getEPackage();
	for(AbstractMetamodelDeclaration decl: GrammarUtil.allMetamodelDeclarations(grammar)) {
		if (pack.equals(decl.getEPackage())) {
			result.setMetamodel(decl);
			return result;
		}
	}
	return result;
}
 
Example 3
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 4
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 5
Source File: FlattenedGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private TypeRef copyTypeRef(TypeRef ref) {
	if (ref == null)
		return null;
	TypeRef copy = copy(ref);
	copy.setClassifier(ref.getClassifier());
	return copy;
}
 
Example 6
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCheckCrossReferenceTerminal_01() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	CrossReference reference = XtextFactory.eINSTANCE.createCrossReference();
	RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
	reference.setTerminal(call);
	ParserRule rule = XtextFactory.eINSTANCE.createParserRule();
	call.setRule(rule);
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	rule.setType(typeRef);
	typeRef.setClassifier(EcorePackage.Literals.ESTRING);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkCrossReferenceTerminal(reference);
	messageAcceptor.validate();
}
 
Example 7
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCheckCrossReferenceTerminal_02() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	CrossReference reference = XtextFactory.eINSTANCE.createCrossReference();
	RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
	reference.setTerminal(call);
	ParserRule rule = XtextFactory.eINSTANCE.createParserRule();
	call.setRule(rule);
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	rule.setType(typeRef);
	typeRef.setClassifier(EcorePackage.Literals.EINT);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(call, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkCrossReferenceTerminal(reference);
	messageAcceptor.validate();
}
 
Example 8
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCheckRuleCallInUnorderedGroup_01() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	typeRef.setClassifier(EcorePackage.Literals.EOBJECT);
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setType(typeRef);
	ruleCall.setRule(parserRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(ruleCall, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 9
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCheckRuleCallInUnorderedGroup_02() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	typeRef.setClassifier(EcorePackage.Literals.EBIG_DECIMAL);
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setType(typeRef);
	ruleCall.setRule(parserRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 10
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBug318424_01() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	CrossReference reference = XtextFactory.eINSTANCE.createCrossReference();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	reference.setType(typeRef);
	typeRef.setClassifier(EcorePackage.Literals.EBOOLEAN);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(typeRef, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkCrossReferenceType(reference);
	messageAcceptor.validate();
}
 
Example 11
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBug318424_02() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	Action action = XtextFactory.eINSTANCE.createAction();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	action.setType(typeRef);
	typeRef.setClassifier(EcorePackage.Literals.EBOOLEAN);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(typeRef, true, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkInstantiatedType(action);
	messageAcceptor.validate();
}
 
Example 12
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);
}