LS2J extended example
This sample code demonstrates calling Java™ methods within LotusScript® using LS2J. Immediately following this example the code is modified slightly to demonstrate handling situations where there is a syntax discrepancy between Java™ and LotusScript®.
Mortgage Calculator
With this mortgage calculator you can get the value of a Java™ property and call several Java™ methods by using the names directly from LotusScript®.
Mortgage.java
public class Mortgage
{
// Java property
public static String F = "Mortgage Calculator";
// Java methods
public double CalculateInterest(float rate, short yr, _
double principal)
{
// This bank doesn't bother with compound interest!
return (rate / 100) * yr * principal;
}
public double CalculateTotal(double principal, _
double interest)
{
return principal + interest;
}
public double CalculateMonthlyPayment(float rate, _
short yr, double principal)
{
double interest = CalculateInterest(rate, yr, _
principal);
double total = CalculateTotal(principal, interest);
return total / (yr * 12);
}
}
Mortgage.lss
Uselsx "*javacon"
Dim mySession As JavaSession
Sub Initialize
Dim myClass As JavaClass
Dim myObject As JavaObject
Dim header As String
Dim rate As Single
Dim yr As Integer
Dim principal As Double, interest As Double
Dim total As Double, monthly_payment As Double
Set mySession = New JavaSession()
' Set LS values
rate = 8.5
yr = 30
principal = 200000
Set mySession = New JavaSession()
' Get Java "Mortgage" class
Set myClass = mySession.GetClass("Mortgage")
' Call Java "Mortgage" Constructor
Set myObject = myClass.CreateObject
' Get Java property (can just use the name)
header = myObject.F
' Call Java Methods (can just use the names)
interest = myObject.CalculateInterest(rate, yr, _
principal)
total = myObject.CalculateTotal(principal, interest)
monthly_payment = _
myObject.CalculateMonthlyPayment(rate, yr, principal)
MsgBox { } & header & {
Interest rate: } & rate & {%
Years: } & 30 & {
Principal: $} & principal & {
Interest: $} & Round(interest, 2) & {
Total: $} & Round(total, 2) & {
Monthly payment: $} & Round(monthly_payment, 2)
End Sub
Mortgage Calculator (Version 2)
In this example, a few problems with syntax have been introduced:
- Two Java™ property names and two Java™ method names differ only by case.
- Two other Java™ methods are overloaded, differing only by parameter type.
- One Java™ method is over 40 characters long.
These types of syntax are allowed in Java™ but not in LotusScript®. "Mortgage2.lss" shows how to use LS2J in this situation. At the end is an example of trapping a JavaError.
Mortgage2.java
public class Mortgage2
{
// Java properties with names that differ only by case
public static String F = "Mortgage Calculator";
public static String f = "from your friendly neighborhood bank";
// Java methods
// Two Java methods with names that differ only by case
public double calculateinterest(float rate, _
short yr, double principal)
{
return rate * yr * principal;
}
public double CalculateInterest(float rate, _
short yr, double principal)
{
return calculateinterest(rate/100, yr, principal);
}
// Method with a long name
public double
CalculateTotal_with_a_method_name_over_40_characters_long
(double principal, double interest)
{
return principal + interest;
}
// Two Java overloaded methods -- differ only by parameter types
public double CalculateMonthlyPayment(double total, short yr)
{
return total / (yr * 12);
}
public double CalculateMonthlyPayment(float rate, short yr, _
double principal)
{
double interest = CalculateInterest(rate, yr, principal);
double total =
CalculateTotal_with_a_method_name_over_40_characters_long
(principal, interest);
return CalculateMonthlyPayment(total, yr);
}
}
Mortgage2.lss
Uselsx "*javacon"
Dim mySession As JavaSession
Sub Initialize
Dim myClass As JavaClass
Dim myObject As JavaObject
Dim myProperty As JavaProperty
Dim myMethod As JavaMethod
Dim myError As JavaError
Dim header1 As String, header2 As String
Dim rate As Single
Dim yr As Integer
Dim principal As Double, interest As Double
Dim total As Double, monthly_payment As Double
Set mySession = New JavaSession()
' Set LS values
rate = 8.5
yr = 30
principal = 200000
Set mySession = New JavaSession()
Set myClass = mySession.GetClass("Mortgage2;")
' Call "Mortgage2" constructor
Set myObject = myClass.CreateObject
' Use GetProperty()/GetValue() syntax when
' property names differ only by case
Set myProperty = myClass.GetProperty("F")
header1 = myProperty.GetValue()
Set myProperty = myClass.GetProperty("f")
header2 = myProperty.GetValue()
' Use GetMethod()/Invoke() syntax when method
' names differ only by case
Set myMethod = _
myClass.GetMethod("CalculateInterest", "(FSD)D")
interest = myMethod.Invoke(myObject, rate, yr, _
principal)
' or when method name is over 40 characters long
Set myMethod = myClass.GetMethod _
("CalculateTotal_with_a_method_name_over_40_characters_long", _
"(DD)D")
total = myMethod.Invoke(myObject, principal, interest)
' or for overloaded Methods (differing only by parameter type)
Set myMethod = _
myClass.GetMethod("CalculateMonthlyPayment", "(DS)D")
monthly_payment = _
myObject.CalculateMonthlyPayment(total, yr)
MsgBox { } & header1 & {
} & header2 & {
Interest rate: } & rate & {%
Years: } & 30 & {
Principal: $} & principal & {
Interest: $} & Round(interest, 2) & {
Total: $} & Round(total, 2) & {
Monthly payment: $} & Round(monthly_payment, 2)
On Error GoTo errhandler
' Throws "java.lang.NoSuchMethodException"
' when user tries to get a method that doesn't exist
Set myMethod = myClass.GetMethod("CalculateTax", _
"(D)D")
done:
Exit Sub
errhandler:
Set myError = mySession.GetLastJavaError()
MsgBox "JavaError was " & myError.errorMsg
' Clear the Error
mySession.ClearJavaError
Resume done
End Sub