org.eclipse.xtext.xbase.XCasePart Java Examples
The following examples show how to use
org.eclipse.xtext.xbase.XCasePart.
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: XSwitchExpressions.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
public XExpression getThen(XCasePart casePart, XSwitchExpression switchExpression) { XExpression then = casePart.getThen(); if (then != null) { return then; } int casePartIndex = switchExpression.getCases().indexOf(casePart); if (casePartIndex == -1) { return null; } int count = switchExpression.getCases().size(); if (casePartIndex == count - 1) { return switchExpression.getDefault(); } if (casePartIndex + 1 < count) { return getThen(switchExpression.getCases().get(casePartIndex + 1), switchExpression); } return null; }
Example #2
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
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 #3
Source File: XSwitchExpressions.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
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: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@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 #5
Source File: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@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 #6
Source File: XSwitchExpressionImpl.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @SuppressWarnings("unchecked") @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case XbasePackage.XSWITCH_EXPRESSION__SWITCH: setSwitch((XExpression)newValue); return; case XbasePackage.XSWITCH_EXPRESSION__CASES: getCases().clear(); getCases().addAll((Collection<? extends XCasePart>)newValue); return; case XbasePackage.XSWITCH_EXPRESSION__DEFAULT: setDefault((XExpression)newValue); return; case XbasePackage.XSWITCH_EXPRESSION__DECLARED_PARAM: setDeclaredParam((JvmFormalParameter)newValue); return; } super.eSet(featureID, newValue); }
Example #7
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
/** * 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 #8
Source File: XSwitchExpressionImpl.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @Override public EList<XCasePart> getCases() { if (cases == null) { cases = new EObjectContainmentEList<XCasePart>(XCasePart.class, this, XbasePackage.XSWITCH_EXPRESSION__CASES); } return cases; }
Example #9
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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 #10
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * 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 #11
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected boolean allCasesAreExitedEarly(XSwitchExpression expr) { for(XCasePart casePart: expr.getCases()) { if(casePart.getThen() != null && !isEarlyExit(casePart.getThen())) { return false; } } return true; }
Example #12
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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 #13
Source File: XSwitchExpressions.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
public boolean isConstant(XCasePart casePart) { XExpression caseExpression = casePart.getCase(); if (caseExpression == null) { return false; } try { switchConstantExpressionsInterpreter.evaluate(caseExpression); return true; } catch (ConstantExpressionEvaluationException e) { return false; } }
Example #14
Source File: XbaseImplicitReturnFinder.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void _findImplicitReturns(final XSwitchExpression expression, final ImplicitReturnFinder.Acceptor acceptor) { final Consumer<XCasePart> _function = (XCasePart it) -> { this.findImplicitReturns(it.getThen(), acceptor); }; expression.getCases().forEach(_function); this.findImplicitReturns(expression.getDefault(), acceptor); }
Example #15
Source File: XbaseParserTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void doTestSwitch_2(XSwitchExpression se) { assertEquals(2,se.getCases().size()); assertNull(se.getDefault()); XCasePart c1 = se.getCases().get(0); assertEquals("java.lang.String",c1.getTypeGuard().getIdentifier()); assertFeatureCall("length",c1.getCase()); assertFeatureCall("foo",((XMemberFeatureCall)c1.getCase()).getMemberCallTarget()); assertFeatureCall("bar",c1.getThen()); XCasePart c2 = se.getCases().get(1); assertEquals("java.lang.String",c2.getTypeGuard().getIdentifier()); assertNull(c2.getCase()); assertFeatureCall("baz",((XBlockExpression)c2.getThen()).getExpressions().get(0)); }
Example #16
Source File: XbaseShufflingInjectorProvider.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Override protected List<XCasePart> getCases(XSwitchExpression switchExpression) { List<XCasePart> result = super.getCases(switchExpression); for(XCasePart casePart: result) { if (casePart.getThen() == null) { return result; } } return Lists.reverse(result); }
Example #17
Source File: XbaseParserTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Test public void testSwitch_0() throws Exception { XSwitchExpression se = (XSwitchExpression) expression("switch true { case 1==0 : '1' }"); assertNull(se.getDefault()); assertEquals(1, se.getCases().size()); assertNotNull(se.getSwitch()); XCasePart casePart = se.getCases().get(0); assertTrue(casePart.getCase() instanceof XBinaryOperation); assertTrue(casePart.getThen() instanceof XStringLiteral); }
Example #18
Source File: XbaseParserTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Test public void testSwitch_1() throws Exception { XSwitchExpression se = (XSwitchExpression) expression("switch number{case 1:'1' case 2:'2' default:'3'}"); assertTrue(se.getDefault() instanceof XStringLiteral); assertEquals(2, se.getCases().size()); assertTrue(se.getSwitch() instanceof XFeatureCall); XCasePart case1 = se.getCases().get(0); assertEquals("1", ((XNumberLiteral) case1.getCase()).getValue()); assertTrue(case1.getThen() instanceof XStringLiteral); XCasePart case2 = se.getCases().get(1); assertEquals("2", ((XNumberLiteral) case2.getCase()).getValue()); assertTrue(case2.getThen() instanceof XStringLiteral); }
Example #19
Source File: AbstractXbaseLinkingTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Test public void testSwitchExpression_01() throws Exception { XSwitchExpression switchExpr = (XSwitchExpression) expression( "switch x : new Object() { " + " case x : x" + "}"); final XCasePart xCasePart = switchExpr.getCases().get(0); assertEquals(switchExpr.getDeclaredParam(), ((XFeatureCall) xCasePart.getThen()).getFeature()); assertEquals(switchExpr.getDeclaredParam(), ((XFeatureCall) xCasePart.getCase()).getFeature()); }
Example #20
Source File: XbaseQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Fix(IssueCodes.REDUNDANT_CASE) public void fixRedundantCase(final Issue issue, IssueResolutionAcceptor acceptor) { acceptor.accept(issue, "Remove redundant case.", "Remove redundant case.", null, new ReplaceModification(issue, "")); acceptor.accept(issue, "Assign empty expression.", "Assign empty expression.", null, new ISemanticModification() { @Override public void apply(EObject element, IModificationContext context) throws Exception { XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class); if (switchExpression == null) { return; } XCasePart casePart = IterableExtensions.last(switchExpression.getCases()); if (casePart == null || !(casePart.isFallThrough() && casePart.getThen() == null)) { return; } List<INode> nodes = NodeModelUtils.findNodesForFeature(casePart, XbasePackage.Literals.XCASE_PART__FALL_THROUGH); if (nodes.isEmpty()) { return; } INode firstNode = IterableExtensions.head(nodes); INode lastNode = IterableExtensions.last(nodes); int offset = firstNode.getOffset(); int length = lastNode.getEndOffset() - offset; ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), offset, length); appendable.append(": {"); appendable.increaseIndentation().newLine(); appendable.decreaseIndentation().newLine().append("}"); appendable.commitChanges(); } }); }
Example #21
Source File: XbaseQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
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 #22
Source File: EarlyExitValidator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected int markAsDeadCode(List<XCasePart> cases, XCasePart from, int idx, int size) { if (!markAsDeadCode(from.getThen())) { for(int j = idx + 1; j < size; j++) { XCasePart next = cases.get(j); if (markAsDeadCode(next.getTypeGuard()) || markAsDeadCode(next.getCase()) || markAsDeadCode(next.getThen())) { idx = j; j = size; } } } return idx; }
Example #23
Source File: EarlyExitValidator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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 #24
Source File: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Check public void checkRedundantCase(XSwitchExpression switchExpression) { XCasePart casePart = IterableExtensions.last(switchExpression.getCases()); if (casePart == null || !(casePart.isFallThrough() && casePart.getThen() == null)) { return; } if (switchExpression.getDefault() == null) { error("Redundant case.", casePart, null, IssueCodes.REDUNDANT_CASE); } else { warning("Redundant case.", casePart, null, IssueCodes.REDUNDANT_CASE); } }
Example #25
Source File: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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 #26
Source File: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@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 #27
Source File: ParserTest.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
@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 #28
Source File: AbstractXbaseSemanticSequencer.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
@Deprecated protected void sequence_XCasePart(EObject context, XCasePart semanticObject) { sequence_XCasePart(createContext(context, semanticObject), semanticObject); }
Example #29
Source File: XtendHoverSignatureProviderTest.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@Test public void testAutcastExpressions() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("package testPackage"); _builder.newLine(); _builder.append("class Foo {"); _builder.newLine(); _builder.append("\t"); _builder.append("def foo() {"); _builder.newLine(); _builder.append("\t\t"); _builder.append("val CharSequence c = \"\""); _builder.newLine(); _builder.append("\t\t"); _builder.append("if (c instanceof String) {"); _builder.newLine(); _builder.append("\t\t\t"); _builder.append("c.substring(1, 1)"); _builder.newLine(); _builder.append("\t\t"); _builder.append("}"); _builder.newLine(); _builder.append("\t\t"); _builder.append("switch(c){"); _builder.newLine(); _builder.append("\t\t\t"); _builder.append("String : c.length"); _builder.newLine(); _builder.append("\t\t"); _builder.append("}"); _builder.newLine(); _builder.append("\t"); _builder.append("}"); _builder.newLine(); _builder.append("}"); _builder.newLine(); final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet()); XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes()); XtendMember _head_1 = IterableExtensions.<XtendMember>head(((XtendClass) _head).getMembers()); final XtendFunction func = ((XtendFunction) _head_1); XExpression _expression = func.getExpression(); final XBlockExpression block = ((XBlockExpression) _expression); final XExpression dec = IterableExtensions.<XExpression>head(block.getExpressions()); Assert.assertEquals("CharSequence c", this.signatureProvider.getSignature(dec)); XExpression _get = block.getExpressions().get(1); final XIfExpression ifexpr = ((XIfExpression) _get); final XExpression then = ifexpr.getThen(); XExpression _head_2 = IterableExtensions.<XExpression>head(((XBlockExpression) then).getExpressions()); final XExpression target = ((XMemberFeatureCall) _head_2).getMemberCallTarget(); Assert.assertEquals("String c", this.signatureProvider.getSignature(target)); XExpression _get_1 = block.getExpressions().get(2); final XSwitchExpression switchExpr = ((XSwitchExpression) _get_1); XExpression _then = IterableExtensions.<XCasePart>head(switchExpr.getCases()).getThen(); final XExpression expr = ((XMemberFeatureCall) _then).getMemberCallTarget(); Assert.assertEquals("String c", this.signatureProvider.getSignature(expr)); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
Example #30
Source File: XtendHoverSignatureProviderTest.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@Test public void testAutcastExpressions_2() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("package testPackage"); _builder.newLine(); _builder.append("class Foo {"); _builder.newLine(); _builder.append("\t"); _builder.append("CharSequence c = \"\""); _builder.newLine(); _builder.append("\t"); _builder.append("def foo() {"); _builder.newLine(); _builder.append("\t\t"); _builder.append("if (c instanceof String) {"); _builder.newLine(); _builder.append("\t\t\t"); _builder.append("c.substring(1, 1)"); _builder.newLine(); _builder.append("\t\t"); _builder.append("}"); _builder.newLine(); _builder.append("\t\t"); _builder.append("switch(c){"); _builder.newLine(); _builder.append("\t\t\t"); _builder.append("String : c.length"); _builder.newLine(); _builder.append("\t\t"); _builder.append("}"); _builder.newLine(); _builder.append("\t"); _builder.append("}"); _builder.newLine(); _builder.append("}"); _builder.newLine(); final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet()); XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes()); Assert.assertEquals("CharSequence c", this.signatureProvider.getSignature(IterableExtensions.<XtendMember>head(((XtendClass) _head).getMembers()))); XtendTypeDeclaration _head_1 = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes()); XtendMember _get = ((XtendClass) _head_1).getMembers().get(1); final XtendFunction func = ((XtendFunction) _get); XExpression _expression = func.getExpression(); final XBlockExpression block = ((XBlockExpression) _expression); XExpression _head_2 = IterableExtensions.<XExpression>head(block.getExpressions()); final XIfExpression ifexpr = ((XIfExpression) _head_2); final XExpression then = ifexpr.getThen(); XExpression _head_3 = IterableExtensions.<XExpression>head(((XBlockExpression) then).getExpressions()); final XExpression target = ((XMemberFeatureCall) _head_3).getMemberCallTarget(); Assert.assertEquals("String Foo.c", this.signatureProvider.getSignature(target)); XExpression _get_1 = block.getExpressions().get(1); final XSwitchExpression switchExpr = ((XSwitchExpression) _get_1); XExpression _then = IterableExtensions.<XCasePart>head(switchExpr.getCases()).getThen(); final XExpression expr = ((XMemberFeatureCall) _then).getMemberCallTarget(); Assert.assertEquals("String Foo.c", this.signatureProvider.getSignature(expr)); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }