Java Objects as Return Parameters

Java method calls can return complex Java objects. The JavaGetObject function can be used to retrieve a handle on such objects. Java objects can also be used as input parameters for other Java method calls. The JavaSetObject function is used for this purpose.

Input parameters specified with the JavaSetObject function must exactly match the formal parameters of the Java method call. If the formal and actual data type do not match exactly, but their assignments are compatible, then a kind of cast operation must be performed. For example, the formal data type is java.lang.Object. The actual datatype is java.lang.String. Strings are assignment compatible to objects.

One time cast operation: The third optional parameter of the JavaSetObject function allows you to specify the data type that is to be used for the next Java method call.

transaction TMyJavaTrans
var
  hValue :number;
  hVector :number;
begin
  hValue := JavaLoadString("myValue");
  hVector := JavaLoadObject("java/util/Vector");
  JavaSetObject(hVector, hValue, "java/lang/Object");
  JavaCallMethod(hVector, "add");
end TMyJavaTrans;

A java.util.Vector object expects a java.lang.Object object as parameter for its add method. So, as shown above, the Java string hValue must be treated as a java.lang.Object object for the call of the add method. Therefore the third optional parameter of JavaSetObject is set to java/lang/Object.

Permanent cast operation: If a Java object that is referenced in BDL code needs to have a specific data type for several Java method calls, then the JavaCastObject function can be used to permanently change the data type of the object.

transaction TMyJavaTrans
var
  hValue :number;
  hVector1 :number;
  hVector2 :number;
begin                            
  hVector1 := JavaLoadObject("java/util/Vector");
  hVector2 := JavaLoadObject("java/util/Vector");
  hValue := JavaLoadString("myValue");
  JavaCastObject(hValue, "java/lang/Object");
  
  JavaSetObject(hVector1, hValue);
  JavaSetObject(hVector2, hValue);
  JavaCallMethod(hVector1, "add");
  JavaCallMethod(hVector2, "add");
end TMyJavaTrans;

The actual datatype of a referenced Java object in BDL is slightly different from what is commonly considered to be the actual datatype of an object in the Java world. If an object is retrieved from a collection class, then the actual data type of the referenced object equals the formal data type of the method used to access the object.

transaction TMyJavaTrans
var
  hObj :number;
  hVector :number;
begin                            
  hVector := JavaLoadObject("java/util/Vector");
    … /* some methods filling the Vector */
  JavaSetNumber(hVector, 0);
  JavaCallMethod(hVector, "elementAt");
  hObj := JavaGetObject(hVector);
end TMyJavaTrans;

So in the BDL context, the actual data type of the hObj reference is java/lang/Object, because the formal return value of the elementAt method is java/lang/Object. The real actual data type of the Java object in the Java virtual machine is unknown.

You can pass a null object to a Java method call. The following function call can be used to define a null parameter:

JavaSetObject(hTestObj, JAVA_NULL_OBJECT);