Java Code Examples for org.eclipse.xtext.xbase.XSwitchExpression#getCases()

The following examples show how to use org.eclipse.xtext.xbase.XSwitchExpression#getCases() . 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: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Determine whether the given switch expression should be compiled to a Java switch for Java version 7 or higher.
 */
protected boolean isCompiledToJava7Switch(XSwitchExpression expr) {
	// NOTE: This method could be merged with #isCompiledToJavaSwitch(XSwitchExpression)
	if (!switchExpressions.isJava7SwitchExpression(expr)) {
		return false;
	}
	for (XCasePart casePart : expr.getCases()) {
		if (!switchExpressions.isJavaCaseExpression(expr, casePart)) {
			return false;
		}
		if (!switchExpressions.isConstant(casePart)) {
			return false;
		}
	}
	return true;
}
 
Example 4
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 5
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Determine whether the given switch expression should be compiled to a Java switch for Java version 6 or lower. 
 */
protected boolean isCompiledToJavaSwitch(XSwitchExpression expr) {
	if (!switchExpressions.isJavaSwitchExpression(expr)) {
		return false;
	}
	for (XCasePart casePart : expr.getCases()) {
		if (!switchExpressions.isJavaCaseExpression(expr, casePart)) {
			return false;
		}
		if (!switchExpressions.isConstant(casePart)) {
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean allCasesAreExitedEarly(XSwitchExpression expr) {
	for(XCasePart casePart: expr.getCases()) {
		if(casePart.getThen() != null && !isEarlyExit(casePart.getThen())) {
			return false;
		}
	}
	return true;
}
 
Example 7
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSwitchExpression_0() throws Exception {
	XSwitchExpression exp = (XSwitchExpression) expressionWithExpectedType(
			"switch null {" +
			"  java.lang.Boolean case null : null" +
			"  default : null" +
			"}", "String");
	assertExpected(null,exp.getSwitch());
	for (XCasePart cp : exp.getCases()) {
		assertExpected(null, cp.getCase());
		assertExpected("java.lang.String", cp.getThen());
	}
	assertExpected("java.lang.String", exp.getDefault());
}
 
Example 8
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSwitchExpression_1() throws Exception {
	XSwitchExpression exp = (XSwitchExpression) expressionWithExpectedType(
			"switch true {" +
			"  java.lang.Boolean case null : null" +
			"  default : null" +
			"}", "String");
	for (XCasePart cp : exp.getCases()) {
		assertExpected(null, cp.getCase());
		assertExpected("java.lang.String", cp.getThen());
	}
	assertExpected("java.lang.String", exp.getDefault());
}
 
Example 9
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private int getInsertOffset(XSwitchExpression switchExpression) {
	EList<XCasePart> cases = switchExpression.getCases();
	if (cases.isEmpty()) {
		return insertionOffsets.inEmpty(switchExpression);
	}
	XCasePart casePart = IterableExtensions.last(cases);
	return insertionOffsets.after(casePart);
}
 
Example 10
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns <code>true</code> for expressions that seem to be early exit expressions, e.g.
 * <pre>
 *   while(condition) {
 *     if (anotherCondition)
 *       return value
 *     changeResultOfFirstCondition
 *   }
 * </pre>
 */
public boolean isIntentionalEarlyExit(/* @Nullable */ XExpression expression) {
	if (expression == null) {
		return true;
	}
	if (expression instanceof XBlockExpression) {
		XBlockExpression block = (XBlockExpression) expression;
		List<XExpression> children = block.getExpressions();
		for(XExpression child: children) {
			if (isIntentionalEarlyExit(child)) {
				return true;
			}
		}
	} else if (expression instanceof XIfExpression) {
		return isIntentionalEarlyExit(((XIfExpression) expression).getThen()) 
				|| isIntentionalEarlyExit(((XIfExpression) expression).getElse());
	} else if (expression instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) expression;
		for(XCasePart caseExpression: switchExpression.getCases()) {
			if (isIntentionalEarlyExit(caseExpression.getThen())) {
				return true;
			}
		}
		if (isIntentionalEarlyExit(switchExpression.getDefault())) {
			return true;
		}
	} else if (expression instanceof XTryCatchFinallyExpression) {
		XTryCatchFinallyExpression tryCatchFinally = (XTryCatchFinallyExpression) expression;
		if (isIntentionalEarlyExit(tryCatchFinally.getExpression())) {
			for(XCatchClause catchClause: tryCatchFinally.getCatchClauses()) {
				if (!isIntentionalEarlyExit(catchClause.getExpression()))
					return false;
			}
			return true;
		}
		return false;
	} else if (expression instanceof XAbstractWhileExpression) {
		return isIntentionalEarlyExit(((XAbstractWhileExpression) expression).getBody());
	} else if (expression instanceof XForLoopExpression) {
		return isIntentionalEarlyExit(((XForLoopExpression) expression).getEachExpression());
	} else if (expression instanceof XBasicForLoopExpression) {
		return isIntentionalEarlyExit(((XBasicForLoopExpression) expression).getEachExpression());
	} else if (expression instanceof XSynchronizedExpression) {
		return isIntentionalEarlyExit(((XSynchronizedExpression) expression).getExpression());
	}
	return expression instanceof XReturnExpression || expression instanceof XThrowExpression;
}
 
Example 11
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isDefiniteEarlyExit(XExpression expression) {
	// TODO further improvements
	if (expression instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) expression;
		return isDefiniteEarlyExit(ifExpression.getThen()) && isDefiniteEarlyExit(ifExpression.getElse());
	} else if (expression instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) expression;
		if (isDefiniteEarlyExit(switchExpression.getDefault())) {
			for(XCasePart caseExpression: switchExpression.getCases()) {
				if (!isDefiniteEarlyExit(caseExpression.getThen())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XTryCatchFinallyExpression) {
		XTryCatchFinallyExpression tryExpression = (XTryCatchFinallyExpression) expression;
		if (isDefiniteEarlyExit(tryExpression.getFinallyExpression())) {
			return true;
		}
		if (isDefiniteEarlyExit(tryExpression.getExpression())) {
			for(XCatchClause catchClause: tryExpression.getCatchClauses()) {
				if (!isDefiniteEarlyExit(catchClause.getExpression())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XBlockExpression) {
		List<XExpression> expressions = ((XBlockExpression) expression).getExpressions();
		for(int i = expressions.size() - 1; i >= 0; i--) {
			if (isDefiniteEarlyExit(expressions.get(i))) {
				return true;
			}
		}
	} else if (expression instanceof XSynchronizedExpression) {
		return isDefiniteEarlyExit(((XSynchronizedExpression) expression).getExpression());
	}
	return expression instanceof XReturnExpression || expression instanceof XThrowExpression;
}
 
Example 12
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Only for testing purpose.
 * @nooverride This method is not intended to be re-implemented or extended by clients.
 * @noreference This method is not intended to be referenced by clients.
 */
protected List<XCasePart> getCases(XSwitchExpression switchExpression) {
	return switchExpression.getCases();
}