The complete PL/I program is shown in Figure 30. 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 string field in the Java object that we are interested in. This Java field is further identified by providing the name of the field, myString, and the JNI field descriptor, Ljava/lang/String;, which identifies the field as a Java String field. The value of the Java string field is then retrieved using the GetObjectField JNI function. Before PL/I can use the Java string value, it must be unpacked into a form that PL/I can understand. The GetStringUTFChars JNI function is used to convert the Java string into a PL/I varyingz string which is then displayed by the PL/I program.
After displaying the retrieved Java string, the PL/I program prompts the user for a PL/I string to be used to update the string field in the calling Java object. The PL/I string value is converted to a Java string using the NewString JNI function. This new Java string is then used to update the string field in the calling Java object using the SetObjectField JNI function.
When the PL/I program ends control is returned to Java, where the newly updated Java string 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_passString_pliShowString:
Proc( JNIEnv , myJObject )
external( "_Java_jPassString_pliShowString" )
options( byvalue nodescriptor linkage(stdcall) );
%include jni;
Dcl myBool Type jBoolean;
Dcl myClazz Type jClass;
Dcl myFID Type jFieldID;
Dcl myJObject Type jObject;
Dcl myJString Type jString;
Dcl newJString Type jString;
Dcl myID Char(9) Varz static init( 'myString' );
Dcl mySig Char(18) Varz static
init( 'Ljava/lang/String;' );
Dcl pliStr Char(132) Varz Based(pliStrPtr);
Dcl pliReply Char(132) Varz;
Dcl pliStrPtr Pointer;
Dcl nullPtr Pointer;
Display(' ');
/* Get information about the calling Class */
myClazz = GetObjectClass(JNIEnv, myJObject);
/* Get Field ID for String field from Java */
myFID = GetFieldID(JNIEnv, myClazz, myID, mySig );
/* Get the Java String in the string field */
myJString = GetObjectField(JNIEnv, myJObject, myFID );
/* Convert the Java String to a PL/I string */
pliStrPtr = GetStringUTFChars(JNIEnv, myJString, myBool );
Display('From PLI: String retrieved from Java is: ' || pliStr );
Display('From PLI: Enter a string to be returned to Java:' )
reply(pliReply);
/* Convert the new PL/I string to a Java String */
newJString = NewString(JNIEnv, trim(pliReply), length(pliReply) );
/* Change the Java String field to the new string value */
nullPtr = SetObjectField(JNIEnv, myJObject, myFID, newJString);
End;
end;