Blog Post

#416 Java FFI is Now Available

brian Sun 14 Dec 2008

Its been a long time coming, but Build 1.0.36 includes the first version of the Java FFI feature. To get started take a look at:

  • new docLang chapter with details on the Java FFI feature
  • /examples/java/swing.fan is a script which demos basic Swing app (couldn't get to work on my Mac yet)
  • compilerJava is written using FFI via bootstrap: src/compilerJava/fan/ReflectUtil.fan

Here is a very simple script which uses java.util.Date:

using [java] java.util
class Test
{
  Void main()
  {
    date := Date()               // constructors
    year := date.getYear + 1900  // methods
    millis := date->getTime      // dynamic invokes
    echo("$date, $year, $millis")
  }
}

The Java FFI is by no means complete, but this first version provides a pretty awesome amount of interoperability with Java:

  • import Java packages as Fan pods
  • call out to just about any Java API
  • construct Java classes
  • subclass Java classes and implement Java interfaces
  • full support for Java primitives
  • implicit boxing b/w Java Object arrays and Fan sys::List
  • direct symbolic support for Java primitive arrays

Features which aren't in this version, but I plan on working on immediately for the next build:

  • Overloading methods which use primitives or arrays
  • Coercion of a Fan function to a Java interface with one method
  • Resolution of method overload according to JLS still has some quirks when combined with Fan's implicit coercions - so I need to tidy this up

Features which I'm planning on delaying (probably for a while):

  • Ability to subclass from a Java class more than one level deep
  • Ability to override overloaded methods
  • Multi-dimensional arrays

With something this complicated I expect there will be lots of bugs and issues I didn't consider. So take it for a spin and give me your feedback!

Login or Signup to reply.