Calling Java methods

In the ActiveX to enterprise bean bridge, methods are called by using the language's method invocation syntax. This topic provides some conceptual information about calling Java methods, based on the following important differences between Java invocation and ActiveX Automation invocation:

You should take care when invoking Java methods through ActiveX Automation. If you use the wrong case on a method call or use the wrong parameter type, the Automation Error 438 "Object doesn't support this property or method" is thrown.

To compensate for Java's polymorphic behavior, you need to give the exact parameter types to the method call. The parameter types are what determine the correct method to invoke. For a listing of correct types to use, see ActiveX to Java primitive data type conversion values.

For example, the following Visual Basic code fails if CLng() was not present or toHexString was incorrectly typed as ToHexString:

  ...
  Dim strHexValue as String
  strHexValue = clsMyString.toHexString(CLng(255))

Sometimes it is difficult to force some development environments to leave the case of your method calls unchanged. For example, in Visual Basic, if you want to call a method close() (uncapitalized), Visual Basic would try to capitalize it as Close(). In Visual Basic, the only way to effectively get around this behavior is to use the CallByName() method. For example:

  o.Close(123)                                'Incorrect...
  CallByName(o, "close", vbMethod, 123)       'Correct...

or in VBScript, use the Eval function:

  o.Close(123)                                 'Incorrect...
  Eval("o.Close(123)")                         'Correct...

The return value of a function is always converted dynamically to the correct type. However, take care to use the set keyword in Visual Basic. If you expect a non-primitive data type to be returned, use set. (If you expect a primitive data type to be returned, you do not need to use set.) For example:

  Set oMyObject = o.getObject
  iMyInt = o.getInt

In some cases, you may not know the type of object that is returned from a method call, because wrapper classes are converted automatically to primitives (for example, java.lang.Integer returns an ActiveX Automation Long). In such cases, you may need to use your language's built-in exception handling techniques to try to coerce the returned type (for example, On Error and Err.Number in Visual Basic).

Methods with Character Arguments

Because ActiveX automation does not support the same character types that are supported by Java, the ActiveX to enterprise bean bridge uses Strings (byte or VT_I1 does not work, because Characters are multiple bytes in Java). If you try to call a method that takes a char or java.lang.Character type use the JMethodArgs argument container to pass character values to methods or constructors.

Methods with "Object" Type as Argument and Abstract Arguments

Because of the polymorphic nature of Java, the ActiveX to Java bridge uses direct argument type mapping to find a method. This works well in most cases, but sometimes methods are declared with a Parent or Abstract Class as an argument type (for example, java.lang.Object). You need the ability to send an object of arbitrary type to a method. To accomplish this, use the XJB.JMethodArgs object to coerce your parameters to match the parameters on your method. You can get a JMethodArgs instance by using the JClassFactory.GetArgsContainer() method.

The JMethodArgs object is a container for method parameters or arguments. It enables you to add parameters to it one-by-one and then you can send the JMethodArgs object to your method call. The JClassProxy and JObjectProxy objects recognize the JMethodArgs object and attempt to find the correct method and let Java coerce your parameters appropriately.

For example, to add an element to a Hashtable object the method syntax is Object put(Object key, Object value). In Visual Basic, the method usage would look like this:

  Dim oMyHashtable as Object
  Set oMyHashtable = _
      oXJB.NewInstance(oXJB.FindClass("java.utility.Hashtable"))

  ' This line does work. The ActiveX to enterprise bean bridge cannot find a method
  ' called "put" that has a short and String as a parameter:
  oMyHashtable.put 100, "Dogs"
  oMyHashtable.put 200, "Cats"

  ' You must use a XJB.JMethodArgs object instead:
  Dim oMyHashtableArgs as Object
  Set oMyHashtableArgs = oXJB.GetArgsContainer
  oMyHashtableArgs.AddObject("java.lang.Object", 100)
  oMyHashtableArgs.AddObject("java.lang.Object", "Dogs")

  oMyHashtable.put oMyHashTableArgs
  ' Reuse the same JMethodArgs object by clearing it.
  oMyHashtableArgs.Clear
  oMyHashtableArgs.AddObject("java.lang.Object", 200)
  oMyHashtableArgs.AddObject("java.lang.Object", "Cats")

  oMyHashtable.put oMyHashTableArgs