Example: ActiveX client application calling Java methods
In the ActiveX to Enterprise Java Beans (EJB) bridge, methods are called using the native language method invocation syntax.
The following differences between Java invocation and ActiveX Automation invocation exist:
- Unlike Java methods, ActiveX does not support method (and constructor) polymorphism; that is, we cannot have two methods in the same class with the same name.
- Java methods are case-sensitive, but ActiveX Automation is not case-sensitive.
To compensate for Java polymorphic behavior, give the exact parameter types to the method call. The parameter types 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 fails if the CLng() method was not present or the toHexString syntax 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 the method calls unchanged. For example, in Visual Basic to call a method close() (lowercase), the Visual Basic code capitalizes it "Close()". In Visual Basic, the only way to effectively work 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 we expect a non-primitive data type to return, use set. (If we expect a primitive data type to return, we do not need to use set.) See the following example for more explanation:
Set oMyObject = o.getObject iMyInt = o.getInt
In some cases, you might not know the type of object returning 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, we might need to use the language 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 methods, the ActiveX to EJB bridge uses strings (byte or VT_I1 do not work because characters have multiple bytes in Java code). If we 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. For more information about how this argument container 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 programming, the ActiveX to Java bridge uses direct argument type mapping to find a method. This method 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 acquire this ability, use the XJB.JMethodArgs object to coerce your parameters to match the parameters on the method. We can get a JMethodArgs instance using the JClassFactory.GetArgsContainer() method.
The JMethodArgs object is a container for method parameters or arguments. This container enables you to add parameters to it one-by-one and then we can send the JMethodArgs object to the method call. The JClassProxy and JObjectProxy objects recognize the JMethodArgs object and attempt to find the correct method and let the Java language coerce the 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 looks like the following example code:
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" ' We 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
ActiveX to EJB Bridge
Example: Use an ActiveX client application to access a Java class or object