Lesson 2 - Performing Static Analysis
During static analysis analyzes source code to expose violations of coding standards, language specific "rules" that help you prevent errors.
Jtest enforces the following types of coding standards:
- Traditional coding standards: Traditional coding standards are rules that apply to constructs within the class under test. A traditional coding standard might test whether or not a file's source code contains a construct that has a high probability of resulting in an error. For example, one traditional coding standard checks that you use "equals" instead of "==" when comparing strings (writing "==" when you should have used "equals" causes the program to check if two strings are identical, rather than check if two strings have the same value).
- Global coding standards: Global coding standards are rules that ensure that projects use fields, methods, and classes wisely. A global coding standard might check that a project does not contain logical flaws and unclear code such as unused or overly-accessible fields, methods, and classes; such problems increase the probability of an error being introduced into the code, and may also make the code less object-oriented.
- Metrics: Metrics are measures of the size and complexity of code. When Jtest performs static analysis, it also measures your class's and (if applicable) project's metrics; it reports all metrics in the Metrics window, and reports a static analysis violation for any class metric that is outside of the bounds that have been set for that metric.
- Custom coding standards: Custom coding standards are rules specially tailored to your own or your group's unique coding style. You can create such customized rules using Jtest's RuleWizard feature.
Static analysis is performed automatically when you test a class or project.
When enforcing all static analysis rules except for global static analysis rules, Jtest statically analyzes the classes by parsing the .java source and applying a set of rules to it, then alerting you to any rule violations found.
When enforcing global static analysis rules, Jtest scans all of the project's .class files to collect global usage information, uses this information to check if each class violates any rules, then alerts you to any violations in the classes under test. Because Jtest uses .class files (rather than .java files) to enforce global static analysis rules, you can perform global static analysis even when .java files are not available.
Note: Global static analysis can only be performed while testing a project.
This lesson contains the following sections:
Performing Static Analysis: Overview
To perform static analysis, just Jtest what class or set of classes to test then click the Start button.
If you test a project, results will be displayed in the Class Name> Errors Found> Static Analysis branch of the Project Testing UI's Results panel.
If you test a single class, results will be displayed in the Static Analysis branch of the Class Testing UI's Errors Found panel.
Important: Because Jtest's static analysis tests at the source level, Jtest will only perform static analysis on classes whose .java files are available.
Performing Static Analysis: Example
- Go to Jtest's Class Testing UI. (This UI opens by default when you launch Jtest).
- If a class is already loaded into the Class Testing UI (i.e., if you see a class name in the Class Name field), click the New button to clear the previous test.
- Browse to Simple.class (in <jtest_install_dir>/examples/eval) using the Browse button in the Class Name panel.
- Click the Start button in the tool bar.
Jtest will perform static and dynamic analysis on the class.
Information on test progress will be displayed in the Test Progress panel. Errors found will be reported in the Errors Found panel.
You will see two types of errors reported in the Errors Found panel.
For this example, we're going to focus on the static analysis violations.
The Static Analysis branch of the Errors Found panel should list the following violations:
To see more information about a violation, expand the violation's branch. For example, expand the violation of the PB.TLS rule.
This violation message reveals that the developer inadvertently wrote "case10" instead of "case 10". If the class is not fixed, it will give incorrect results when it is passed the value 10.
To learn more about any rule that Jtest enforces, right-click the topmost branch of the error violation that you would like explained, then choose View Rule Description from the shortcut menu.
A dialog box that contains a description and example of the selected rule violation, as well as a suggested repair, will open.
You can also view the source code responsible for the violation; to do this, simply double-click the node containing the violation's file/line information, then view the code in the source code viewer that opens.
To view this class's metrics, click the Metrics button. A metrics window containing this class's metrics will then open.
To view the description of a certain metric, right-click that metric and choose View Rule Description from the shortcut menu.
Customizing Static Analysis
You can change which rules Jtest enforces by enabling or disabling specific rules, or entire rule categories.
Note: You can find a description of each rule, as well as an example violation and suggested repair, in the Reference section of the Jtest User's Guide.
Enabling/Disabling Specific Rules
One way to control what rules are enforced is to enable/disable individual rules. If you do this, be aware that Jtest looks for violations of a rule only if the rule is enabled and its severity level is enabled.
- Determine which rules are currently enabled/disabled.
- Open the Global Test Parameters window by clicking the Global button.
- In the Global Test Parameters window, open Static Analysis> Rules.
- Open the Built-in Rules node or User Defined Rules node.
- Open the node for the category of rules you want to view.
- Enable/disable rules by selecting or clearing the button next to the rule name.
For example, to prevent Jtest from enforcing the NAMING.IFV rule (Use upper-cases for interface member fields), open the Global Test Parameter's Static Analysis> Rules> Built-in Rules> Naming Conventions node, then clear the Use uppercase letters for "interface" fields option.
Enabling/Disabling Rule Categories
Each rule has two categories:
- The descriptive category that describes the type of rule (for example, Servlets, Design by Contract, Initialization, User Defined Rules, etc.)
- The severity category that describes the severity of a violation of the rule (violations of coding standards that are most likely to cause an error are level 1; violations of coding standards that are least likely to cause an error are level 5). By default, Jtest reports violations of all coding standards with a severity level of 1, 2, or 3. A rule's severity is indicated by the final character in each rule code, as indicated in the Global Test Parameters window's Static Analysis> Rules> Built-in Rules node. For example, PB.SBC has a severity level of 1, and OOP.IIN has a severity level of 5.
To enable/disable all rules in a certain descriptive category:
- Open the Global Test Parameters window by clicking Global.
- Right-click the Static Analysis> Rules> <name_of_rule_category> node. A shortcut menu will open.
- Choose Enable All or Disable All from the shortcut menu.
For example, if you want did not want to enforce Enterprise JavaBean rules for your current project, you would open the Global Test Parameter's Static Analysis> Rules> Built in-Rules branch, right-click the Enterprise JavaBeans node, then choose Disable All from the shortcut menu. Or, if you wanted to enable the Design by Contract rules, you would open the Global Test Parameter's Static Analysis> Rules> Built in-Rules branch, right-click the Design by Contract node, then choose Enable All from the shortcut menu.
To enable/disable rule all rules in a certain severity categories:
- Open the Global Test Parameters window by clicking Global.
- Open the Severity Levels Enabled node (beneath Static Analysis> Rules).
- Enable/disable categories by right-clicking a category whose status you want to change and choosing Enable or Disable from the shortcut menu.
For example, if you want Jtest to enforce rules from severity levels 1-4 (instead of 1-3), open the Global Test Parameter's Static Analysis> Rules> Severity Levels Enabled branch, then enable the Level 4 option.
You can also enable/disable severity categories at the class and project level. You might want to do this, for example, if you generally want to enforce only the most severe rules, but you want to enforce all rules for a specific class. To enable/disable rules at the class or project level, set the Severity flags in Class Test Parameters - Static Analysis (if you are testing a single class) or Project Test Parameters - Static Analysis (if you are testing a set of classes).