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

How to get ICodeRegionInfo from IElementInfo when target is method?

Last post 11-06-2014, 8:44 PM by abdulrauf. 6 replies.
Sort Posts: Previous Next
  •  11-05-2014, 11:09 PM 4014

    How to get ICodeRegionInfo from IElementInfo when target is method?

    I am trying to get the souse code of methods. So my target is methods. My check method is 

            public override bool Check(IElementInfo element)

            {

                //ICodeRegionInfo region = (ICodeRegionInfo)element; //type casting failed

                //ICodeRegionInfo region = element as ICodeRegionInfo; //returning null because of course typecasting failed

               // How to get coderegioninfo ?? 

                SubMain.Core.ExceptionHandling.UnhandledExceptionManager.LogHandler(region.RawCode, "log.txt"); 

     

                return false;

            }

     

     

    I know how to get code region when target is project. 

                IProjectInfo pinfo = (IProjectInfo)element;

                IProjectFileInfo[] pfiles = pinfo.GetProjectFiles();

                for (int i = 0; i < pfiles.Length; i++)

                {

                    ICodeRegionInfo[] regions = pfilesIdea.GetCodeRegions();

                    for (int j = 0 ; j < regions.Length;j++)

                    {

                        SubMain.Core.ExceptionHandling.UnhandledExceptionManager.LogHandler(regions[j].RawCode, path);

                    }

                } 

     

                return true; 

     

     

    Filed under: ,
  •  11-06-2014, 12:26 AM 4016 in reply to 4014

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Hello,

    You can cast Element to ICodeRegionInfo only when rule target is CodeRegion. What rule would you like to do?

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  11-06-2014, 1:40 AM 4017 in reply to 4016

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Misha Zhutov (SubMain):

    Hello,

    You can cast Element to ICodeRegionInfo only when rule target is CodeRegion. What rule would you like to do?

     

    I want to make a rule that checks that open method call should be followed by close method call in every method (target:method).

    For example

    main()

    {

    open(); 

     

    as there is not close() call after open() anywhere in code so rule should raise warning.

    but if

     main()

    {

    open(); 

    //some code

    close(); 

     now rule shouldn't raise any warning. 

  •  11-06-2014, 2:00 AM 4019 in reply to 4017

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Hi,

    You do not need CodeRegion as well as ICodeRegionInfo. You need a rule with Method as a target, then you need to enumerate all statements/expressions and find Invocation expression and ensure that its name is Open. Then, try to find the same way for Close invocation expression.

    Please take a look at SubMain.CodeItRight.Rules.Usage.dll -> AlwaysCloseSQLResources rule -> FindOpened and FindClosed methods. These are exactly what you need.

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  11-06-2014, 3:35 AM 4022 in reply to 4019

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Misha Zhutov (SubMain):

    Hi,

    You do not need CodeRegion as well as ICodeRegionInfo. You need a rule with Method as a target, then you need to enumerate all statements/expressions and find Invocation expression and ensure that its name is Open. Then, try to find the same way for Close invocation expression.

    Please take a look at SubMain.CodeItRight.Rules.Usage.dll -> AlwaysCloseSQLResources rule -> FindOpened and FindClosed methods. These are exactly what you need.

     

     

    Thanks but there is another problem. I have written a code by matching expressions which is working fine but I am unable to get expressions inside try or catch or finally block inside function. There should be a way to recursively get statements of every block inside function. My check method is :

     

                string path = @"E:/logs.txt";

                IMethodInfo minfo = (IMethodInfo)element;

                SubMain.Core.ExceptionHandling.UnhandledExceptionManager.LogHandler("Entering "+minfo.DisplayName, path);

                StatementCollection statements = RuleUtilities.GetMethodStatements(minfo);

                bool flag = false;

                if (statements != null)

                {

                    int statementsCount = statements.Count;

                    for (int i = 0; i < statementsCount; i++)

                    {

                        IStatement statement = statementsIdea;

                        ExpressionCollection expressions = statement.Expressions;

                        for (int j = 0 ; j < expressions.Count;j++)

                        {

                            if (expressions[j].ToString() == "PO.Open()")

                            {

                                flag = true;

                            }

                            else

                            {

                                if ((expressions[j].ToString() == "PO.Close()" || expressions[j].ToString() == "PO.Dispose()") && flag == true)

                                {

                                    flag = false;

                                }

                            }

     

                            SubMain.Core.ExceptionHandling.UnhandledExceptionManager.LogHandler(expressions[j].ToString(), path);

                        }

     

                    }

                }

     

                return flag; 

     

    Lets consider an example

            public void foo()

            {

                PO.Open(); //this expression is successfully detected

                try

                {

                    //failed to detect expressions inside block

                }

                catch

                {

                    //failed to detect expressions inside block

                }

                finally

                {

    PO.Close(); 

                    //failed to detect expressions inside block

                }

     

            } 

     

    I have even tried innerexpressions property. but failed to statements inside try catch finally as a string

  •  11-06-2014, 4:54 AM 4023 in reply to 4022

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Hi,

    RuleUtilities.GetMethodStatements returns top level statements.
    Each statement (IStatement statement = statementsIdea) has Statements collection.

    You need to enumerate recursively statement and its statements.

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
  •  11-06-2014, 8:44 PM 4025 in reply to 4023

    Re: How to get ICodeRegionInfo from IElementInfo when target is method?

    Misha Zhutov (SubMain):

    Hi,

    RuleUtilities.GetMethodStatements returns top level statements.
    Each statement (IStatement statement = statementsIdea) has Statements collection.

    You need to enumerate recursively statement and its statements.

     

     Hi, Can you please tell me where is documentation of Istatement ? I am unable to find the documentation of submain.codeobjectmodel.reflection on your docs site http://submain.com/codeit.right/sdk/ 

View as RSS news feed in XML
 
     
 
Home |  Products |  Services |  Download |  Purchase |  Support |  Community |  About Us |