Tuesday, March 4, 2008

Customizable Closure with OpenJDK MLVM and javac

The customizable closure code listed below, take advantage of following,
  1. Closure data types are converted to inner classes at compile time.
  2. AnnonymousClassLoader in MLVM, allows us to load multiple definitions of same class in the same instance of the class loader at run time.
Code:
import java.dyn.AnonymousClassLoader;

public class CustomizableClosure {

static private {String => void} welcomeClosure =
{ String name => System.out.println(name + ", " + shortWelcome);};

static String shortWelcome = " Welcome to closure...";
static String detailedWelcome =
" Welcome to MLVM closure customization..Experience the power of AnonymousClassLoader...";

public static void main(String[] args) throws Exception {
CustomizableClosure.testSimpleClosure();
CustomizableClosure.testCustomizableClosure();
}

static void testSimpleClosure() {
System.out.println("=================");
System.out.println("Before customization..");
welcomeClosure.invoke("Dear bob");
System.out.println("=================");
}

static void testCustomizableClosure() throws Exception {
AnonymousClassLoader acl = new AnonymousClassLoader();

acl.setClassFile(welcomeClosure.getClass());
Class hostClass = CustomizableClosure.class;

Class acls = welcomeClosure.getClass();
acl.putSymbolPatch("shortWelcome", "detailedWelcome");
acls = acl.loadClass();

{String => void} obj = ({String => void}) acls.newInstance();
System.out.println("================");
System.out.println("After customization..");
obj.invoke("Dear bob");
System.out.println("================");
}
}
Output:

=================
Before customization..
Dear bob, Welcome to closure...
=================
================
After customization..
Dear bob, Welcome to MLVM closure customization..Experience the power of AnonymousClassLoader...
=================

OK, what's going on?
  1. {String => void} welcomeClosure = { String name => System.out.println(name + ", " + shortWelcome);} this closure snippet is converted to inner class at compile time.
  2. In method "testSimpleClosure", we are executing closure code as is.
  3. In method "testCustomizableClosure", we take closure (compiled inner class) as base template, customize it (by changing welcome text) at run time and execute it.
Step by step instructions on how to build and run Customizable Closure code.
  1. Build OpenJDK and Hotspot.
  2. Apply MLVM patch and rebuild Hotspot.
  3. Download javac for closures from javac.info
  4. Prepend MLVM AnonymousClassLoader.class and javac.jar to bootclasspath of java and javac. In my local machine, i used following commands for compilation and execution.
  • javac -J-Xbootclasspath/p:/share/software/OpenJDK/closures-2008-02-12/lib/javac.jar:/share/software/OpenJDK/mlvm/bootcp -source 7 -d classes/ CustomizableClosure.java
  • java -Xbootclasspath/p:/share/software/OpenJDK/closures-2008-02-12/lib/javac.jar:/share/software/OpenJDK/mlvm/bootcp -cp classes CustomizableClosure