SubMain - CodeIt.Right The First Time!

/Community

Support Community for SubMain Products
 Home Products Services Download Purchase Support
in Search
 
Home Forums Blogs Tutorials/CIR Tutorials/GD Downloads
Welcome to SubMain Community Sign in | Join | Help

Protected Methods in aspx file should have try catch ?

Last post 10-22-2014, 1:25 AM by Misha Zhutov (SubMain). 17 replies.
Page 1 of 2 (18 items)   1 2 Next >
Sort Posts: Previous Next
  •  11-27-2013, 10:30 PM 3530

    Protected Methods in aspx file should have try catch ?

    Hi,

    I have to create a Custom Rule to check if Protected Methods in aspx file should have try catch

     IMethodInfo methodInfo = element as IMethodInfo;
                if (methodInfo != null)
                {

                    CodeObjectModel.Reflection.IMethod method = CodeElementFactory.GetMethod(methodInfo);
                    if (method != null)
                    {
                        if (method.IsProtected)
                        {

     

                        }
                    }
                }

    regards,
    Sushil nembhani

     

  •  11-27-2013, 11:51 PM 3531 in reply to 3530

    Re: Protected Methods in aspx file should have try catch ?

    Hi Sushil,

    It is not clear from your post what's the question. How to implement the rule?

    Could you please send us code sample that violates the rule?

     

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  11-28-2013, 3:07 AM 3532 in reply to 3531

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    I have to check if the method contains try/catch by checking the method code contents

     IMethodInfo methodInfo = element as IMethodInfo;
    if (methodInfo != null)
    {

    CodeObjectModel.Reflection.IMethod method = CodeElementFactory.GetMethod(methodInfo);
    if (method != null)
    {
    if (method.IsProtected)
    {
    // how would i check the code in the method contains try/catch or not 

    }
    }

    regards,
    Sushil Nembhani

  •  11-28-2013, 4:01 AM 3533 in reply to 3532

    Re: Protected Methods in aspx file should have try catch ?

    Sushil,

    Please use the following code:

    IMethodInfo methodInfo = element as IMethodInfo;
    if (methodInfo != null && methodInfo.IsProtected && RuleUtilities.IsAspProjectFile(methodInfo.ProjectFile))
    {    
     StatementCollection statements = RuleUtilities.GetMethodStatements(methodInfo);
     if (statements != null)
     {
      if (SearchForTryCatchStatement(statements, methodInfo.CaseSensitive))
      {
       return true;
      }
     }
    }

    private bool SearchForTryCatchStatement(StatementCollection statements)
    {
     int statementsCount = statements.Count;
     for (int i = 0; i < statementsCount; i++)
     {
      IStatement statement = statementsIdea;
      if (statement.StatementType == StatementType.Try)
      {
       return true;
      }

      if (statement.Statements.Count > 0)
      {
       if (SearchForTryCatchStatement(statement.Statements))
       {
        // TODO
       }
      }
     }

     return false;
    }

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  12-04-2013, 10:29 PM 3537 in reply to 3533

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    Thanks for the code snippet.

    I wanted to check if my asp.net page & usercontrols have try/catch for all the protected methods.

    I tried the below code for asp.net page but does not seem to give the required violations

     public override bool Check(IElementInfo element)
            {
                ITypeInfo typeInfo = element as ITypeInfo;

                IMethodInfo methodInfo = element as IMethodInfo;
                if (methodInfo != null && methodInfo.IsProtected && RuleUtilities.IsAspProjectFile(methodInfo.ProjectFile))
                {

                    StatementCollection statements = RuleUtilities.GetMethodStatements(methodInfo);
                    if (statements != null)
                    {
                        if (!SearchForTryCatchStatement(statements))
                        {
                            return true;
                        }
                    }
                }

                // return False when there is NO violation
                return false;

            }

    --------------------------------------------------------------------


    private bool SearchForTryCatchStatement(StatementCollection statements)
            {
                int statementsCount = statements.Count;
                for (int i = 0; i < statementsCount; i++)
                {
                    IStatement statement = statementsIdea;
                    if (statement.StatementType == StatementType.Try)
                    {
                        return true;
                    }

                    if (statement.Statements.Count > 0)
                    {
                        if (SearchForTryCatchStatement(statement.Statements))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }

    regards,
    Sushil Nembhani



    regards,
    Sushil nembhani

  •  12-05-2013, 2:20 AM 3538 in reply to 3537

    Re: Protected Methods in aspx file should have try catch ?

    Sushil,

    Please target your rule to Class and use the following Check method:

    public override bool Check(IElementInfo element)
    {
     IClassInfo classInfo = element.ToClassInfo();
     if (classInfo != null)
     {
      IMemberInfo[] members = classInfo.GetMembers();
      if (members != null)
      {
       foreach (IMemberInfo memberInfo in members)
       {
        IMethodInfo methodInfo = memberInfo as IMethodInfo;
        if (methodInfo != null && methodInfo.IsProtected &&
         RuleUtilities.IsAspProjectFile(methodInfo.ProjectFile) &&
         !methodInfo.IsConstructor) // skip ASP.NET page constructor!
        {
         StatementCollection statements = RuleUtilities.GetMethodStatements(methodInfo);
         if (statements != null)
         {
          if (!SearchForTryCatchStatement(statements))
          {
           return true;
          }
         }
        }
       }
      }

     return false;
     }

    Please let us know if that helps.


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  12-05-2013, 3:03 AM 3539 in reply to 3538

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    Thanks for the code snippet. It is giving the violation for ASP.NET but have two more issues

    1. If the page has more than one violation then also it gives just one always
    2. If we click on "Go to source code" it does not go the method with the violation.


    Also just wanted to confirm if the method IsASPProjectFile will check for usercontrols or only asp.net pages ?

    regards,
    Sushil Nembhani

     

  •  12-05-2013, 5:27 AM 3540 in reply to 3539

    Re: Protected Methods in aspx file should have try catch ?

    1. Each rule can raise only one violation. So, either you target Class and have one violation per class or you target Method and have violations in all protected methods that don't have try-catch.

    2. Use the following code to navigate to method:

    base.SetDisplayedElement(methodInfo);
    return true;

    .SetDisplayedElement(methodInfo);
    return true;

    in Check method when you find a method that does not contain try-catch.


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  12-06-2013, 12:02 AM 3541 in reply to 3540

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    I have tried the above code by setting Target to Method & code to navigate. After changing this code it does not gives any violations. In my previous code for class it was giving one violation.

    Please find the below Rule Class CheckTry.cs

    #undef UseStatementAsTarget
    using System;
    using System.IO;
    using System.Text;
    using System.Reflection;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Runtime.Serialization;
    using System.Globalization;
    using System.Resources;

    using SubMain.CodeItRight.Sdk;
    using SubMain.CodeItRight.Sdk.Rules;
    using SubMain.CodeItRight.Sdk.Core.Collections;
    using SubMain.CodeItRight.Sdk.Core.Exceptions;
    using SubMain.CodeItRight.Sdk.Core.Reflection;
    using SubMain.CodeItRight.Sdk.Core.Rules;
    using SubMain.CodeItRight.Sdk.Core.Utils;
    using SubMain.CodeItRight.Sdk.Utils;
    using SubMain.CodeObjectModel.Reflection;

    //using SubMain.Core.Services;

    namespace SubMain.CodeItRight.Rules.AspNet
    {
        /// <summary>
        /// Template for new rule created by wizard
        /// </summary>
        [Serializable]
        [ModificationDate("11/20/2013")]
        [Category("SubMain.AspNet")]
        public class CheckTry : RuleBase
        {
            #region Template to be populated by rule author

            public override string[] AutoCorrectionOptions
            {
                get
                {
                    return new string[] { };
                }
            }

            public override bool Check(IElementInfo element)
            {
               
                IClassInfo classInfo = element.ToClassInfo();
                if (classInfo != null)
                {
                    IMemberInfo[] members = classInfo.GetMembers();
                    if (members != null)
                    {
                        foreach (IMemberInfo memberInfo in members)
                        {
                            IMethodInfo methodInfo = memberInfo as IMethodInfo;
                            if (methodInfo != null && methodInfo.IsProtected &&
                             RuleUtilities.IsAspProjectFile(methodInfo.ProjectFile) &&
                             !methodInfo.IsConstructor) // skip ASP.NET page constructor!
                            {
                                StatementCollection statements = RuleUtilities.GetMethodStatements(methodInfo);
                                if (statements != null)
                                {
                                    if (!SearchForTryCatchStatement(statements))
                                    {
                                        base.SetDisplayedElement(methodInfo);
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }

                return false;
            }

            private bool SearchForTryCatchStatement(StatementCollection statements)
            {
                int statementsCount = statements.Count;
                for (int i = 0; i < statementsCount; i++)
                {
                    IStatement statement = statementsIdea;
                    if (statement.StatementType == StatementType.Try)
                    {
                        return true;
                    }

                    if (statement.Statements.Count > 0)
                    {
                        if (SearchForTryCatchStatement(statement.Statements))
                        {
                            return true;
                        }
                    }
                }

                return false;
            }


            private bool Correct_0(IElementInfo elementInfo)
            {
                // TODO
                return false;
            }

          

            #endregion

            #region Wizard Generated Code

            #region Data Entered On Wizard form

            private const string TITLE = "ASP.NET Protected methods should have Try/Catch";
            private const SeverityLevel SEVERITY = SeverityLevel.Warning;
            private const RuleScopes SCOPES = RuleScopes.Any;
            private const RuleTargets AVAILABLE_TARGETS = RuleTargets.Method;

            #endregion

            #region Constructor

            public CheckTry()
            {
                this.Title = TITLE;
                this.Severity = SEVERITY;
                this.Targets = this.AvailableTargets;
                this.Scopes = SCOPES;
            }

            #endregion

            public override string ID
            {
                get
                {
                    return "0CEEB5F5-5D87-4b5b-AEE7-A119F56B4915";
                }
            }

            public override RuleTargets AvailableTargets
            {
                get
                {
                    return AVAILABLE_TARGETS;
                }
            }

            public override string ViolationDescription
            {
                get
                {
                    if (Element != null)
                    {
                        return "ASP.NET Protected methods should have Try/Catch";
                    }

                    return string.Empty;
                }
            }

            public override string CorrectDescription
            {
                get
                {
                    if (Element != null)
                    {
                        return "";
                    }

                    return string.Empty;
                }
            }

            public override bool Correct(int correctionOptionIndex)
            {
                base.Correct(correctionOptionIndex);
                switch (correctionOptionIndex)
                {
                    case 0:
                        return Correct_0(Element);
                    default:
                        break;
                }

                return false;
            }

            #endregion
        }
    }

     

    regards,
    Sushil Nembhani

  •  12-06-2013, 12:08 AM 3542 in reply to 3541

    Re: Protected Methods in aspx file should have try catch ?

    Sushil,

    In your Check method the first line is

    IClassInfo classInfo = element.ToClassInfo();

    but your rule targets Method. Change rule target on Class:

    private const RuleTargets AVAILABLE_TARGETS = RuleTargets.Class;

    and your code will work.

     

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  12-06-2013, 1:02 AM 3543 in reply to 3542

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    My code is working for the class level but i have to implement at the method level
    As per my below requirements

    1. If the page has more than one violation it should give all the violations
    2. If we click on "Go to source code" it does should go the method with the violation.

    I have the done the below but its not working

    1. Set the target to  Method to get violations in all protected methods that don't have try-catch.
    2. Use the following code to navigate to method:
    base.SetDisplayedElement(methodInfo);
    return true;

    Anything else i need to do in the check method. Is it possible to send the check method implementation for method level ?


    regards,
    Sushil Nembhani

  •  12-06-2013, 2:15 AM 3544 in reply to 3543

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    I was able to solve the issue by changing the Check Method with MethodInfo Instead of ClassInfo.

    I have just one query related to which file to analyze for checking these violations.
    1. If i select the aspx file & click "analyze file" it does not give the violations
    2. If the select the aspx.vb file & then click "analyze file" it gives the violations

    regards,
    Sushil Nembhani

  •  12-06-2013, 2:19 AM 3545 in reply to 3544

    Re: Protected Methods in aspx file should have try catch ?

    Attachment: CheckTry.zip

    Please find CheckTry.zip file attached.

    Analyze File command analyzes only ONE file. Does your ASPX file have any protected methods? Why should it generate violation?

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  12-06-2013, 2:29 AM 3546 in reply to 3545

    Re: Protected Methods in aspx file should have try catch ?

    Hi,

    Thanks for the code snippet.

    My only query was when we click analyze file i have to do it seperately for aspx & code behind file i.e. aspx.vb ?

    regards,
    Sushil Nembhani

  •  12-06-2013, 2:32 AM 3547 in reply to 3546

    Re: Protected Methods in aspx file should have try catch ?

    Yes, you have to do it separately for ASPX and code-behind file.

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
Page 1 of 2 (18 items)   1 2 Next >
View as RSS news feed in XML
 
     
 
Home |  Products |  Services |  Download |  Purchase |  Support |  Community |  About Us |