5

Is there a way to create a Java class with ABCL (that extends another class)?

2
  • 1
    Armed Bear Common Lisp. It is a Common Lisp implementation that runs on the JVM.
    – compman
    Jan 24, 2011 at 20:11
  • This isn't exactly what I asked about, but it does what I want to do. This URL has an "Implementation of Java interfaces" section. You can (in Lisp) create Java objects that implement Java interfaces.
    – compman
    Jan 24, 2011 at 20:24

2 Answers 2

5

One can write directly write a Java class as JVM bytecode via the functions in the JVM package which is the code that ABCL's own compiler uses. As of abcl-0.25.0, there is unsupported code for a JAVA:JNEW-RUNTIME-CLASS method which allows one to dynamically write a Java class that calls Lisp methods for execution. The code uses classes from the ObjectWeb ASM BCEL which must be present in the JVM classpath. Exactly which version of the ASM BCEL library is needed, and whether it works with the current ABCL is untested. ABCL issue #153 tracks the work necessary to support this in the contemporary ABCL implementation.

But if one has an existing Java interface for which one would like to use Lisp based methods to provide an implementation, the process is considerably simpler (and supported!)

The relevant function is JAVA:JINTERFACE-IMPLEMENTATION whose use is demonstrated in the BankAccount example.

For the Java interface defined as

public interface BankAccount {
  public int getBalance();
  public void deposit(int amount);
  public void withdraw(int amount); 
}

The following Lisp code creates a usable Java Proxy in the current JVM:

 (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)))))))

To get a reference to this implementation from Java, one uses the code in BankMainAccount.java

  ...
  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());
  ... 
0

This example shows how to implement a Java interface in ABCL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.