/// <summary>Performs analyze of <paramref name="thrownException"/>.</summary>
        /// <param name="thrownException">Thrown exception to analyze.</param>
        public override void Visit(ThrownExceptionModel thrownException)
        {
            if (thrownException == null)
                return;

            if (!thrownException.AnalyzeUnit.IsInspectionRequired || thrownException.IsCaught || thrownException.IsExceptionDocumented)
                return;

            var isOptional = IsSubtypeDocumented(thrownException) ||
                             IsThrownExceptionSubclassOfOptionalException(thrownException) ||
                             IsThrownExceptionThrownFromExcludedMethod(thrownException) ||
                             thrownException.IsThrownFromAnonymousMethod;

            if (thrownException.IsEventInvocationException)
            {
                if (ServiceLocator.Settings.DelegateInvocationsMayThrowExceptions)
                {
                    var highlighting = new EventExceptionNotDocumentedHighlighting(thrownException);
                    ServiceLocator.StageProcess.AddHighlighting(highlighting, thrownException.DocumentRange);
                }
            }
            else
            {
                var highlighting = isOptional
                    ? new ExceptionNotDocumentedOptionalHighlighting(thrownException)
                    : new ExceptionNotDocumentedHighlighting(thrownException);
                ServiceLocator.StageProcess.AddHighlighting(highlighting, thrownException.DocumentRange);
            }
        }
        internal bool IsSupertypeOf(ThrownExceptionModel thrownException)
        {
            var exceptionType = GetExceptionType();
            if (exceptionType == null)
                return false;

            return thrownException.ExceptionType.IsSubtypeOf(exceptionType);
        }
        public static ArgumentNullExceptionDescription CreateFrom(ThrownExceptionModel exception)
        {
            if ("System.ArgumentNullException".Equals(exception.ExceptionType.GetClrName().FullName))
            {
                return(new ArgumentNullExceptionDescription(exception.ExceptionsOrigin.Node as IThrowStatement));
            }

            return(null);
        }
        public static ArgumentNullExceptionDescription CreateFrom(ThrownExceptionModel exception)
        {
            if ("System.ArgumentNullException".Equals(exception.ExceptionType.GetClrName().FullName))
            {
                return new ArgumentNullExceptionDescription(exception.ExceptionsOrigin.Node as IThrowStatement);
            }

            return null;
        }
        private bool IsThrownExceptionSubclassOfOptionalException(ThrownExceptionModel thrownExceptionModel)
        {
            var optionalExceptions = ServiceLocator.Settings.GetOptionalExceptions();

            if (thrownExceptionModel.IsThrownFromThrowStatement)
                return optionalExceptions.Any(e => e.ReplacementType != OptionalExceptionReplacementType.InvocationOnly &&
                    thrownExceptionModel.ExceptionType.IsSubtypeOf(e.ExceptionType));
            else
                return optionalExceptions.Any(e => e.ReplacementType != OptionalExceptionReplacementType.ThrowOnly &&
                    thrownExceptionModel.ExceptionType.IsSubtypeOf(e.ExceptionType));
        }
        /// <summary>
        /// Performs analyze of <paramref name="thrownException"/>.
        /// </summary>
        /// <param name="thrownException">
        /// Thrown exception to analyze.
        /// </param>
        public override void Visit(ThrownExceptionModel thrownException)
        {
            // add a squiggle if the throwing a Exception (new Exception())
            // throw; statements are ignored
            if (thrownException.IsThrownFromThrowStatement &&
                thrownException.FullName == "System.Exception" &&
                !thrownException.IsRethrow)
            {
                var highlight = new ThrowingSystemExceptionHighlighting();
                var range = thrownException.DocumentRange;

                ServiceLocator.StageProcess.AddHighlighting(highlight, range);
            }
        }
 private bool ThrowsExceptionOrSubtype(ExceptionDocCommentModel exceptionDocumentation, ThrownExceptionModel thrownException)
 {
     if (thrownException.IsThrownFromThrowStatement)
     {
         if (ServiceLocator.Settings.IsDocumentationOfExceptionSubtypeSufficientForThrowStatements)
             return thrownException.IsExceptionOrSubtype(exceptionDocumentation);
     }
     else
     {
         if (ServiceLocator.Settings.IsDocumentationOfExceptionSubtypeSufficientForReferenceExpressions)
             return thrownException.IsExceptionOrSubtype(exceptionDocumentation);
     }
     return false;
 }
        private bool IsSubtypeDocumented(ThrownExceptionModel thrownException)
        {
            if (thrownException.IsThrownFromThrowStatement)
            {
                if (ServiceLocator.Settings.IsDocumentationOfExceptionSubtypeSufficientForThrowStatements && thrownException.IsExceptionOrSubtypeDocumented)
                    return true;
            }
            else
            {
                if (ServiceLocator.Settings.IsDocumentationOfExceptionSubtypeSufficientForReferenceExpressions && thrownException.IsExceptionOrSubtypeDocumented)
                    return true;
            }

            return false;
        }
        private bool IsThrownExceptionThrownFromExcludedMethod(ThrownExceptionModel thrownException)
        {
            var parent = thrownException.ExceptionsOrigin as ReferenceExpressionModel;
            if (parent != null)
            {
                var node = parent.Node;
                var resolveResult = node.Reference.CurrentResolveResult;
                if (resolveResult != null)
                {
                    var element = resolveResult.DeclaredElement as IXmlDocIdOwner;
                    if (element != null && (resolveResult.DeclaredElement is IMethod || resolveResult.DeclaredElement is IProperty))
                    {
                        // remove generic placeholders ("`1") and method signature ("(...)")
                        var fullMethodName = Regex.Replace(element.XMLDocId.Substring(2), "(`+[0-9]+)|(\\(.*?\\))", ""); // TODO: merge with other

                        var excludedMethods = ServiceLocator.Settings.GetOptionalMethodExceptions();
                        return excludedMethods.Any(t => t.FullMethodName == fullMethodName && t.IsSupertypeOf(thrownException));
                    }
                }
            }
            return false;
        }
Esempio n. 10
0
        public ExceptionDocCommentModel AddExceptionDocumentation(ThrownExceptionModel thrownException, IProgressIndicator progressIndicator)
        {
            if (thrownException.ExceptionType == null)
            {
                return(null);
            }

            var exceptionDescription = thrownException.ExceptionDescription.Trim();

            if (thrownException.ExceptionsOrigin is ThrowStatementModel)
            {
                if (thrownException.ExceptionType.GetClrName().FullName == "System.ArgumentNullException")
                {
                    exceptionDescription = ArgumentNullExceptionDescription.CreateFrom(thrownException).GetDescription().Trim();
                }
            }
            else
            {
                exceptionDescription = Regex.Replace(exceptionDescription, "<paramref name=\"(.*?)\"/>", m => m.Groups[1].Value).Trim();
            }

            var exceptionDocumentation = string.IsNullOrEmpty(exceptionDescription)
                ? string.Format("<exception cref=\"T:{0}\">" + Constants.ExceptionDescriptionMarker + ".</exception>{1}", thrownException.ExceptionType.GetClrName().FullName, Environment.NewLine)
                : string.Format("<exception cref=\"T:{0}\">{1}</exception>{2}", thrownException.ExceptionType.GetClrName().FullName, exceptionDescription, Environment.NewLine);

            if (thrownException.ExceptionsOrigin.ContainingBlock is AccessorDeclarationModel)
            {
                var accessor = ((AccessorDeclarationModel)thrownException.ExceptionsOrigin.ContainingBlock).Node.NameIdentifier.Name;
                exceptionDocumentation = string.IsNullOrEmpty(exceptionDescription)
                    ? string.Format("<exception cref=\"T:{0}\" accessor=\"{1}\">" + Constants.ExceptionDescriptionMarker + ".</exception>{2}", thrownException.ExceptionType.GetClrName().FullName, accessor, Environment.NewLine)
                    : string.Format("<exception cref=\"T:{0}\" accessor=\"{1}\">{2}</exception>{3}", thrownException.ExceptionType.GetClrName().FullName, accessor, exceptionDescription, Environment.NewLine);
            }

            ChangeDocumentation(_documentationText + "\n" + exceptionDocumentation);

            return(DocumentedExceptions.LastOrDefault());
        }
 /// <summary>Initializes a new instance of the <see cref="EventExceptionNotDocumentedHighlighting"/> class. </summary>
 /// <param name="thrownException">The thrown exception. </param>
 internal EventExceptionNotDocumentedHighlighting(ThrownExceptionModel thrownException)
     : base(thrownException)
 {
 }
 /// <summary>Initializes a new instance of the <see cref="ExceptionNotDocumentedOptionalHighlighting"/> class. </summary>
 /// <param name="thrownException">The thrown exception. </param>
 internal ExceptionNotDocumentedOptionalHighlighting(ThrownExceptionModel thrownException)
 {
     ThrownException = thrownException;
 }
 /// <summary>Performs analyze of <paramref name="thrownException"/>.</summary>
 /// <param name="thrownException">Thrown exception to analyze.</param>
 public virtual void Visit(ThrownExceptionModel thrownException)
 {
 }