[armedbear-cvs] r13186 - trunk/abcl/examples/java-interface

Mark Evenson mevenson at common-lisp.net
Wed Jan 26 10:05:48 UTC 2011


Author: mevenson
Date: Wed Jan 26 05:05:44 2011
New Revision: 13186

Log:
Add a slightly simpler example of implemeting a Java interface in Lisp.

TODO Needs further documentation.

Added:
   trunk/abcl/examples/java-interface/BankAccount.java
   trunk/abcl/examples/java-interface/BankExampleMain.java
   trunk/abcl/examples/java-interface/bank-account.lisp
   trunk/abcl/examples/java-interface/build.xml
Modified:
   trunk/abcl/examples/java-interface/README

Added: trunk/abcl/examples/java-interface/BankAccount.java
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/java-interface/BankAccount.java	Wed Jan 26 05:05:44 2011
@@ -0,0 +1,5 @@
+public interface BankAccount {
+  public int getBalance();
+  public void deposit(int amount);
+  public void withdraw(int amount);
+}
\ No newline at end of file

Added: trunk/abcl/examples/java-interface/BankExampleMain.java
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/java-interface/BankExampleMain.java	Wed Jan 26 05:05:44 2011
@@ -0,0 +1,23 @@
+import org.armedbear.lisp.Interpreter;
+import org.armedbear.lisp.Symbol;
+import org.armedbear.lisp.Packages;
+import org.armedbear.lisp.JavaObject;
+import org.armedbear.lisp.LispObject;
+
+public class BankExampleMain 
+{
+  static public void main(String argv[]) {
+    Interpreter interpreter = Interpreter.createInstance();
+    interpreter.eval("(load \"bank-account.lisp\")");
+    org.armedbear.lisp.Package defaultPackage
+      = Packages.findPackage("CL-USER");
+    Symbol bankAccountImplSymbol
+      = defaultPackage.findAccessibleSymbol("*BANK-ACCOUNT-IMPL*");
+    LispObject value = bankAccountImplSymbol.symbolValue();
+    Object object =  ((JavaObject) value).getObject();
+    BankAccount account = (BankAccount) object;
+    System.out.println("Initial balance: " + account.getBalance());
+    account.withdraw(500);
+    System.out.println("After withdrawing 500: " + account.getBalance());
+  }
+}
\ No newline at end of file

Modified: trunk/abcl/examples/java-interface/README
==============================================================================
--- trunk/abcl/examples/java-interface/README	(original)
+++ trunk/abcl/examples/java-interface/README	Wed Jan 26 05:05:44 2011
@@ -3,7 +3,7 @@
 
 To compile 
 
-    cmd$ javac  -cp ../../dist/abcl.jar  Main.java
+    cmd$ javac  -cp ../../dist/abcl.jar  Main.java BankAccountMain.java
 
 where the "../../../dist/abcl.jar" represents the path to your
 abcl.jar file, which is built via the Ant based build.  This path
@@ -27,3 +27,4 @@
 where "Main" is the initial class to run in your Java program.
 
 
+

Added: trunk/abcl/examples/java-interface/bank-account.lisp
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/java-interface/bank-account.lisp	Wed Jan 26 05:05:44 2011
@@ -0,0 +1,19 @@
+(defparameter *bank-account-impl*
+  (let ((balance 1000))
+    (jinterface-implementation
+     "BankAccount"
+
+     "getBalance" 
+       (lambda ()
+         balance)
+     "deposit" 
+        (lambda (amount) 
+          (let ((amount (jobject-lisp-value amount)))
+            (setf balance (+ balance amount))))
+     "withdraw" 
+       (lambda (amount)
+         (let ((amount (jobject-lisp-value amount)))
+           (setf balance (- balance amount)))))))
+
+(defun get-bank-account-impl () 
+  *bank-account-impl*)

Added: trunk/abcl/examples/java-interface/build.xml
==============================================================================
--- (empty file)
+++ trunk/abcl/examples/java-interface/build.xml	Wed Jan 26 05:05:44 2011
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="antlib:org.apache.tools.ant"
+         default="compile">
+
+<!--
+    XXX not working
+  <import file="../../build.xml"/>
+  
+  <target name="compile" depends="abcl-master.abcl.jar"
+-->
+  <target name="compile">
+    <javac 
+        classpath="../../dist/abcl.jar"
+        srcdir=".">
+    </javac>
+  </target>
+</project>
\ No newline at end of file




More information about the armedbear-cvs mailing list