Obtaining value from a object

[Migrated content. Thread originally posted on 17 May 2012]

Hello,

I am trying to obtain the values of an array from a Java program, using a Visual COBOL program, this way (this has been taken from the example 'Using the JArray Class"):

       01 arrayElement    pointer.


                   invoke wsTable "getElement" using
                                   by value CDims
                                   by reference Dims
                                   by reference arrayElement

The problem is that the values I am trying to access are String values in Java, and I do not know exactly how to do it. I am able to obtain the array dimensions but not capable to show the String values through the screen.
  • Verified Answer

    Hi Voyager,

    I think code along the lines of:-


          $set ooctrl( p-f)
           Program-id. ReadArray.
           class-control.
               arraydemo   is class "$Java$arraydemo"
               javasup     is class "javasup"
               JavaString  is class "$Java$java.lang.String"
               .
         
           thread-local-storage section.
           copy JavaTypes.
          *01  ws-data   jarray.
           01 aJavaObj             object reference.
         
           01 theTotal        pic 9(9).
           01 CDims           pic x(4) comp-5.
           01 Dims.
             03 Dims-entry    pic x(4) comp-5 occurs 256.
           01 Bounds.
             03 Bounds-entry  pic x(4) comp-5 occurs 256.
           01 ind0            pic x(4) comp-5.
           01 ind1            pic x(4) comp-5.
           01 ArrayElement    pic x(4) comp-5.
           01 StringElement   object reference.
           01 wsTable         object reference.
           01 ws-type         pic x(4) comp-5.
           01 ws-len          pic x(4) comp-5.
           01 ws-str          pic x(100).
           
           01 ws-success      PIC 9(9) COMP-5.
               88 instance-of          value 1.
         
           procedure division.
             invoke arraydemo "new" returning aJavaObj
             invoke aJavaObj "getIntArray" returning wsTable
         
          *> find out the number of elements in the array
               invoke wsTable "getDimensions" returning CDims
               display "The array has " CDims " dimension(s)"
         
          *> get the number of elements in each dimension
               display "dimensions are " with no advancing
               invoke wsTable "getBounds" using Bounds
               perform varying ind0 from 1 by 1 until ind0 > CDims
                    display Bounds-entry(ind0) with no advancing
                    if ind0 < CDims
                       display " by " with no advancing
                    end-if
               end-perform
               display " "
         
          *> display each element in the array
               perform varying ind0 from 0 by 1
                       until ind0 = Bounds-entry(1)
                   move ind0 to Dims-entry(1)
                   perform varying ind1 from 0 by 1
                             until ind1 = Bounds-entry(2)
                       move ind1 to Dims-entry(2)
                       invoke wsTable "getElement" using
                                       by value CDims
                                       by reference Dims
                                       by reference arrayElement
                       display "Element " ind0 ","
                             ind1 " is " arrayElement
          *> modify the contents of the array
                       add 50 to arrayElement
                       invoke wsTable "putElement" using
                                       by value CDims
                                       by reference Dims
                                       by reference arrayElement
                   end-perform
               end-perform
               invoke wsTable "finalize" returning wsTable

               invoke aJavaObj "getStringArray" returning wsTable
               invoke wsTable "getType" returning ws-type
               
          *> find out the number of elements in the array
               invoke wsTable "getDimensions" returning CDims
               display "The array has " CDims " dimension(s)"
         
          *> get the number of elements in each dimension
               display "dimensions are " with no advancing
               invoke wsTable "getBounds" using Bounds
               perform varying ind0 from 1 by 1 until ind0 > CDims
                    display Bounds-entry(ind0) with no advancing
                    if ind0 < CDims
                       display " by " with no advancing
                    end-if
               end-perform
               display " "
         
          *> display each element in the array
               perform varying ind0 from 0 by 1
                       until ind0 = Bounds-entry(1)
                   move ind0 to Dims-entry(1)
                       move 0 to Dims-entry(2)
                       invoke wsTable "getElement" using
                                       by value CDims
                                       by reference Dims
                                       by reference stringElement
                                       
          ***** We have an Object. We can check if it is a String.
           
                       invoke javasup "isinstanceof" using stringElement
                                                           JavaString
                           returning ws-success
                       if not instance-of then
                           display "ERROR NOT A STRING"
                           display "Need to Die Gracefully"
                           stop run
                       end-if
                       invoke stringElement "length" returning ws-len   
                       invoke stringElement "toString" returning ws-str
                       display "Length of String = " ws-len
                       display "   String = " ws-str(1:ws-len)
               end-perform
               invoke wsTable "finalize" returning wsTable

               display "End of test"       

               stop run.


    should work OK for you. This works against Java Class :-


    import com.microfocus.cobol.*;

    public class arraydemo extends RuntimeSystem
    {
        int myArray[][];
        String myStringArray[];
        public arraydemo()
        {
            myArray = new int[5][2];
            myStringArray = new String[5];
            int i,j;
            for (i = 0; i < 5; i )
            {
                myStringArray[i] = "This is string " i;
                for (j = 0; j < 2; j )
                    myArray[i][j] = i * 100 j;
            }
        }
        public int[][] getIntArray()
        {
            return myArray;
        }
        public String[] getStringArray()
        {
            return myStringArray;
        }
    }


    This is using our Native COBOL Support for Java via JNI. This type of integration is easier under COBOL JVM.

    Regards
    David.
  • Thanks David, for your help.

    Now I am meaning to modify every string and hand back the value to the Java module, but I am not sure how to do it. I see in other examples that they are using "putElement", but I suppose it has to be different when using Strings...
  • You can use putElement to fill the array with new Java string objects like the following:


    perform varying ind0 from 0 by 1                           
        until ind0 = Bounds-entry(1)                             
        move ind0 to Dims-entry(1)                               
        move 0 to Dims-entry(2)                                 
        add 1 to ws-count                                       
        string "my new string"                                   
           ws-count                                             
           x"00" delimited by size into ws-stringvalue           
        invoke javaString "new" using ws-stringvalue             
           returning ws-newstring  *> this is object reference
        invoke wsTable "putElement" using                       
                            by value CDims                       
                            by reference Dims                   
                            by reference ws-newstring           
    end-perform