Java Code Examples for org.eclipse.xtext.xbase.XExpression#eContainingFeature()

The following examples show how to use org.eclipse.xtext.xbase.XExpression#eContainingFeature() . 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: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkInvalidReturnExpression(XExpression expression) {
	final EReference contFeature = (EReference) expression.eContainingFeature();
	final Map<EReference, EarlyExitKind> map = getDisallowedEarlyExitReferences();
	if (map.containsKey(contFeature)) {
		EarlyExitKind exitKind = map.get(contFeature);
		List<XExpression> returns = newArrayList();
		collectExits(expression, returns);
		for (XExpression expr : returns) {
			if (expr instanceof XReturnExpression && (exitKind == EarlyExitKind.RETURN || exitKind == EarlyExitKind.BOTH)) {
				error("A return expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
			if (expr instanceof XThrowExpression && (exitKind == EarlyExitKind.THROW || exitKind == EarlyExitKind.BOTH)) {
				error("A throw expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
		}
	}
}
 
Example 2
Source File: RootResolvedTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected AbstractDiagnostic createTypeDiagnostic(XExpression expression, LightweightTypeReference actualType, LightweightTypeReference expectedType) {
	if (!expectedType.isAny()) {
		String actualName = actualType.getSimpleName();
		String expectedName = expectedType.getSimpleName();
		if (actualName.equals(expectedName)) {
			if (expectedType.isAssignableFrom(actualType)) {
				return null;
			}
		}
		if (expression.eContainingFeature() == XbasePackage.Literals.XABSTRACT_FEATURE_CALL__IMPLICIT_FIRST_ARGUMENT) {
			return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
					"Type mismatch: cannot convert implicit first argument from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
					expression, null, -1, null);
		} else {
			return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
					"Type mismatch: cannot convert from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
					expression, null, -1, null);
		}
	} else {
		return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
				"Type mismatch: type %s is not applicable at this location", actualType.getHumanReadableName()), expression, null, -1,
				null);
	}
}
 
Example 3
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isValueExpectedRecursive(XExpression expr) {
	EStructuralFeature feature = expr.eContainingFeature();
	EObject container = expr.eContainer();
	
	// is part of block
	if (container instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) container;
		final List<XExpression> expressions = blockExpression.getExpressions();
		if (expressions.get(expressions.size()-1) != expr) {
			return false;
		}
	}
	// no expectation cases
	if (feature == XbasePackage.Literals.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__EACH_EXPRESSION) {
		return false;
	}
	// is value expected
	if (container instanceof XAbstractFeatureCall 
		|| container instanceof XConstructorCall
		|| container instanceof XAssignment
		|| container instanceof XVariableDeclaration
		|| container instanceof XReturnExpression
		|| container instanceof XThrowExpression
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__FOR_EXPRESSION
		|| feature == XbasePackage.Literals.XSWITCH_EXPRESSION__SWITCH
		|| feature == XbasePackage.Literals.XCASE_PART__CASE
		|| feature == XbasePackage.Literals.XIF_EXPRESSION__IF
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE
		|| feature == XbasePackage.Literals.XBASIC_FOR_LOOP_EXPRESSION__EXPRESSION
		|| feature == XbasePackage.Literals.XSYNCHRONIZED_EXPRESSION__PARAM) {
		return true;
	}
	if (isLocalClassSemantics(container) || logicalContainerProvider.getLogicalContainer(expr) != null) {
		LightweightTypeReference expectedReturnType = typeResolver.resolveTypes(expr).getExpectedReturnType(expr);
		return expectedReturnType == null || !expectedReturnType.isPrimitiveVoid();
	}
	if (container instanceof XCasePart || container instanceof XCatchClause) {
		container = container.eContainer();
	}
	if (container instanceof XExpression) {
		return isValueExpectedRecursive((XExpression) container);
	}
	return true;
}