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

The following examples show how to use org.eclipse.xtext.TypeRef#getClassifier() . 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: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private String getClassifierName(TypeRef ref) {
	String classifierName = UNKNOWN;
	if (ref != null) {
		if (ref.getClassifier() != null)
			classifierName = ref.getClassifier().getName();
		else {
			ICompositeNode node = NodeModelUtils.getNode(ref);
			if (node != null) {
				List<ILeafNode> leafs = Lists.newArrayList(node.getLeafNodes());
				for (int i = leafs.size() - 1; i >= 0; i--) {
					if (!leafs.get(i).isHidden()) {
						classifierName = leafs.get(i).getText();
						break;
					}
				}
			}
		}
	}
	return classifierName;
}
 
Example 2
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Not a full featured solution for the computation of available structural features, but it is sufficient for some
 * interesting 85%.
 */
@Override
public void completeAssignment_Feature(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	AbstractRule rule = EcoreUtil2.getContainerOfType(model, AbstractRule.class);
	TypeRef typeRef = rule.getType();
	if (typeRef != null && typeRef.getClassifier() instanceof EClass) {
		Iterable<EStructuralFeature> features = ((EClass) typeRef.getClassifier()).getEAllStructuralFeatures();
		Function<IEObjectDescription, ICompletionProposal> factory = getProposalFactory(grammarAccess.getValidIDRule().getName(), context);
		Iterable<String> processedFeatures = completeStructuralFeatures(context, factory, acceptor, features);
		if(rule.getType().getMetamodel() instanceof GeneratedMetamodel) {
			if(notNull(rule.getName()).toLowerCase().startsWith("import")) {
				completeSpecialAttributeAssignment("importedNamespace", 2, processedFeatures, factory, context, acceptor); 
				completeSpecialAttributeAssignment("importURI", 1, processedFeatures, factory, context, acceptor); 
			} else {
				completeSpecialAttributeAssignment("name", 3, processedFeatures, factory, context, acceptor); 
			}
		}
	}
	super.completeAssignment_Feature(model, assignment, context, acceptor);
}
 
Example 3
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void addSuperType(ParserRule rule, TypeRef subTypeRef, EClassifierInfo superTypeInfo) throws TransformationException {
	final EClassifier subType = subTypeRef.getClassifier();
	final EClassifierInfo subTypeInfo = subType == null
	        ? findOrCreateEClassifierInfo(subTypeRef, null, true)
			: eClassifierInfos.getInfoOrNull(subType);
	if (subTypeInfo == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Type '"
				+ superTypeInfo.getEClassifier().getName() + "' is not available.", rule.getType());
	if (superTypeInfo.isAssignableFrom(subTypeInfo))
		return;
	if (subTypeInfo.getEClassifier() instanceof EDataType)
		throw new TransformationException(TransformationErrorCode.InvalidSupertype, "Cannot add supertype '"
				+ superTypeInfo.getEClassifier().getName() + "' to simple datatype '"
				+ subTypeInfo.getEClassifier().getName() + "'.", rule.getType());
	if (!subTypeInfo.isGenerated())
		throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel,
				"Cannot add supertype '" + superTypeInfo.getEClassifier().getName() + "' to sealed type '"
						+ subTypeInfo.getEClassifier().getName() + "'.", rule.getType());
	subTypeInfo.addSupertype(superTypeInfo);
}
 
Example 4
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 5
Source File: EClassifierInfos.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public EClassifierInfo getInfo(TypeRef typeRef) {
	if (typeRef.getClassifier() == null)
		return null;
	EClassifierInfo result = getInfo(typeRef.getMetamodel(), typeRef.getClassifier().getName());
	if (result == null) {
		Grammar declaringGrammar = GrammarUtil.getGrammar(typeRef);
		if (grammar.equals(declaringGrammar))
			return result;
		for(EClassifierInfos parent: parents) {
			result = parent.getInfo(typeRef);
			if (result != null)
				return result;
		}
	}
	return result;
}
 
Example 6
Source File: InheritanceTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testMetamodel() throws Exception {
	AbstractRule rule = GrammarUtil.findRuleForName(getGrammarAccess().getGrammar(), "OverridableParserRule2");
	assertNotNull("rule", rule);
	TypeRef ref = rule.getType();
	assertNotNull("ref", ref);
	final EClass clazz = (EClass) ref.getClassifier();
	assertNotNull("class", clazz);
	assertEquals("AType2", clazz.getName());
	assertEquals(2, clazz.getESuperTypes().size());
	Set<String> expectedNames = new HashSet<String>(Arrays.asList(new String[]{"AType", "RootRule"}));
	Iterator<String> iter = Iterables.transform(clazz.getESuperTypes(), new Function<EClass, String>(){
		@Override
		public String apply(EClass param) {
			return param.getName();
		}
	}).iterator();
	while(iter.hasNext()) {
		String name = iter.next();
		assertTrue("name = '" + name + "'", expectedNames.remove(name));
	}
	assertTrue(expectedNames.toString(), expectedNames.isEmpty());
}
 
Example 7
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 8
Source File: DatatypeRuleUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseParserRule(ParserRule object) {
	if (visitedRules.isEmpty()) {
		visitedRules.add(object);
		return object.getAlternatives() != null && doSwitch(object.getAlternatives());
	}
	final TypeRef typeRef = object.getType();
	if (typeRef == null || typeRef.getClassifier() == null) {
		throw new IllegalStateException("checks are only allowed for linked grammars. Rule: " + object.getName());
	}
	if (!visitedRules.add(object))
		return true;
	Boolean result = GrammarUtil.isDatatypeRule(object);
	return result; 
}
 
Example 9
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 10
Source File: CurrentTypeFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Boolean caseRuleCall(RuleCall object) {
	EClassifier wasType = currentType;
	AbstractRule calledRule = object.getRule();
	if (currentType == null) {
		if (calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
			ParserRule parserRule = (ParserRule) calledRule;
			if (parserRule.isFragment()) {
				if (context.getType() != null)
					currentType = context.getType().getClassifier();
				if (!parserRule.isWildcard()) {
					doSwitch(parserRule);
				}
			} else {
				TypeRef returnType = calledRule.getType();
				if (returnType != null) {
					currentType = returnType.getClassifier();
				}
			}
		}
	} else if (isFragmentButNotWildcard(calledRule)) {
		doSwitch(calledRule);
	}
	if (object == stopElement)
		return true;
	if (GrammarUtil.isOptionalCardinality(object))
		currentType = getCompatibleType(currentType, wasType, object);
	return false;
}
 
Example 11
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void checkTypeIsEClass(TypeRef type) {
	if (type != null) {
		EClassifier classifier = type.getClassifier();
		if (classifier != null && !classifier.eIsProxy()) {
			if (!(classifier instanceof EClass)) {
				error(
						"Type of cross reference must be an EClass.", 
						type, 
						null,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						null);
			}
		}
	}
}
 
Example 12
Source File: NodeModelSemanticSequencer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void createSequence(ISerializationContext context, EObject semanticObject) {
	SemanticNodeIterator ni = new SemanticNodeIterator(semanticObject);
	while (ni.hasNext()) {
		Triple<INode, AbstractElement, EObject> node = ni.next();
		if (node.getSecond() instanceof RuleCall) {
			RuleCall rc = (RuleCall) node.getSecond();
			TypeRef ruleType = rc.getRule().getType();
			if (ruleType == null || ruleType.getClassifier() instanceof EClass)
				acceptSemantic(semanticObject, rc, node.getThird(), node.getFirst());
			else if (GrammarUtil.containingCrossReference(node.getSecond()) != null) {
				EStructuralFeature feature = FeatureFinderUtil.getFeature(node.getSecond(),
						semanticObject.eClass());
				acceptSemantic(semanticObject, rc, semanticObject.eGet(feature), node.getFirst());
			} else {
				String strVal = NodeModelUtils.getTokenText(node.getFirst());
				Object val = valueConverter.toValue(strVal, ruleNames.getQualifiedName(rc.getRule()),
						node.getFirst());
				acceptSemantic(semanticObject, rc, val, node.getFirst());
			}
		} else if (node.getSecond() instanceof Keyword)
			acceptSemantic(semanticObject, node.getSecond(), node.getFirst().getText(), node.getFirst());
		else if (node.getSecond() instanceof Action) {
			acceptSemantic(semanticObject, node.getSecond(), semanticObject, node.getFirst());
		}
	}
}
 
Example 13
Source File: ContextTypePDAProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected EClass getInstantiatedType(AbstractElement element) {
	TypeRef type = null;
	if (GrammarUtil.isAssigned(element) || GrammarUtil.isEObjectFragmentRuleCall(element)) {
		type = GrammarUtil.containingRule(element).getType();
	} else if (element instanceof Action) {
		type = ((Action) element).getType();
	}
	if (type != null) {
		EClassifier classifier = type.getClassifier();
		if (classifier instanceof EClass && !classifier.eIsProxy()) {
			return (EClass) classifier;
		}
	}
	return null;
}
 
Example 14
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);
}