This lesson demonstrates how to create a rule that reports a violation when instance fields do not begin with an underscore.
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 Node
Whenever you create a rule, the first thing that you need to do is activate the desired node dictionary and then select the node that you want to be the subject of your rule. To begin creating this rule:
- Choose File> New> Rule from the menu bar. The New File panel opens.
- Choose Java from the Dictionary menu in the Rule Tab of the New File panel.
The Java dictionary contains nodes used to build rules concerning the language structure. The JavaText dictionary contains nodes used to build rules concerning the text of the Java source file. Generally, if you are building a rule that depends on whitespace, comment styles, or the format of the source file, you use the JavaText dictionary. If you are building a rule that depends on the logical structure of the language, you use the Java dictionary. - Enable the By Node option from the Rule Creation box.
- Choose the Declarations> Variables> Field node in the Node Selections box.
- Click OK. The Field node appears in the right GUI panel.
Adding Additional Qualifications to the Parent Rule Node
- Right-click the Field rule node and choose the IsStatic property from the shortcut menu to specify the rule should check for instance (non-static) fields. This property checks if the field is static. This is not what we want to check for, so we will need to modify this node.
- Right-click the IsStatic box and choose ToggleTo to change the rule condition and enable the rule to check if an instance variable is present.
- Right-click the Field rule node and choose Name from the shortcut menu. This enables you to specify that this rule will be about naming conventions.
In the Regexp field of the dialog, specify a value the product should look for in the name of the instance variable.
If you want to report an error when a specific value is missing in the code, (as we do in our example), enter the value and enable the Negate option. This tells the product to report an error if the specified value is not present. Disable this option to report an error when a specific value is present in the code.We want the Name value to begin with an underscore in our example, so we will enter
^_.
in the Regexp field and enable the Negate check box. The caret character (^) indicates the beginning of an expression and the dot character (.) matches any single character.- Click OK. The rule will 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 Field rule node in this example) and choose Create Output> Display to open the Customize Output dialog.
- Enter the message you want delivered when this rule is violated, e.g.,
Invalid field name: $name
. When this message is reported by the product,$name
will be replaced by the actual name of the field.
- Click OK. The rule will now look like this:
The rule now reports the specified error message when an instance variable’s name does not begin with an underscore. The rule is now complete. After you customize this rule's properties and save it, the product will be able to enforce it.
Customizing Rule Properties
You can specify the rule ID and rule header (name for the rule) in the Rule Properties panel. To access this panel, right-click the empty area of the right panel and choose Properties from the shortcut menu.
The following properties are required:
- Rule ID: The unique ID you want to assign to this rule. Use the following format to organize your custom rules into categories:
category.id
(e.g.,Example.IFM)
. - Header: The name you want assigned to this rule. For this example, you could enter
Instance fields should start with the character “_”
.
You can assign a severity to the rule by choosing a severity from the drop-down menu. We'll assign this example rule a Medium severity. See Customizing Rule Properties for additional information about rule properties.
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.