Java Code Examples for org.eclipse.xtext.xbase.XCasePart#getTypeGuard()

The following examples show how to use org.eclipse.xtext.xbase.XCasePart#getTypeGuard() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkTypeGuardsOrder(XSwitchExpression expression) {
	if (isIgnored(IssueCodes.UNREACHABLE_CASE)) {
		return;
	}
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), expression);
	List<LightweightTypeReference> previousTypeReferences = new ArrayList<LightweightTypeReference>();
	for (XCasePart casePart : expression.getCases()) {
		JvmTypeReference typeGuard = casePart.getTypeGuard();
		if (typeGuard == null) {
			continue;
		}
		LightweightTypeReference actualType = owner.toLightweightTypeReference(typeGuard);
		if (actualType == null) {
			continue;
		}
		if (isHandled(actualType, previousTypeReferences)) {
			addIssue("Unreachable code: The case can never match. It is already handled by a previous condition.", typeGuard, IssueCodes.UNREACHABLE_CASE);
			continue;
		}
		if (casePart.getCase() == null) {
			previousTypeReferences.add(actualType);
		}
	}
}
 
Example 2
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkTypeGuardsOrderWithGenerics(XSwitchExpression expression) {
	if (isIgnored(IssueCodes.UNREACHABLE_CASE)) {
		return;
	}
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), expression);
	List<LightweightTypeReference> previousTypeReferences = new ArrayList<LightweightTypeReference>();
	for (XCasePart casePart : expression.getCases()) {
		JvmTypeReference typeGuard = casePart.getTypeGuard();
		if (typeGuard == null) {
			continue;
		}
		LightweightTypeReference typeReference = owner.toLightweightTypeReference(typeGuard);
		LightweightTypeReference actualType = typeReference.getRawTypeReference();
		if (actualType == null || typeReference == actualType) {
			continue;
		}
		if (isHandled(actualType, previousTypeReferences)) {
			addIssue("Unreachable code: The case can never match. It is already handled by a previous condition (with the same type erasure).", typeGuard, IssueCodes.UNREACHABLE_CASE);
			continue;
		}
		if (casePart.getCase() == null) {
			previousTypeReferences.add(actualType);
		}
	}
}
 
Example 3
Source File: XSwitchExpressions.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isJavaCaseExpression(XSwitchExpression it, XCasePart casePart) {
	if (casePart.getTypeGuard() != null) {
		return false;
	}
	XExpression caseExpression = casePart.getCase();
	if (caseExpression == null) {
		return false;
	}
	IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(it);
	LightweightTypeReference caseType = resolvedTypes.getActualType(caseExpression);
	if (caseType == null) {
		return false;
	}
	LightweightTypeReference switchType = getSwitchVariableType(it);
	if (!switchType.isAssignableFrom(caseType)) {
		return false;
	}
	return true;
}
 
Example 4
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable appendCloseIfStatement(XCasePart casePart, ITreeAppendable caseAppendable, XSwitchExpressionCompilationState state) {
	// close surrounding if statements
	if (state.caseNeedsIfNotMatchedCheck()) {
		if (casePart.getCase() != null) {
			caseAppendable.decreaseIndentation().newLine().append("}");
		}
		
		if (casePart.getTypeGuard() != null) {
			caseAppendable.decreaseIndentation().newLine().append("}");
			caseAppendable.closeScope();
		}
	} else if (casePart.getCase() != null && casePart.getTypeGuard() != null) {
		caseAppendable.decreaseIndentation().newLine().append("}");
		caseAppendable.closeScope();
	} else if (casePart.getTypeGuard() != null) {
		caseAppendable.closeScope();
	}
	state.finishProcessingCase();
	return caseAppendable;
}
 
Example 5
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkTypeReferenceIsNotVoid(XCasePart expression) {
	if (expression.getTypeGuard() != null) {
		if (toLightweightTypeReference(expression.getTypeGuard()).isPrimitiveVoid()) {
			error("Primitive void cannot be used here.", expression.getTypeGuard(), null, INVALID_USE_OF_TYPE);
		}
	}
}
 
Example 6
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkTypeGuards(XCasePart casePart) {
	if (casePart.getTypeGuard() == null)
		return;
	LightweightTypeReference typeGuard = toLightweightTypeReference(casePart.getTypeGuard());
	if (typeGuard.isPrimitive()) {
		error("Primitives are not allowed as type guards", Literals.XCASE_PART__TYPE_GUARD, INVALID_USE_OF_TYPE);
		return;
	}
	LightweightTypeReference targetTypeRef = getActualType(((XSwitchExpression) casePart.eContainer()).getSwitch());
	checkCast(casePart.getTypeGuard(), typeGuard, targetTypeRef);
}
 
Example 7
Source File: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkDeadCode(XSwitchExpression switchExpression) {
	List<XCasePart> cases = switchExpression.getCases();
	for(int i = 0, size = cases.size(); i < size; i++) {
		XCasePart casePart = cases.get(i);
		XExpression caseExpression = casePart.getCase();
		if (!earlyExitComputer.isEarlyExit(caseExpression)) {
			validateCondition(caseExpression, false);
		} else if (!markAsDeadCode(casePart.getThen())) {
			if (casePart.getTypeGuard() == null) { 
				i = markAsDeadCode(cases, casePart, i, size);
			}
		}
	}
}
 
Example 8
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiGuard_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m(Object x) { switch(x) { NullPointerException | IllegalArgumentException : x } } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XSwitchExpression switchExpr = (XSwitchExpression) body.getExpressions().get(0);
	XCasePart singleCase = switchExpr.getCases().get(0);
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) singleCase.getTypeGuard();
	assertEquals(2, parameterType.getReferences().size());
}
 
Example 9
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable appendOpenIfStatement(XCasePart casePart, ITreeAppendable b, String matchedVariable, String variableName, XSwitchExpressionCompilationState state) {
	ITreeAppendable caseAppendable = b.trace(casePart, true);
	if (state.caseNeedsIfNotMatchedCheck()) {
		caseAppendable.newLine().append("if (!").append(matchedVariable).append(") {");
		caseAppendable.increaseIndentation();
	}
	JvmTypeReference typeGuard = casePart.getTypeGuard();
	if (typeGuard != null) {
		ITreeAppendable typeGuardAppendable = caseAppendable.trace(typeGuard, true);
		typeGuardAppendable.newLine().append("if (");
		if (typeGuard instanceof JvmSynonymTypeReference) {
			Iterator<JvmTypeReference> iter = ((JvmSynonymTypeReference) typeGuard).getReferences().iterator();
			while(iter.hasNext()) {
				typeGuardAppendable.append(variableName);
				typeGuardAppendable.append(" instanceof ");
				typeGuardAppendable.trace(typeGuard).append(iter.next().getType());
				if (iter.hasNext()) {
					typeGuardAppendable.append(" || ");
				}
			}
		} else {
			typeGuardAppendable.append(variableName);
			typeGuardAppendable.append(" instanceof ");
			typeGuardAppendable.trace(typeGuard).append(typeGuard.getType());
		}
		typeGuardAppendable.append(") {");
		typeGuardAppendable.increaseIndentation();
		typeGuardAppendable.openPseudoScope();
	}
	if (casePart.getCase() != null) {
		ITreeAppendable conditionAppendable = caseAppendable.trace(casePart.getCase(), true);
		internalToJavaStatement(casePart.getCase(), conditionAppendable, true);
		conditionAppendable.newLine().append("if (");
		LightweightTypeReference convertedType = getLightweightType(casePart.getCase());
		if (convertedType.isType(Boolean.TYPE) || convertedType.isType(Boolean.class)) {
			internalToJavaExpression(casePart.getCase(), conditionAppendable);
		} else {
			JvmType objectsType = findKnownType(Objects.class, casePart);
			if (objectsType != null) {
				conditionAppendable.append(objectsType);
				conditionAppendable.append(".equal(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			} else {
				// use ObjectExtensions rather than a == b || a != null && a.equals(b) since
				// that won't work with primitive types and other incompatible conditional operands
				conditionAppendable.append(ObjectExtensions.class);
				conditionAppendable.append(".operator_equals(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			}
		}
		conditionAppendable.append(")");
		caseAppendable.append(" {");
		caseAppendable.increaseIndentation();
	}
	// set matched to true
	return caseAppendable.newLine().append(matchedVariable).append("=true;");
}