This lesson demonstrates how to create a rule that reports a violation when methods with more than 17 statements do not have documentation.
In this section:
Designing the Rule Pattern
Designing the rule pattern consists of creating a parent rule node and then adding qualifications to that node so that it fully expresses the code pattern that you wish to search for and remove during static analysis.
Creating the Parent Rule
- Choose File> New> Rule from the menu bar.
- Choose Java from the Dictionary menu in the Rule Tab of the New File panel.
- Enable the By Node option in the Rule Creation box.
- Choose the Declarations> Method node in the Node Selections box.
- Click OK. The method node appears in the right GUI panel.
Adding Additional Qualifications to the Parent Rule Node
For this lesson, we want the rule to check only declared methods, not references to methods.
- Right-click the Method rule node in the workspace panel and choose IsDecl from the shortcut menu.
- To configure static analysis to check the statements in the body of the method, right-click the Method node and choose Body.
- Choose the Statements directory node and click OK.
- Right-click the Body rule node and choose Create Collector from the shortcut menu. A collector symbol (pentagon) will appear below the Body node.
- Right-click the collector and choose Count #.
$$>17in the Expression field. This indicates that we want to check only methods with more than 17 statements. The rule should now look like this:
- To indicate that we want the rule to check Javadocs, right-click the Method node and choose HasJavadoc from the list.
- Right-click HasJavadoc and choose Toggle from the shortcut menu to set the property to F for false. The rule should now look like this:
Specifying an Error Message
We need to specify the text printed when the rule is violated.
- Right-click the parent rule node (the Method rule node) and choose Create Output> Display from the shortcut menu.
- Enter the message you to show when this rule is violated in the Customize Output dialog, e.g.,
Missing Javadoc comment for: $name($name is a variable that will be replaced by the actual name of the method).
Your rule now tells the product to report the specified error message when a method with more than 17 statements does not contain documentation. Your rule is now functionally complete, but you will need to specify a rule ID and header, as well as specify other rule properties. See Customizing Rule Properties for details.
Saving and Enabling Your Rule
Before you can check your rule, you need to save and enable it as described in Saving and Enabling Rules.