Invoking a method in a Java™ object
There are two ways to invoke a method in a Java™ object.
- Use the dot notation in JavaObject.
Dim mySession As JavaSession Dim myClass As JavaClass Dim myObject As JavaObject Set mySession = new JavaSession Set myClass = mySession.getClass("myjavaapp") Set myObject = myClass.CreateObject() Call myObject.myMethod(arg1, arg2)
- Use the JavaMethod ADT.
Dim mySession As JavaSession Dim myClass As JavaClass Dim myObject As JavaObject Dim myMethod as JavaMethod Set mySession = new JavaSession Set myClass = mySession.getClass("myjavaapp") Set myMethod = myClass.getMethod("MyMethod", "()V") Set myObject = myClass.CreateObject() myMethod.Invoke(myObject);
The dot notation method is easier and more intuitive, but certain restrictions apply. The JavaMethod ADT method is significantly harder to use but is more appropriate for general use. Dot notation is ambiguous if any of the the conditions listed as follows occur. If they do, you can use the more general mechanism to resolve the ambiguity.
- Case sensitivity
LotusScript® is case insensitive while Java™ is case sensitive. While theoretically you can have two Java™ methods differ only in case -- for example, MyMethod and mymethod -- they are distinctly different methods. There is no way for LotusScript® to identify the correct method to invoke using the dot notation. The result is JVM-dependent if you try to access the function; that is, the results may differ depending on what operating system you are using.
- Long method names
LotusScript® has an internal limitation of 40 characters for names. If you use the dot notation method, you won't be able to get to methods with names longer than 40 characters.
- Method overloading
LotusScript® currently does not support method overloading. Because Java™ does, it is fairly common for a Java™ class to contain methods of the same name but with different signatures. If you use the dot notation, LotusScript® uses trial-and-error to try and match the method. It is somewhat JVM-dependent, because the method that is matched depends on the order which the methods are presented to LotusScript® by the JVM through JNI. The following algorithm is used to match the method:
- Enumerate all methods with the specified name.
- Retrieve the signature and check for the number of arguments. If they don't match, move on.
- If the number of arguments matches, try to convert arguments in LotusScript® to arguments in Java™. Move to the next method if the number of arguments don't match.
LS2J calls through to the Java™ method of the first successful match.
Most Java™ implementations enumerate methods in the order they were declared in the Java™ source file. However, that is not always the case: for example, the AIX® JVM seems to enumerate the method in reverse order.