RPG only supports calling Java™ methods; it does not support accessing Java fields. Normally, fields can be accessed through "get" and "set" methods, but it is also possible to access fields using JNI calls. Here is an example showing JNI calls necessary to access the fields of a Java class or object.
*------------------------------------------------------------------
* This example shows how to use JNI to access the fields of a
* class or an object.
*
* This program creates a Rectangle object and accesses the
* width and height variables directly, using JNI calls.
*
* In this particular case, the getWidth(), getHeight,
* setWidth() and setHeight() methods could have been used
* to access these fields, avoiding the use of JNI calls.
*------------------------------------------------------------------
H THREAD(*SERIALIZE)
/DEFINE JNI_COPY_FIELD_FUNCTIONS
/COPY JNI
/COPY JAVAUTIL
*------------------------------------------------------------------
* JAVA classes and methods
*------------------------------------------------------------------
D Rectangle C 'java.awt.Rectangle'
D NewRectangle PR O EXTPROC(*JAVA
D : Rectangle
D : *CONSTRUCTOR)
D x 10I 0 VALUE
D y 10I 0 VALUE
D width 10I 0 VALUE
D height 10I 0 VALUE
*------------------------------------------------------------------
* Constants with ASCII representations of Java names
*------------------------------------------------------------------
* One way to determine these values is to use %UCS2 to convert
* a character value to UCS-2, and display the result in hex
* in the debugger.
*
* The ASCII value is in every second byte of the UCS-2 characters.
*
* For example, %UCS2('abc') = X'006100620063'
* -- -- --
* The ASCII representation of 'abc' is X'616263'
*------------------------------------------------------------------
D ASCII_I C x'49'
D ASCII_x C x'78'
D ASCII_y C x'79'
D ASCII_width C X'7769647468'
D ASCII_height C X'686569676874'
* Note that this is 'java/awt/Rectangle', not 'java.awt.Rectangle'
* because the JNI uses slash as a separator.
D ASCII_Rectangle...
D C X'6A6176612F6177742F52656-
D 374616E676C65'
*------------------------------------------------------------------
* Cancel handling
*------------------------------------------------------------------
D EnableCanHdlr PR EXTPROC('CEERTX')
D Handler * CONST PROCPTR
D CommArea * CONST OPTIONS(*OMIT)
D Feedback 12A OPTIONS(*OMIT)
D CanHdlr PR
D CommArea * CONST
*------------------------------------------------------------------
* Variables and procedures
*------------------------------------------------------------------
D rect s O CLASS(*JAVA : Rectangle)
D x S 10I 0
D y S 10I 0
D rectClass S LIKE(jclass)
D fieldId S LIKE(jfieldID)
D msg S 52A
D Cleanup PR
*------------------------------------------------------------------
* Enable the cancel handler to ensure cleanup is done
*------------------------------------------------------------------
C CALLP EnableCanHdlr (%PADDR(CanHdlr)
C : *OMIT : *OMIT)
*------------------------------------------------------------------
* Create a new rectangle with x,y co-ordinates (5, 15),
* width 100 and height 200.
*------------------------------------------------------------------
C EVAL rect = NewRectangle (5 : 15 : 100 : 200)
*------------------------------------------------------------------
* Prepare to call JNI functions to access the Rectangle's fields
*------------------------------------------------------------------
C EVAL JNIEnv_P = getJniEnv ()
C EVAL rectClass = FindClass (JNIEnv_P
C : ASCII_Rectangle)
*------------------------------------------------------------------
* Call JNI functions to retrieve the Rectangle's width and height
*------------------------------------------------------------------
C eval fieldId = GetFieldID (JNIEnv_P
C : rectClass
C : ASCII_width
C : ASCII_I)
C eval width = GetIntField (JNIEnv_P
C : rect
C : fieldId)
C eval fieldId = GetFieldID (JNIEnv_P
C : rectClass
C : ASCII_height
C : ASCII_I)
C eval height = GetIntField (JNIEnv_P
C : rect
C : fieldId)
C eval msg = 'The rectangle has dimensions ('
C + %trim(%editc(width : '1'))
C + ', '
C + %trim(%editc(height : '1'))
C + ')'
C msg dsply
*------------------------------------------------------------------
* Call the Cleanup procedure
*------------------------------------------------------------------
C callp Cleanup()
C eval *INLR = '1'
*------------------------------------------------------------------
* Cleanup. * - Free objects if necessary
*------------------------------------------------------------------
P Cleanup B
C if rect <> *NULL and
C JNIEnv_P <> *NULL
C callp DeleteLocalRef(JNIEnv_P : rect)
C endif
C eval rect = *NULL
C eval JNIEnv_P = *NULL
P Cleanup E
*------------------------------------------------------------------
* Cancel handler. Ensures that cleanup is done.
*------------------------------------------------------------------
P CanHdlr B
D CanHdlr PI
D CommArea * CONST
C callp Cleanup()
P CanHdlr E