In earlier Micro Focus products, COBOL and Java interoperability was achieved using a class and method Wizard to create Java classes that extend the class com.microfocus.cobol.RuntimeObject and its companion COBOL class.
You can now use these generated classes with JVM COBOL applications, enabling you to pass a Java instance to native object COBOL by creating an instance of the com.microfocus.cobol.RuntimeObject. The native COBOL receives these instances as native object references.
In the following example, the Java class addressbook extends com.microfocus.cobol.RuntimeObject and is used to create an instance:
working-storage section. 01 ab type addressbook. procedure division. set ab to type addressbook::new() call "useab_oon" using by value ab
The receiving native COBOL program:
$set ooctrl(+p) ooctrl(-f) dialect(iso2002) mf warning(1) identification division. program-id. useab_con as "useab_oon". repository. class cls-sb as "$java$java.lang.StringBuffer". working-storage section. 01 nameBuffer object reference. 01 nameBufferLength binary-long. 01 nameBufferX pic x(80). linkage section. 01 ab object reference. procedure division using by value ab. invoke cls-sb "new" using by value 80 size 4 returning nameBuffer invoke ab "getName" using nameBuffer invoke nameBuffer "length" returning nameBufferLength invoke nameBuffer "toString" returning nameBufferX display "Customer name : " nameBufferX(1:nameBufferLength) invoke nameBuffer "finalize" returning nameBuffer goback.
The native COBOL class:
$set ooctrl(+p) ooctrl(-f) dialect(iso2002) mf warning(1) identification division. class-id. addressBook as "addressbook" inherits from javabase. repository. class javabase as "javabase" factory. working-storage section. end factory. object. working-storage section. method-id. getName . linkage Section. 01 customerName pic x(80). procedure division using by reference customerName. move "Mr Smith" to customerName exit method. end method getName. end object. end class addressBook.
And finally, addressbook.java, which is automatically generated:
import com.microfocus.cobol.*; // OCWIZARD start imports // OCWIZARD end imports public class addressbook extends com.microfocus.cobol.RuntimeObject { /* static methods - the COBOL runtime will register these */ public native static boolean cobinvokestatic_boolean (String methodName, Object[] params) throws CobolException, Exception; public native static byte cobinvokestatic_byte (String methodName, Object[] params) throws CobolException, Exception; public native static char cobinvokestatic_char (String methodName, Object[] params) throws CobolException, Exception; public native static short cobinvokestatic_short (String methodName, Object[] params) throws CobolException, Exception; public native static int cobinvokestatic_int (String methodName, Object[] params) throws CobolException, Exception; public native static long cobinvokestatic_long (String methodName, Object[] params) throws CobolException, Exception; public native static float cobinvokestatic_float (String methodName, Object[] params) throws CobolException, Exception; public native static double cobinvokestatic_double (String methodName, Object[] params) throws CobolException, Exception; public native static void cobinvokestatic_void (String methodName, Object[] params) throws CobolException, Exception; public native static Object cobinvokestatic_Object (String methodName, Object[] params) throws CobolException, Exception; static private String javaClassName = "addressbook"; static { /* Load the class */ /* cobloadclass (libraryname, cobolname, fulljavaname); */ cobloadclass ("addressbook", "addressbook", javaClassName); } // OCWIZARD - start java methods public void getName (StringBuffer customerName) throws Exception, CobolException { // Parameters are passed to COBOL in an array Object[] params = {customerName}; cobinvoke_void ("getName", params); } // OCWIZARD - end java methods }