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);
}