Page tree

Skip to end of metadata
Go to start of metadata

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

  1. Choose File> New> Rule from the menu bar.
  2. Choose Java from the Dictionary menu in the Rule Tab of the New File panel.
  3. Enable the By Node option in the Rule Creation box.
  4. Choose the Declarations> Method node in the Node Selections box.
  5. 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. 

  1. Right-click the Method rule node in the workspace panel and choose IsDecl from the shortcut menu.
  2. To configure static analysis to check the statements in the body of the method, right-click the Method node and choose Body
  3. Choose the Statements directory node and click OK
  4. Right-click the Body rule node and choose Create Collector from the shortcut menu. A collector symbol (pentagon) will appear below the Body node.
  5. Right-click the collector and choose Count #.
  6. Enter $$>17 in the Expression field. This indicates that we want to check only methods with more than 17 statements. The rule should now look like this:
  7. To indicate that we want the rule to check Javadocs, right-click the Method node and choose HasJavadoc from the list.
  8. 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. 

  1. Right-click the parent rule node (the Method rule node) and choose Create Output> Display from the shortcut menu.
  2. 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.

  • No labels