Coding analysis methods with the COBOL APIs

Use the COBOL APIs to analyze the COBOL language elements that you select for your user-written rules.

About this task

For more information about the COBOL APIs and the generated implementation code, see the related links at the end of the topic.

Procedure

To implement the code analysis for a custom rule:

  1. Open the Plug-in Development perspective.
  2. In the Package Explorer, right-click the Java™ source file that was generated for your custom rule, then click Open With > Java Editor. The location of the Java file in the Package Explorer is project_name > src > package_name > class_name.java. For example: MyPluginProject > src > com.example > CobolRule.java.
  3. In the Java editor, add Java code to each of the generated visit() methods to do the code analysis. A visit() method is generated for each nonabstract subinterface of the Cobol Application Model interface corresponding to the COBOL language element that you selected. For example, suppose that you selected the ACCEPT element in the Program tree on the third page of the creation wizard:
    • The CAM interface corresponding to ACCEPT is the AcceptStmt interface.
    • The Javadoc information for the AcceptStmt interface shows, under the heading All Known Subinterfaces, that AcceptStmt has two subinterfaces: AcceptDataTranferStmt and AcceptSystemInfoTransferStmt.
    • Two visit() methods are generated, one for each of the two subinterfaces.
    Note:
    • The visit() methods belong to the interface ICOBOLVisitor, in the package com.ibm.rsar.analysis.codereview.cobol.custom.model.util, in the Custom Rules API for COBOL Code Review. Do not implement ICOBOLVistor directly; instead, subclass AbstractCOBOLVisitor. The following examples show the generated visit() methods:
      • boolean visit(com.ibm.etools.cobol.application.model.cobol.AcceptDataTransferStmt n)
      • boolean visit(com.ibm.etools.cobol.application.model.cobol.AcceptSystemInfoTransferStmt n)
    • The node that is passed as input to a visit() method is a subinterface of the interface ASTNode in the package com.ibm.etools.cobol.application.model.cobol in the COBOL Application Model (CAM) API.
    When a COBOL source file is analyzed, the code analyzer starts a visit() method whenever it encounters an instance of the corresponding ASTNode.
  4. In each visit() method, add Java code for the following functions:
    • Use the ASTNode n that is passed as input to the visit() method to get information about the instance of the COBOL language element that you are analyzing. For example, to get the Program ID attribute of the Identification division, where n is an IdentificationDivision object, you can write the following code:
      String myProgramID = n.getProgramId();
    • Perform the code analysis for your custom rule. If you have more than one visit() method for your rule, you probably also must write Java code to coordinate the results from the individual visit() methods.
    • Set the return code and the return information. Return true to continue visiting child nodes of the current node or false to skip visiting child nodes. If the COBOL language element that is being analyzed violates the rule, add the corresponding node to the tokens list.
  5. When you are finished, close the Java editor.

Feedback