![]() |
In the ActiveX to EJB bridge, methods are called using the native 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 via ActiveX Automation. If you use the wrong case on a method call or use the wrong parameter type, you get an Automation Error 438 "Object doesn't support this property or method" being thrown.
To be able 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 EJB bridge, converting data types.
For example, the following Visual Basic code would fail 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
wanted to call a method close()
(uncapitalized), Visual Basic
would try to capitalize it "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, you must take care to use the set keyword in Visual Basic. If you expect a non-primitive data type to be returned, you must 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 you will get back 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 natively support character types supported by Java, the ActiveX to EJB bridge uses Strings (byte or VT_I1 do not work, because Characters have multiple bytes in Java). If you try to call a method that takes a char or java.lang.Character type you must use the JMethodArgs argument container to pass character values to methods or constructors. For more information about how this is used, see Methods with "Object" Type as Argument and Abstract Arguments.
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, you must 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 will not work. The ActiveX to EJB 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
Related concepts... | |
How ActiveX programs use the ActiveX to EJB bridge | |
ActiveX to EJB bridge, accessing Java fields | |
ActiveX to EJB bridge, good programming guidelines | |