Java 类org.eclipse.xtext.xbase.XCasePart 实例源码

项目:xtext-extras    文件:XbaseValidator.java   
@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);
        }
    }
}
项目:xtext-extras    文件:XbaseValidator.java   
@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);
        }
    }
}
项目:xtext-extras    文件:XbaseCompiler.java   
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;
}
项目:xtext-extras    文件:XbaseCompiler.java   
/**
 * 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;
}
项目:xtext-extras    文件:XSwitchExpressionImpl.java   
/**
 * <!-- 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);
}
项目:xtext-extras    文件:XSwitchExpressions.java   
public boolean isConstant(final XCasePart casePart) {
  final XExpression case_ = casePart.getCase();
  if ((case_ == null)) {
    return false;
  }
  try {
    this._switchConstantExpressionsInterpreter.evaluate(case_);
    return true;
  } catch (final Throwable _t) {
    if (_t instanceof ConstantExpressionEvaluationException) {
      return false;
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
项目:xtext-extras    文件:XSwitchExpressions.java   
public XExpression getThen(final XCasePart casePart, final XSwitchExpression switchExpression) {
  final XExpression then = casePart.getThen();
  if ((then != null)) {
    return then;
  }
  final int casePartIndex = switchExpression.getCases().indexOf(casePart);
  if ((casePartIndex == (-1))) {
    return null;
  }
  final int count = switchExpression.getCases().size();
  if ((casePartIndex == (count - 1))) {
    return switchExpression.getDefault();
  }
  if (((casePartIndex + 1) < count)) {
    return this.getThen(switchExpression.getCases().get((casePartIndex + 1)), switchExpression);
  }
  return null;
}
项目:xtext-extras    文件:XbaseValidator.java   
@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);
        }
    }
}
项目:xtext-extras    文件:XbaseValidator.java   
@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);
}
项目:xtext-extras    文件:XbaseValidator.java   
@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);
    }
}
项目:xtext-extras    文件:EarlyExitValidator.java   
@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);
            }
        }
    }
}
项目:xtext-extras    文件:EarlyExitValidator.java   
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;
}
项目:xtext-extras    文件:XbaseCompiler.java   
/**
 * 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;
}
项目:xtext-extras    文件:XbaseCompiler.java   
protected boolean allCasesAreExitedEarly(XSwitchExpression expr) {
    for(XCasePart casePart: expr.getCases()) {
        if(casePart.getThen() != null && !isEarlyExit(casePart.getThen())) {
            return false;
        }
    }
    return true;
}
项目:xtext-extras    文件:XSwitchExpressionImpl.java   
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public EList<XCasePart> getCases()
{
    if (cases == null)
    {
        cases = new EObjectContainmentEList<XCasePart>(XCasePart.class, this, XbasePackage.XSWITCH_EXPRESSION__CASES);
    }
    return cases;
}
项目:xtext-extras    文件:XbaseImplicitReturnFinder.java   
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);
}
项目:xtext-extras    文件:XSwitchExpressions.java   
public boolean isJavaCaseExpression(final XSwitchExpression it, final XCasePart casePart) {
  boolean _xblockexpression = false;
  {
    JvmTypeReference _typeGuard = casePart.getTypeGuard();
    boolean _tripleNotEquals = (_typeGuard != null);
    if (_tripleNotEquals) {
      return false;
    }
    final XExpression case_ = casePart.getCase();
    if ((case_ == null)) {
      return false;
    }
    @Extension
    final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(it);
    final LightweightTypeReference caseType = resolvedTypes.getActualType(case_);
    if ((caseType == null)) {
      return false;
    }
    final LightweightTypeReference switchType = this.getSwitchVariableType(it);
    boolean _isAssignableFrom = switchType.isAssignableFrom(caseType);
    boolean _not = (!_isAssignableFrom);
    if (_not) {
      return false;
    }
    _xblockexpression = true;
  }
  return _xblockexpression;
}
项目:xtext-extras    文件:XbaseShufflingInjectorProvider.java   
@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);
}
项目:xtext-extras    文件:XbaseParserTest.java   
@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);
}
项目:xtext-extras    文件:XbaseParserTest.java   
@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);
}
项目:xtext-extras    文件:XbaseParserTest.java   
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));
}
项目:xtext-extras    文件:XbaseExpectedTypeProviderTest.java   
@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());
}
项目:xtext-extras    文件:XbaseExpectedTypeProviderTest.java   
@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());
}
项目:xtext-extras    文件:AbstractXbaseLinkingTest.java   
@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());
}
项目:xtext-extras    文件:ImplicitReturnFinderTest.java   
@Test
public void testSwitch01() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("switch (this) case String: 1 default: 2");
    XExpression _expression = this.expression(_builder);
    final XSwitchExpression expr = ((XSwitchExpression) _expression);
    this.hasImplicitReturns(expr, IterableExtensions.<XCasePart>head(expr.getCases()).getThen(), expr.getDefault());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:AbstractXbaseSemanticSequencer.java   
@Deprecated
protected void sequence_XCasePart(EObject context, XCasePart semanticObject) {
    sequence_XCasePart(createContext(context, semanticObject), semanticObject);
}
项目:xtext-extras    文件:XbaseValidator.java   
protected boolean isValueExpectedRecursive(XExpression expr) {
    EStructuralFeature feature = expr.eContainingFeature();
    EObject container = expr.eContainer();

    // is part of block
    if (container instanceof XBlockExpression) {
        XBlockExpression blockExpression = (XBlockExpression) container;
        final List<XExpression> expressions = blockExpression.getExpressions();
        if (expressions.get(expressions.size()-1) != expr) {
            return false;
        }
    }
    // no expectation cases
    if (feature == XbasePackage.Literals.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION
        || feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY
        || feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__EACH_EXPRESSION) {
        return false;
    }
    // is value expected
    if (container instanceof XAbstractFeatureCall 
        || container instanceof XConstructorCall
        || container instanceof XAssignment
        || container instanceof XVariableDeclaration
        || container instanceof XReturnExpression
        || container instanceof XThrowExpression
        || feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__FOR_EXPRESSION
        || feature == XbasePackage.Literals.XSWITCH_EXPRESSION__SWITCH
        || feature == XbasePackage.Literals.XCASE_PART__CASE
        || feature == XbasePackage.Literals.XIF_EXPRESSION__IF
        || feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE
        || feature == XbasePackage.Literals.XBASIC_FOR_LOOP_EXPRESSION__EXPRESSION
        || feature == XbasePackage.Literals.XSYNCHRONIZED_EXPRESSION__PARAM) {
        return true;
    }
    if (isLocalClassSemantics(container) || logicalContainerProvider.getLogicalContainer(expr) != null) {
        LightweightTypeReference expectedReturnType = typeResolver.resolveTypes(expr).getExpectedReturnType(expr);
        return expectedReturnType == null || !expectedReturnType.isPrimitiveVoid();
    }
    if (container instanceof XCasePart || container instanceof XCatchClause) {
        container = container.eContainer();
    }
    if (container instanceof XExpression) {
        return isValueExpectedRecursive((XExpression) container);
    }
    return true;
}
项目:xtext-extras    文件:ThrownExceptionSwitch.java   
@Override
public Boolean caseXCasePart(XCasePart object) {
    return Boolean.TRUE;
}
项目:xtext-extras    文件:ExtendedEarlyExitComputer.java   
/**
 * 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;
}
项目:xtext-extras    文件:ExtendedEarlyExitComputer.java   
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;
}
项目:xtext-extras    文件:XbaseCompiler.java   
protected void _toJavaIfStatement(
        XCasePart casePart,
        List<XCasePart> fallThroughCases,
        XSwitchExpression expr,
        XExpression then,
        ITreeAppendable b,
        boolean isReferenced,
        String switchResultName,
        String matchedVariable,
        String variableName,
        XSwitchExpressionCompilationState state) {
    ITreeAppendable caseAppendable = b;
    if (!fallThroughCases.isEmpty()) {
        boolean first = true;
        Iterator<XCasePart> i = fallThroughCases.iterator();
        boolean caseNeedsIfNotMatchedCheck = state.caseNeedsIfNotMatchedCheck();
        while(i.hasNext()) {
            XCasePart fallThroughCase = i.next();
            caseAppendable = appendOpenIfStatement(fallThroughCase, caseAppendable, matchedVariable, variableName, state);
            if (first) {
                first = false;
                if (!caseNeedsIfNotMatchedCheck) {
                    closeBlock(caseAppendable);
                }
            } else {
                closeBlock(caseAppendable);
            }
            appendCloseIfStatement(fallThroughCase, caseAppendable, state);
            i.remove();
        }
        caseAppendable = appendOpenIfStatement(casePart, caseAppendable, matchedVariable, variableName, state);
        appendCloseIfStatement(casePart, caseAppendable, state);
        closeBlock(caseAppendable);

        caseAppendable.newLine().append("if (").append(matchedVariable).append(") {").increaseIndentation();
        executeThenPart(expr, switchResultName, then, caseAppendable, isReferenced);
        closeBlock(caseAppendable);
        if (caseNeedsIfNotMatchedCheck) {
            closeBlock(caseAppendable);
        }
    } else {
        caseAppendable = appendOpenIfStatement(casePart, caseAppendable, matchedVariable, variableName, state);
        executeThenPart(expr, switchResultName, then, caseAppendable, isReferenced);
        appendCloseIfStatement(casePart, caseAppendable, state);
        closeBlock(caseAppendable);
    }
}
项目:xtext-extras    文件:XbaseCompiler.java   
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();
    }
    if (casePart.getTypeGuard() != null) {
        ITreeAppendable typeGuardAppendable = caseAppendable.trace(casePart.getTypeGuard(), true);
        typeGuardAppendable.newLine().append("if (");
        typeGuardAppendable.append(variableName);
        typeGuardAppendable.append(" instanceof ");
        typeGuardAppendable.trace(casePart.getTypeGuard()).append(casePart.getTypeGuard().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;");
}
项目:xtext-extras    文件:XbaseCompiler.java   
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
    if (expr instanceof XAnnotation) {
        return false;
    }
    if (expr instanceof XListLiteral) {
        return false;
    }
    if (expr instanceof XSetLiteral) {
        return false;
    }
    if (expr instanceof XCastedExpression) {
        return false;
    }
    if (expr instanceof XInstanceOfExpression) {
        return false;
    }
    if (expr instanceof XMemberFeatureCall && isVariableDeclarationRequired((XMemberFeatureCall) expr, b))
        return true;
    EObject container = expr.eContainer();
    if ((container instanceof XVariableDeclaration)
        || (container instanceof XReturnExpression) 
        || (container instanceof XThrowExpression)) {
        return false;
    }
    if (container instanceof XIfExpression) {
        XIfExpression ifExpression = (XIfExpression) container;
        if (ifExpression.getThen() == expr || ifExpression.getElse() == expr) {
            return false;
        }
    }
    if (container instanceof XCasePart) {
        XCasePart casePart = (XCasePart) container;
        if (casePart.getThen() == expr) {
            return false;
        }
    }
    if (container instanceof XSwitchExpression) {
        XSwitchExpression switchExpression = (XSwitchExpression) container;
        if (switchExpression.getDefault() == expr) {
            return false;
        }
    }
    if (container instanceof XBlockExpression) {
        List<XExpression> siblings = ((XBlockExpression) container).getExpressions();
        if (siblings.get(siblings.size() - 1) == expr) {
            return isVariableDeclarationRequired(getFeatureCall(expr), expr, b);
        }
    }
    if (container instanceof XClosure) {
        if (((XClosure) container).getExpression() == expr) {
            return false;
        }
    }
    if (expr instanceof XAssignment) {
        XAssignment a = (XAssignment) expr;
        for (XExpression arg : getActualArguments(a)) {
            if (isVariableDeclarationRequired(arg, b, recursive)) {
                return true;
            }
        }
    }
    return super.isVariableDeclarationRequired(expr, b, recursive);
}
项目:xtext-extras    文件:ValidationTests.java   
@Test public void testPrimitiveAsTypeGuard() throws Exception {
    XCasePart expression = ((XSwitchExpression) expression("switch(new Object()) { int: 1 }")).getCases().get(0);
    helper.assertError(expression, XCASE_PART, INVALID_USE_OF_TYPE, "primitive", "not", "allowed", "type", "guard");
}
项目:xtext-extras    文件:AbstractXbaseSemanticSequencer.java   
/**
 * Contexts:
 *     XCasePart returns XCasePart
 *
 * Constraint:
 *     (typeGuard=JvmTypeReference? case=XExpression? (then=XExpression | fallThrough?=','))
 */
protected void sequence_XCasePart(ISerializationContext context, XCasePart semanticObject) {
    genericSequencer.createSequence(context, semanticObject);
}
项目:xtext-extras    文件:XbaseTypeComputer.java   
/**
 * 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();
}
项目:dsl-devkit    文件:AbstractCheckSemanticSequencer.java   
/**
 * Constraint:
 *     (typeGuard=JvmTypeReference? case=XExpression? (then=XExpression | fallThrough?=','))
 */
protected void sequence_XCasePart(EObject context, XCasePart semanticObject) {
    genericSequencer.createSequence(context, semanticObject);
}
项目:dsl-devkit    文件:AbstractCheckCfgSemanticSequencer.java   
/**
 * Constraint:
 *     (typeGuard=JvmTypeReference? case=XExpression? (then=XExpression | fallThrough?=','))
 */
protected void sequence_XCasePart(EObject context, XCasePart semanticObject) {
    genericSequencer.createSequence(context, semanticObject);
}