/// <summary>
        /// Inserts the assert.
        /// </summary>
        /// <param name="element">
        /// The element.
        /// </param>
        /// <param name="anchor">
        /// The anchor.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        private void CheckAssignment(IElement element, IStatement anchor, string name)
        {
            var functionDeclaration = anchor.GetContainingTypeMemberDeclaration() as IMethodDeclaration;
              if (functionDeclaration == null)
              {
            return;
              }

              var body = functionDeclaration.Body;
              if (body == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(element.GetPsiModule());

              var csharpElement = element as ICSharpElement;
              if (csharpElement == null)
              {
            return;
              }

              var code = string.Format("if({0} == null) {{ }}", name);

              var statement = factory.CreateStatement(code);

              var result = body.AddStatementAfter(statement, anchor);

              var range = result.GetDocumentRange();
              var codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }
        /// <summary>Executes the internal.</summary>
        /// <param name="element">The element.</param>
        protected override void Execute(IElement element)
        {
            var statement = this.Provider.GetSelectedElement<IExpressionStatement>(true, true);
              if (statement == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(statement.GetPsiModule());
              if (factory == null)
              {
            return;
              }

              var expression = statement.Expression;
              if ((expression == null) || !(expression.ToTreeNode() is IUnaryExpressionNode))
              {
            return;
              }

              var type = expression.Type();
              if (!type.IsResolved)
              {
            return;
              }

              var declaredType = type as IDeclaredType;
              if (declaredType == null)
              {
            return;
              }

              var enumerate = declaredType.GetTypeElement() as IEnum;
              if (enumerate == null)
              {
            return;
              }

              var typeName = enumerate.ShortName;

              var code = new StringBuilder("switch(");

              code.Append(statement.GetText());

              code.Append(") {");

              foreach (var field in enumerate.EnumMembers)
              {
            code.Append("case ");
            code.Append(typeName);
            code.Append('.');
            code.Append(field.ShortName);
            code.Append(":\r\nbreak;");
              }

              code.Append("default:\r\n");
              code.Append("throw new System.ArgumentOutOfRangeException();");

              code.Append("\r\n}");

              var result = factory.CreateStatement(code.ToString());
              if (result == null)
              {
            return;
              }

              result = statement.ReplaceBy(result);

              var range = result.GetDocumentRange();

              var codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }
        /// <summary>Inserts the assert.</summary>
        /// <param name="assertion">The assertion.</param>
        /// <param name="element">The element.</param>
        private void InsertAssertionCode(string assertion, IElement element)
        {
            IStatement anchor = null;
              string name;

              var assignmentExpression = element as IAssignmentExpression;
              if (assignmentExpression != null)
              {
            anchor = assignmentExpression.GetContainingStatement();

            var referenceExpression = assignmentExpression.Dest as IReferenceExpression;
            if (referenceExpression == null)
            {
              return;
            }

            name = referenceExpression.Reference.GetName();
              }
              else
              {
            var treeNode = element.ToTreeNode();

            while (treeNode != null)
            {
              anchor = treeNode as IStatement;

              if (anchor != null)
              {
            break;
              }

              treeNode = treeNode.Parent;
            }

            var localVariable = element as ILocalVariable;
            if (localVariable == null)
            {
              return;
            }

            name = localVariable.ShortName;
              }

              if (anchor == null)
              {
            return;
              }

              var functionDeclaration = anchor.GetContainingTypeMemberDeclaration() as IMethodDeclaration;
              if (functionDeclaration == null)
              {
            return;
              }

              var body = functionDeclaration.Body;
              if (body == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(element.GetPsiModule());

              var csharpElement = element as ICSharpElement;
              if (csharpElement == null)
              {
            return;
              }

              var code = string.Format(assertion, name);

              var statement = factory.CreateStatement(code);
              if (statement == null)
              {
            return;
              }

              var result = body.AddStatementAfter(statement, anchor);

              var range = result.GetDocumentRange();
              var codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }
        /// <summary>Generates the function assert statements.</summary>
        /// <param name="classDeclaration">The class declaration.</param>
        /// <param name="isPublic">if set to <c>true</c> [is public].</param>
        private void ConvertToStringConstant(IClassDeclaration classDeclaration, bool isPublic)
        {
            var element = this.GetElementAtCaret();
              if (element == null)
              {
            return;
              }

              var treeNode = element as ITreeNode;
              if (treeNode == null)
              {
            return;
              }

              var expression = treeNode.Parent as ICSharpExpression;
              if (expression == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(element.GetPsiModule());
              if (factory == null)
              {
            return;
              }

              var text = treeNode.GetText();

              var identifier = GetExistingIdentifier(classDeclaration, text);

              if (string.IsNullOrEmpty(identifier))
              {
            identifier = this.GetIdentifier(classDeclaration, text);

            var declarationText = string.Format("const string {0} = {1};", identifier, text);

            if (isPublic)
            {
              declarationText = "public " + declarationText;
            }

            if (IntroduceStringConstantSettings.Instance.GenerateXmlComment)
            {
              declarationText = "/// <summary>" + text + "</summary>\r\n" + declarationText;
            }

            var classMemberDeclaration =
              factory.CreateTypeMemberDeclaration(declarationText) as IClassMemberDeclaration;
            if (classMemberDeclaration == null)
            {
              return;
            }

            var anchor = GetClassMemberAnchor(classDeclaration, identifier);

            if (anchor != null)
            {
              classDeclaration.AddClassMemberDeclarationBefore(classMemberDeclaration, anchor);
            }
            else
            {
              classDeclaration.AddClassMemberDeclaration(classMemberDeclaration);
            }
              }

              if (isPublic)
              {
            var qualifiedName = GetQualifiedClassDeclarationName(classDeclaration);

            if (!string.IsNullOrEmpty(qualifiedName))
            {
              identifier = qualifiedName + "." + identifier;
            }
              }

              var identifierExpression = factory.CreateExpression(identifier);
              if (identifierExpression == null)
              {
            return;
              }

              var result = expression.ReplaceBy(identifierExpression);

              var languageService = CSharpLanguageService.CSHARP.Service;
              if (languageService == null)
              {
            return;
              }

              var formatter = languageService.CodeFormatter;
              if (formatter == null)
              {
            return;
              }

              var range = result.GetDocumentRange();
              var codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }
        /// <summary>
        /// Executes the internal.
        /// </summary>
        /// <param name="element">
        /// The element.
        /// </param>
        protected override void Execute(IElement element)
        {
            if (string.IsNullOrEmpty(ValueAnalysisSettings.Instance.AllowNullAttribute))
              {
            return;
              }

              if (!this.IsAvailableInternal())
              {
            return;
              }

              var typeMemberDeclaration = this.GetTypeMemberDeclaration();
              if (typeMemberDeclaration == null)
              {
            return;
              }

              var attributesOwnerDeclaration = typeMemberDeclaration as IAttributesOwnerDeclaration;
              if (attributesOwnerDeclaration == null)
              {
            return;
              }

              var typeElement = GetAttribute(typeMemberDeclaration, ValueAnalysisSettings.Instance.AllowNullAttribute);
              if (typeElement == null)
              {
            return;
              }

              var factory = CSharpElementFactory.GetInstance(typeMemberDeclaration.GetPsiModule());

              var attribute = factory.CreateTypeMemberDeclaration("[" + ValueAnalysisSettings.Instance.AllowNullAttribute + "(\"*\")]void Foo(){}", new object[]
              {
              }).Attributes[0];

              attribute = attributesOwnerDeclaration.AddAttributeAfter(attribute, null);

              var name = attribute.TypeReference.GetName();
              if (!name.EndsWith("Attribute"))
              {
            return;
              }

              /*
              IReferenceName referenceName = factory.CreateReferenceName(name.Substring(0, name.Length - "Attribute".Length), new object[0]);
              referenceName = attribute.Name.ReplaceBy(referenceName);

              if (referenceName.Reference.Resolve().DeclaredElement != typeElement)
              {
            referenceName.Reference.BindTo(typeElement);
              }
              */

              var range = attribute.GetDocumentRange();
              CodeFormatter codeFormatter = new CodeFormatter();
              codeFormatter.Format(this.Solution, range);
        }
        /// <summary>
        /// Executes this instance.
        /// </summary>
        /// <param name="solution">The solution.</param>
        private void Execute(ISolution solution)
        {
            var returnStatement = this.warning.ReturnStatement;

              var function = returnStatement.GetContainingTypeMemberDeclaration() as IFunction;
              if (function == null)
              {
            return;
              }

              var type = function.ReturnType;

              var rule = Rule.GetRule(type, function.Language) ?? Rule.GetDefaultRule();
              if (rule == null)
              {
            return;
              }

              var code = rule.ReturnAssertion;

              var expression = returnStatement.Value.GetText();
              var typeName = type.GetLongPresentableName(returnStatement.Language);

              code = "return " + string.Format(code, expression, typeName) + ";";

              var factory = CSharpElementFactory.GetInstance(returnStatement.GetPsiModule());

              var statement = factory.CreateStatement(code);

              var result = returnStatement.ReplaceBy(statement);

              var range = result.GetDocumentRange();
              CodeFormatter codeFormatter = new CodeFormatter();
              codeFormatter.Format(solution, range);
        }