The complete PL/I program is shown in Figure 33. This sample PL/I program makes several calls through the JNI.
Upon entry, a reference to the calling Java Object, myObject, is passed into the PL/I procedure. The PL/I program will use this reference to get information from the calling object. The first piece of information is the Class of the calling object which is retrieved using the GetObjectClass JNI function. This Class value is then used by the GetFieldID JNI function to get the identity of the Java integer field in the Java object that we are interested in. This Java field is further identified by providing the name of the field, myInt, and the JNI field descriptor, I, which identifies the field as an integer field. The value of the Java integer field is then retrieved using the GetIntField JNI function which is then displayed by the PL/I program.
After displaying the retrieved Java integer, the PL/I program prompts the user for a PL/I integer to be used to update the integer field in the calling Java object. The PL/I integer value is then used to update the integer field in the calling Java object using the SetIntField JNI function.
When the PL/I program ends, control is returned to Java, where the newly updated Java integer is displayed by the Java program.
*Process Limits( Extname( 31 ) ) Margins( 1, 100 ) ;
*Process Dllinit xinfo(def);
*Process Default( IEEE );
plijava_demo: package exports(*);
Java_passNum_pliShowInt:
Proc( JNIEnv , myjobject )
external( "_Java_jPassInt_pliShowInt" )
options( byvalue nodescriptor linkage(stdcall) );
%include jni;
Dcl myClazz Type jClass;
Dcl myFID Type jFieldID;
Dcl myJInt Type jInt;
dcl rtnJInt Type jInt;
Dcl myJObject Type jObject;
Dcl pliReply Char(132) Varz;
Dcl nullPtr Pointer;
Display(' ');
/* Get information about the calling Class */
myClazz = GetObjectClass(JNIEnv, myJObject);
/* Get Field ID for int field from Java */
myFID = GetFieldID(JNIEnv, myClazz, "myInt", "I");
/* Get Integer value from Java */
myJInt = GetIntField(JNIEnv, myJObject, myFID);
display('From PLI: Integer retrieved from Java is: ' || trim(myJInt) );
display('From PLI: Enter an integer to be returned to Java:' )
reply(pliReply);
rtnJInt = pliReply;
/* Set Integer value in Java from PL/I */
nullPtr = SetIntField(JNIEnv, myJObject, myFID, rtnJInt);
End;
end;