LS2J の拡張機能の例

次のサンプルコードは、LS2J を使用した LotusScript® 内での Java™ メソッドの呼び出しを例示しています。この例のすぐ次の例ではコードがわずかに変更され、Java と LotusScript の構文に不一致がある状態の処理方法を示しています。

住宅ローン計算ツール

この住宅ローン計算ツールを使用すると、Java プロパティの値を取得して、Java メソッドのいくつかをその名前を使用して 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

住宅ローン計算ツール (バージョン 2)

この例では、使用されている構文に次のような問題があります。

以上のような種類の構文は、Java では許容されますが、LotusScript では使用できません。「Mortgage2.lss」は、このような状態で LS2J を使用する方法を示しています。最後に 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