Java Code Examples for org.eclipse.xtext.xbase.typesystem.IResolvedTypes#getExpectedType()

The following examples show how to use org.eclipse.xtext.xbase.typesystem.IResolvedTypes#getExpectedType() . 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: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected LightweightTypeReference getTypeForVariableDeclaration(XExpression expr) {
	IResolvedTypes resolvedTypes = getResolvedTypes(expr);
	LightweightTypeReference actualType = resolvedTypes.getActualType(expr);
	if (actualType.isPrimitiveVoid()) {
		LightweightTypeReference expectedType = resolvedTypes.getExpectedType(expr);
		if (expectedType == null) {
			expectedType = resolvedTypes.getExpectedReturnType(expr);
			if (expectedType == null) {
				expectedType = resolvedTypes.getReturnType(expr);
			}
		}
		if (expectedType != null && !expectedType.isPrimitiveVoid()) {
			actualType = expectedType;
		}
	}
	return actualType;
}
 
Example 2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param context unused in this context but required for dispatching
 * @param indicator unused in this context but required for dispatching
 */
@SuppressWarnings("unchecked")
protected Object _doEvaluate(XNumberLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference expectedType = resolvedTypes.getExpectedType(literal);
	Class<? extends Number> type = numberLiterals.getJavaType(literal);
	if (expectedType != null && expectedType.isSubtypeOf(Number.class)) {
		try {
			Class<?> expectedClassType = getJavaType(expectedType.toJavaCompliantTypeReference().getType());
			if (expectedClassType.isPrimitive()) {
				expectedClassType = ReflectionUtil.getObjectType(expectedClassType);
			}
			if (Number.class != expectedClassType && Number.class.isAssignableFrom(expectedClassType)) {
				type = (Class<? extends Number>) expectedClassType;
			}
		} catch (ClassNotFoundException e) {
		}
	}
	return numberLiterals.numberValue(literal, type);
}
 
Example 3
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Replies the type for a variable declaration that is initialized with the given expression.
 *
 * @param expr the expression.
 * @return the variable type.
 */
protected LightweightTypeReference getTypeForVariableDeclaration(XExpression expr) {
	final IResolvedTypes resolvedTypes = getResolvedTypes(expr);
	LightweightTypeReference actualType = resolvedTypes.getActualType(expr);
	if (actualType.isPrimitiveVoid()) {
		LightweightTypeReference expectedType = resolvedTypes.getExpectedType(expr);
		if (expectedType == null) {
			expectedType = resolvedTypes.getExpectedReturnType(expr);
			if (expectedType == null) {
				expectedType = resolvedTypes.getReturnType(expr);
			}
		}
		if (expectedType != null && !expectedType.isPrimitiveVoid()) {
			actualType = expectedType;
		}
	}
	return actualType;
}
 
Example 4
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private LightweightTypeReference findRealReturnType(XExpression expression) {
	if (expression == null)
		return null;
	JvmIdentifiableElement logicalContainer = getLogicalContainerProvider().getLogicalContainer(expression);
	if (logicalContainer instanceof JvmOperation) {
		return getLightweightType(logicalContainer);
	}
	if (expression instanceof XClosure) {
		IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(expression);
		LightweightTypeReference type = resolvedTypes.getExpectedType(expression);
		if (type == null) {
			type = resolvedTypes.getActualType(expression);
		}
		if (type == null) {
			return null;
		}
		FunctionTypeReference functionType = type.tryConvertToFunctionTypeReference(false);
		if (functionType != null) {
			return functionType.getReturnType();
		}
		return null;
	}
	XExpression containerExpression = EcoreUtil2.getContainerOfType(expression.eContainer(), XExpression.class);
	if (containerExpression == null) {
		LightweightTypeReference returnType = getLightweightReturnType(expression);
		return returnType;
	}
	return findRealReturnType(containerExpression);
}
 
Example 5
Source File: AbstractBatchTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertExpressionTypeIsResolved(XExpression expression, IResolvedTypes types) {
	LightweightTypeReference type = types.getActualType(expression);
	Assert.assertNotNull("Type is not resolved. Expression: " + expression.toString(), type);
	Assert.assertNotNull(expression.toString() + " / " + type, type.getIdentifier());
	LightweightTypeReference expectedType = types.getExpectedType(expression);
	Assert.assertNotNull(expression.toString(), String.valueOf(expectedType));
}
 
Example 6
Source File: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getNewMemberType(XAbstractFeatureCall call) {
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(call);
	if(call instanceof XAssignment) {
		XExpression value = ((XAssignment) call).getValue();
		return resolvedTypes.getActualType(value);
	} else {
		return resolvedTypes.getExpectedType(call);
	}
}
 
Example 7
Source File: CompoundReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public LightweightTypeReference getExpectedType(XExpression expression) {
	IResolvedTypes delegate = getDelegate(expression);
	return delegate.getExpectedType(expression);
}
 
Example 8
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected LightweightTypeReference getLightweightExpectedType(XExpression expr) {
	IResolvedTypes resolvedTypes = getResolvedTypes(expr);
	LightweightTypeReference expectedType = resolvedTypes.getExpectedType(expr);
	return expectedType;
}