Java Code Examples for org.eclipse.xtext.xbase.XInstanceOfExpression#getType()

The following examples show how to use org.eclipse.xtext.xbase.XInstanceOfExpression#getType() . 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: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _computeTypes(XInstanceOfExpression object, ITypeComputationState state) {
	ITypeComputationState expressionState = state.withExpectation(state.getReferenceOwner().newReferenceToObject());
	expressionState.computeTypes(object.getExpression());
	JvmTypeReference type = object.getType();
	if (isReferenceToTypeParameter(type)) {
		LightweightTypeReference lightweightReference = state.getReferenceOwner().toLightweightTypeReference(type);
		LightweightTypeReference rawTypeRef = lightweightReference.getRawTypeReference();
		state.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INVALID_USE_OF_TYPE_PARAMETER,
				"Cannot perform instanceof check against type parameter "+lightweightReference.getHumanReadableName()+". Use its erasure "+rawTypeRef.getHumanReadableName()+" instead since further generic type information will be erased at runtime.",
				object.getType(),
				null,
				-1,
				new String[] { 
				}));
	}
	LightweightTypeReference bool = getRawTypeForName(Boolean.TYPE, state);
	state.acceptActualType(bool);
}
 
Example 2
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * If the condition is a {@link XInstanceOfExpression type check}, the checked expression
 * will be automatically casted in the returned state.
 */
protected ITypeComputationState reassignCheckedType(XExpression condition, /* @Nullable */ XExpression guardedExpression, ITypeComputationState state) {
	if (condition instanceof XInstanceOfExpression) {
		XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) condition;
		JvmTypeReference castedType = instanceOfExpression.getType();
		if (castedType != null) {
			state = state.withTypeCheckpoint(guardedExpression);
			JvmIdentifiableElement refinable = getRefinableCandidate(instanceOfExpression.getExpression(), state);
			if (refinable != null) {
				state.reassignType(refinable, state.getReferenceOwner().toLightweightTypeReference(castedType));
			}
		}
	}
	return state;
}
 
Example 3
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Check
public void checkInstanceOfOrder(XIfExpression expression) {
	if (isIgnored(IssueCodes.UNREACHABLE_IF_BLOCK)) {
		return;
	}
	if (expression.eContainer() instanceof XIfExpression) {
		XIfExpression container = (XIfExpression) expression.eContainer();
		if (container.getElse() == expression) {
			return;
		}
	}
	List<XExpression> ifParts = collectIfParts(expression, new ArrayList<XExpression>());
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), expression);
	Multimap<JvmIdentifiableElement, LightweightTypeReference> previousTypeReferences = HashMultimap.create();
	for (XExpression ifPart : ifParts) {
		if (!(ifPart instanceof XInstanceOfExpression)) {
			continue;
		}
		XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) ifPart;
		if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) {
			continue;
		}
		XAbstractFeatureCall featureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression();
		JvmIdentifiableElement feature = featureCall.getFeature();
		if (!(feature instanceof XVariableDeclaration)
				&& !(feature instanceof JvmField)
				&& !(feature instanceof JvmFormalParameter)) {
			continue;
		}
		JvmTypeReference type = instanceOfExpression.getType();
		LightweightTypeReference actualType = owner.toLightweightTypeReference(type);
		if (actualType == null) {
			continue;
		}
		if (isHandled(actualType, previousTypeReferences.get(feature))) {
			addIssue("Unreachable code: The if condition can never match. It is already handled by a previous condition.", type, IssueCodes.UNREACHABLE_IF_BLOCK);
			continue;
		}
		previousTypeReferences.put(feature, actualType);
	}
}