Visit Method

In some cases, you may want to check nodes that are deeper than the Member level such as individual Statement nodes. In this case, you can call a method that starts with "Visit" to initiate a drilldown. All child nodes will then be recursively visited. You can override Visit methods logically deeper in the node tree to execute code once nodes of those types are visited by the recursion. In such an override, you must call the base class implementation if you want recursion to continue deeper. For example, the following processes all AssignmentStatement nodes in a Method:

public override ProblemCollection Check(Member member)
{
  Method methd = member as Method;
  if (methd != null)
  {
    VisitStatements(methd.Body.Statements);
  }
  return this.Problems;
}

public override void VisitAssignmentStatement(AssignmentStatement assignment)
{
  // Process assignment statement. Add to this.Problems if problems are found.

  // Uncomment the following if you need to recurse deeper (for example, to the Expression level).
  // base.VisitAssignmentStatement(assignment);
}

The rule processing engine calls a large number (140) of defined in the StandardVisitor class – a parent class to the BaseIntrospectionRule class. We can override the visit methods we are interested in to execute our custom rule processing. In this case we want to verify that each try/catch block is logging any exceptions, so we implement the VisitCatch(CatchNode catchNode) method by overriding the method of the base class.

Note: This requires modification to the Check() method, so that the Check() method calls the Visit Method at a high level.

An example of the visit method implementation for Catch nodes is shown below:

[sourcecode language='CSharp']

public override void VisitCatch(CatchNode catchNode)
{
if(catchNode!=null)
{
if(catchNode.Type == FrameworkTypes.Exception)
{
Debug.WriteLine(“We are catching an exception type”);
}
foreach(Statement statement in catchNode.Block.Statements)
{
Debug.WriteLine(statement.ToString());
}

Problems.Add(new Problem(GetResolution(catchNode),catchNode.SourceContext));
}

base.VisitCatch(catchNode);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值