Example of Using the ILSMARTLINKAGE Compiler Directive

The following example illustrates how you can expose a procedural program to managed applications by compiling it to JVM COBOL code with the ILSMARTLINKAGE Compiler directive. This helps expose the Linkage Section items and entry points in the procedural code to other managed languages by generating a class for each group item with the lower level data items being the members of the class.

In this example, you are going to use a slightly modified version of the calculator program which was used in the example of using wrapper classes.

Note: The names of the projects you are going to create for this demonstration are the same as the ones used for the example of using a wrapper classes. If you have already created the projects for that example, you might need to switch to a different workspace Eclipse does not support projects with the same name in a single workspace.

Create a JVM COBOL project

First, you are going to create a project to hold the procedural program:

  1. In Eclipse, create a COBOL JVM project - click File > New > COBOL JVM Project.
  2. Specify a name such as calclib, and click Finish.

    This creates a calclib subfolder for your project in the specified location. Next, you need to create a program to hold your procedural code.

  3. Right-click the project, and click New > COBOL Program.
  4. In the New COBOL Program dialog, specify a name such as Calc.cbl, and click Finish.

    This creates the program in the src subfolder of the project, in the default package, and opens the program in the editor.

  5. Replace the code in the program with the following and save the file:
           program-id. Calc.
    
           data division.
           working-storage section.
           
           linkage section.
           01 operands. 
             03 op-1 pic 9(4) comp-5.
             03 op-2 pic 9(4) comp-5.
           
           01 the-result pic 9(8) comp-5.
           
           01 func-code pic x.
           78 add-op value '+'.
           78 sub-op value '-'.
           78 mult-op value 'x'.
           78 div-op value '/'.
          
           procedure division using by reference func-code operands the-result.
    
               evaluate func-code
                  when add-op
                     add op-1 to op-2 giving the-result
                  when sub-op
                      subtract op-2 from op-1 giving the-result
                  when mult-op
                       multiply op-1 by op-2 giving the-result
                  when div-op 
                        divide op-1 by op-2 giving the-result
               end-evaluate.
               
               goback.
               
           end program Calc.
    

    When the automatic build completes, there should be no errors.

    This variant of the example uses hyphenated variables, op-1 and op-2, to illustrate how ILSMARTLINKAGE deals with these.

  6. In the IDE, navigate to the project properties, expand Micro Focus, and click Build Configurations.
  7. In the Additional directives value cell, click , and in the Additional directives dialog box, type ILSMARTLINKAGE, then click OK.
  8. Click Apply and Close.

    Compiling with ILSMARTLINKAGE exposes the group item in the program as a new class, and the group items as properties of that class. Other managed languages do not recognize hyphenated data items but ILSMARTLINKAGE caters for that as well - it removes the hyphens from the names of the data items and changes the case to camel case. As a result, other managed languages will see operands exposed as the class Operands, and the variables op-1 and op-2 exposed as the properties Op1 and Op2 of that class. Similarly, func-code is exposed as FuncCode.

Write a front-end application

Next, you are going to create an application in JVM COBOL that will access the group item entries of the procedural code in the calclib project which are now exposed as a class and its properties:

  1. Create a new COBOL JVM project - click File > New > COBOL JVM Project.
  2. Specify a name such as OOCalc, and then click Finish.
  3. Create a new JVM COBOL class Class1 in the project - right-click the project and click New > COBOL JVM Class.
  4. Ensure that you specify OOCalc in the Package field, and then click Finish.

    This creates Class1.cbl in your project and opens the file in the editor.

  5. Replace the code in the class with the following code that will access the group items in the procedural code:
           class-id OOCalc.Class1.
    
           working-storage section.
           78 add-op value '+'.
           78 sub-op value '-'.
           78 mult-op value 'x'.
           78 div-op value '/'. 
           
           method-id. main static.
           procedure division.
              declare calculation as type Class1
              set calculation to new Class1
              invoke calculation::Add()
           end method.
              
           
           method-id Add public.
           
           local-storage section.
           01 operand type Operands.
           01 CalcOO type Calc.
           01 func-code type FuncCode.
           01 result pic x(4) comp-5.
           
           procedure division.
           
          
           set func-code to new FuncCode()
           set operand to new Operands()
    
           set operand::Op1 to 1.
           set operand::Op2 to 2.
           
           set CalcOO to new Calc()
           set func-code::FuncCode to add-op
       
           
           invoke CalcOO::Calc(func-code, operand, result)
           display "The result is " result
     
           
           
               goback.
           end method.
    
           end class.

    The automatic build fails to compile the code as it does not recognize the types Calc, FuncCode and Operands. To resolve this, you need to add the calclib project to the JVM Build Path for OOCalc.

  6. Right-click OOCalc, and click Properties.
  7. Expand Micro Focus, click JVM Build Path.
  8. Click the Projects tab, and then click Add.
  9. Enable the checkbox in front of calclib, and then click OK.

    As calclib is now on the JVM build path for OOCalc, the code in OOCalc now finds the classes defined in calclib and the subsequent build is successful.

    In Class1, you define the static method main which is the main entry point for your program. main defines the object calculation of type Class1, and then executes the Add method of Class1.

    The code defines the method Add that uses as variables the following objects - operand of type Operands (the group item from the procedural program now exposed as a class); CalcOO of type Calc (the procedural program now seen as a class); func-code of type FuncCode (which is how func-code in the procedural program is exposed).

    You then create instances of func-code, operand, and CalcOO, give values to op-1 and op-2 in the procedural program, specify that you would like to invoke the add-op operation, and invoke the procedural code using func-code, operand and result as parameters to perform the arithmetic calculation - invoke CalcOO::Calc(func-code, operand, result).