private void FromIfThrowToRequires(IPrecondition assertion, bool isGeneric) { // Convertion from if-throw precondition to Contract.Requires // contains following steps: // 1. Negate condition from the if statement (because if (s == null) throw ANE means that Contract.Requires(s != null)) // 2. Create Contract.Requires expression (with all optional generic argument and optional message // 3. Add required using statements if necessary (for Contract class and Exception type) // 4. Replace if-throw statement with newly created contract statement var ifThrowAssertion = (IfThrowPrecondition)assertion; ICSharpExpression negatedExpression = CSharpExpressionUtil.CreateLogicallyNegatedExpression(ifThrowAssertion.IfStatement.Condition); Contract.Assert(negatedExpression != null); string predicateCheck = negatedExpression.GetText(); ICSharpStatement newStatement = null; if (isGeneric) { newStatement = CreateGenericContractRequires(ifThrowAssertion.ExceptionTypeName, predicateCheck, ifThrowAssertion.Message); } else { newStatement = CreateNonGenericContractRequires(predicateCheck, ifThrowAssertion.Message); } ReplaceStatements(ifThrowAssertion.CSharpStatement, newStatement); }
protected override IIfStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression) { var template = "if($0)" + EmbeddedStatementBracesTemplate; var statement = (IIfStatement)factory.CreateStatement(template, expression); var negated = CSharpExpressionUtil.CreateLogicallyNegatedExpression(statement.Condition); statement.Condition.ReplaceBy(negated.NotNull()); return(statement); }
protected override ICSharpExpression CreateExpression(CSharpElementFactory factory, ICSharpExpression expression) { return(CSharpExpressionUtil.CreateLogicallyNegatedExpression(expression)); }