OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol...

45
OO Cobol in z/OS

Transcript of OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol...

Page 1: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

OO Cobol in z/OS

Page 2: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Enterprise Cobol Features

• Allows you to define classes with methods and in Cobol

• Create instances of Java and Cobol classes

• Invoke methods on Java and Cobol objects

• Write classes that inherit from Java or Cobol classes

• Overload methods

Page 3: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Java Interoperability

• OO Cobol programs must be compiled with the THREAD option in order to work with Java programs

• Cobol object instances are freed by the Java runtime system GC

Page 4: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

JNI Support

• Basic Java features are accessed using Cobol syntax

• In Enterprise Cobol you can also invoke the Java Native Interface (JNI) to obtain other Java-oriented capabilities

• For example, converting PIC X type data to Java String objects

Page 5: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Wrapper Classes

• Legacy procedural Cobol code can be wrapped as a Cobol class to allow existing code to be called from Java

• Java code can create instances of Cobol classes and invoke Cobol class methods

• Java code can extend Cobol classes

Page 6: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Restrictions

• Cobol class definitions can’t contain EXEC SQL statements

• Cobol class definitions can’t contain EXEC CICS statements

Page 7: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

A Cobol Driver to Invoke Java cbl dll,thread Identification division. Program-id. "tstDoor" recursive. Environment division. Configuration section. Repository. class Door is "Door". Data Division.

Procedure Division. DISPLAY "COBOL PROGRAM tstDoor ENTERED" INVOKE Door "doSomething" DISPLAY "RETURNED FROM JAVA Door TO tstDoor“ GOBACK. END PROGRAM "tstDoor".

Page 8: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Defining a Cobol ClassIdentification division

Environment division (configuration section only)

Factory definition– Identification division– Data division– Procedure division (containing one or more method

definitions)

Object definition– Identification division– Data division– Procedure division (containing one or more method

definitions)

Page 9: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Creating a Java Class in UNIX

1) Use TSO OMVS to move to UNIX2) Build a directory for the class (cd, mkdir)3) Save an env file in the Directory (see

env panel that follows)4) Use TSO OEDIT to create a .java file in

the directory5) Compile the .java file with javac. Make

sure you allocate enough space when you sign on to TSO

Page 10: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Door.javapublic class Door { private String state; public Door() { System.out.println("building a closed door..."); state = "closed"; } public Door(String s) { System.out.println("building the door..."); if (s.equals("open")) { state = "open"; } else { state = "closed"; } }

Page 11: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Door.java

public void open() { System.out.println("About to open the door..."); if (state.equals("open")) { System.out.println("The door is already open."); } else { state = "open"; } } public void close() { System.out.println("About to close the door..."); if (state.equals("closed")) {

Page 12: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Door.java System.out.println("The door is already closed."); } else { state = "closed"; } } public String toString() { return "The door is " + state + "."; } public static void doSomething() { System.out.println("I did something."); } public void print() { System.out.println("The door is " + state + "."); }

}

Page 13: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

An env File

PATH=/bin:/u/Java5_31/J5.0/bin.

LIBPATH=/lib:/usr/lib:/u/Java5_31/J5.0/bin:/u/Java5_31/J5.0/bin/j9vm

CLASSPATH=/u/csup004/Door

The CLASSPATH should point to the directories that contains your .class files

Page 14: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Compile, Link, Run JCL//CSUP004O JOB 'WOOLBRIGHT',CLASS=A,MSGCLASS=A,TIME=(0,5), // LINES=5,MSGLEVEL=(1,1),NOTIFY=&SYSUID,REGION=0M // SET COBPRFX='IGY410' // SET LIBPRFX='CEE' //* //COMPILE EXEC PGM=IGYCRCTL, // PARM='SIZE(5000K),LIB' //SYSLIN DD DSNAME=&&OBJECT(TSTDOOR),UNIT=SYSDA,DISP=(NEW,PASS), // SPACE=(CYL,(1,1,1)) //SYSPRINT DD SYSOUT=* //STEPLIB DD DSN=&COBPRFX..SIGYCOMP,DISP=SHR // DD DSN=&LIBPRFX..SCEERUN,DISP=SHR //SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT2 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT3 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT4 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT5 DD UNIT=SYSDA,SPACE=(CYL,(1,1))

Page 15: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Compile, Link, Run JCL//SYSUT6 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSUT7 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) //SYSIN DD * cbl dll,thread Identification division. Program-id. "tstDoor" recursive. Environment division. Configuration section. Repository. class Door is "Door". Data Division.

Procedure Division. DISPLAY "COBOL PROGRAM tstDoor ENTERED" INVOKE Door "doSomething" DISPLAY "RETURNED FROM JAVA Door TO tstDoor"

Page 16: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Compile, Link, Run JCL DISPLAY "RETURNED FROM JAVA Door TO tstDoor" GOBACK. END PROGRAM "tstDoor". /* //LKED EXEC PGM=IEWL, // PARM='RENT,LIST,LET,DYNAM(DLL),CASE(MIXED)' //SYSLIB DD DSN=&LIBPRFX..SCEELKED,DISP=SHR // DD DSN=&LIBPRFX..SCEELKEX,DISP=SHR //SYSTERM DD SYSOUT=* //SYSPRINT DD SYSOUT=* //SYSLMOD DD DSN=&&GOSET(TSTDOOR),DISP=(MOD,PASS), // UNIT=SYSALLDA,SPACE=(CYL,(1,1,1)),DSNTYPE=LIBRARY

//SYSDEFSD DD DUMMY //OBJMOD DD DSN=&&OBJECT,DISP=(OLD,DELETE) //SYSLIN DD * INCLUDE OBJMOD(TSTDOOR) INCLUDE '/u/Java5_31/J5.0/bin/j9vm/libjvm.x' INCLUDE '/usr/lpp/cobol/lib/igzcjava.x' /*

Page 17: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Compile, Link, Run JCL//GO EXEC PGM=TSTDOOR, // PARM='/ENVAR("_CEE_ENVFILE=/u/csup004/env")XPLINK(ON) // POSIX(ON)' //* POSIX(ON)' //*STEPLIB DD DSN=CSUP004.LOAD,DISP=SHR //STEPLIB DD DSN=*.LKED.SYSLMOD,DISP=SHR // DD DSN=&LIBPRFX..SCEERUN2,DISP=SHR // DD DSN=&LIBPRFX..SCEERUN,DISP=SHR //SYSOUT DD SYSOUT=* //CEEDUMP DD SYSOUT=* //SYSUDUMP DD DUMMY //JAVAOUT DD PATH='/u/csup004/javaout', // PATHOPTS=(OWRONLY,OCREAT,OTRUNC), // PATHMODE=(SIRUSR,SIWUSR,SIRGRP) //

Page 18: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Sample Compile, Link, Run JCL

• CSU.PUBLIC.OO(OOMAIN1) OR

CSU.PUBLIC.OO(CJSTATIC)

Page 19: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

INVOKE

Page 20: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

INVOKE NEW

• Use INVOKE class NEW to instantiate an object

• The returned reference must be saved in an object reference variable

01 myObj object reference DOOR.

INVOKE Door NEW

RETURNING myObj

Page 21: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

INVOKE NEW

• The constructor call based on the pattern of parameters that are passed.

INVOKE Door NEW using by value jstring1 returning myDoor

01 jstring1 object reference jstring.

Page 22: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

INVOKE A STATIC METHOD

INVOKE Door "doSomething "• Door is the class.• “doSomething” is the static method

in Door

Page 23: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

INVOKING INSTANCE METHODS

01 myObj object reference Door. ================================INVOKE Door NEW RETURNING myObj INVOKE myObj "open"

INVOKE myObj "print" • Class Door contains instance methods

“open” and “print” which have no parms

Page 24: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Interoperable Types

Page 25: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Interoperable Arrays and Strings

Page 26: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Passing Parms, Returning Values

working-storage Section. 01 myObj object reference Door. 01 intval10 pic s9(9) BINARY value 10. 01 intval20 pic s9(9) BINARY value 20. 01 intval30 pic s9(9) BINARY value 0. Procedure Division. DISPLAY "COBOL PROGRAM tstCalc ENTERED" INVOKE Calculator NEW using by value intval10 returning myObj INVOKE myObj "add" using by value intval20 returning intval30

Page 27: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

USING Parameters in INVOKE

• Pass parameters BY VALUE when using INVOKE

• The BY VALUE phrase specifies that the value of the argument is passed, not a reference to the sending data item. The invoked method can modify the formal parameter that corresponds to an argument passed by value, but changes do not affect the argument because the invoked method has access only to a temporary copy of the sending data item.

Page 28: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Returning Constraints

If the RETURNING item is not an object reference, the following rules apply:

– If the target method is implemented in COBOL, the returning item in the INVOKE statement and the RETURNING item in the target method must have an identical data description entry.

– If the target method is implemented in Java, the returning item in the INVOKE statement must correspond to the Java type of the method result, as described in “Interoperable Types”

Page 29: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Returning Constraints

If the RETURNING item is an object reference, the RETURNING item specified in the INVOKE statement must be an object reference typed exactly to the class of the returning item specified in the target method. That is, the class specified in the returning clause must not be a subclass or a superclass of the class of the returning item in the target method.

Page 30: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

OO Exercise #1

• In UNIX, build a Java class called Point that stores a pair of integers (x and y coordinates). Put in the normal things that Java Classes should have (Constructor, accessors, mutators, toString)

• Build a second Java class called PointStuff that has a static method called distance which is passed two Point objects. The method computes the distance between the two points and returns the distance

√((x1-x2)2 –(y1-y2)2. • Write a Cobol program that creates two Point objects

and passes them to distance. Print out the result the method returns.

Page 31: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Building Cobol Classes

IDENTIFICATION DIVISION.

CLASS-ID. Account inherits Base.

The class ID names the class you are defining. Base represents java.lang.Object and is referenced in the Repository.

Page 32: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Repository Paragraph

Environment Division.Configuration Section.Repository. Class Base is “java.lang.Object” Class Account is “Account”.• The Repository is used to relate the internal

class names to external class names.• The Repository has rules for generating external

names if they are omitted.• Extenal names can include fully qualified

package names.

Page 33: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Object Paragraph

Identification Division

Object.

… data and methods

End Object.• The Object paragraph is used to define the

instance data and methods of the class.• The Object paragraph is immediately preceded

by an Identification Division header. (Different from the one that starts the class.)

Page 34: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Working Storage in Object

Identification Division

Object.

Data Division.

Working Storage Section.

01 balance pic s9(6) value 0.

End Object.

Page 35: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Working Storage in Object

• Working Storage fields become instance data for Cobol objects.

• Instance fields are initialized with value clauses in Working Storage

• Fields are allocated when the object is instantiated

• Fields exist until the object is garbage collected by the Java run time

Page 36: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Working Storage

• To initialize more complicated objects, you need to write special initialization methods in a Factory

• Essentially, there is one constructor for Cobol objects

• Fields are equivalent to Java private nonstatic member data

• No other class or subclass can reference Cobol instance data directly

• Provide sets and gets to make fields accessible

Page 37: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Methods in Object

• Instance methods are defined in the Procedure Division of the Object paragraph

• Each method has four divisions followed by END METHOD marker

Page 38: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Method Divisions

Identification Division.

Method-id. “init”

Environment Division

Input-Output Section.

File-Control.

Select...myFile assign to MYFILE

… used to relate filenames used in method to external names (optional)

Page 39: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Method Divisions

Data Division.File Section. …used to define a file record layout

Local-Storage Section.…used to define local variables which are allocated upon entry and freed upon return. Put a value clause to initialize a variable upon entry

Page 40: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Method Divisions

Data Division.

Working-Storage Section.

… One copy of this storage is allocated at runtime. Data persists in its last used state. One copy exists for all threads.

Page 41: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Method Divisions

Data Division.

Linkage Section.

… Used to describe parameters and fields that are returned from the method.

Page 42: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Instance Method Divisions

Procedure Division. … used to define the executable code for the methodYou may not code:ENTRY, EXIT, ALTER, GOTO, SEGMENT-LIMIT,USE FOR DEBUGGINGSORT or MERGEYou may code:EXIT METHODGOBACK

Page 43: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Passing Parameters

• Specifiy parms in the division header.• Parms must be passed by value.• Describe each parm in Linkage as 01 or 77 level

items• Data types must be interoperable with JavaLinkage Section.01 X pic s9(9) binary.01 Y pic s9(9) binary.Procedure Division using by value X, Y

Page 44: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

Returning a Parm

• A method can return a value. This is evident when the method is invoked:

INVOKE Calculator “add”

using by value X Y

returning Total

Variables that are returned must be described as 01 or 77 items in the Linkage

Section

Page 45: OO Cobol in z/OS. Enterprise Cobol Features Allows you to define classes with methods and in Cobol Create instances of Java and Cobol classes Invoke methods.

OO Exercise #2

• Build a Calculator class that has a single private variable “regValue” that will hold an integer (Pic S9(9) Binary).

• Build an “setRegValue” method that changes the stored value

• Build a “getRegValue” method that returns the stored value

• Build an “add” method that passes an integer, adds it to the stored value and returns the sum

• Build a “print” method that prints the object