Update: A bunch of code from hotspot was just integrated into the bsd-port and some of it breaks the build. Use my distribution at the end of this blog post if you want to test the features.

In December JSR-335 was approved by the JCP and is now starting. I’ve joined as member of the expert group, but like with most recent JSRs all the real discussion will take place on the public mailing list. A lot of work has already been done in OpenJDK to support the features of this JSR which will ultimately be included in Java 8 if it completes successfully. Here is the current state of the JSR. Though JDK 8 is when official support would ship, you can already build and execute the draft features of JSR-335 by using the regular OpenJDK 7 build along with some compile-time and run-time tools. Before I go into the guts of how to build them, here are some examples of what you will be able to do:

Create closures that are converted to Single Abstract Method (SAM) implementations when used in context

Create interfaces that specify default behavior for methods that are unimplemented by the implementing class

Reference methods in source and then convert those references to SAM implementations when used in context

OpenJDK 7 doesn’t actually support everything that we need to run code generated for Lambda though. We need to also build a java agent that will modify the bytecodes in classes as they are loaded to run on the older VM for some features. Here is how you do that:

Now we have Test.class file that will only work in a VM that supports some of the bytecode that we are using. In this case we don’t need the transformations so we can execute this code like this:
macpro:jsr335 sam$ java -cp . Test
ran

Here is some code that will not run out of the box, that shows you how you can now write extension methods:

If you try and run this without the agent you will get this:
macpro:jsr335 sam$ java -cp . Test2
Exception in thread "main" java.lang.AbstractMethodError: Test2$Test3.inc(I)I
at Test2.main(Test2.java:9)

In order to run this we need to enable InvokeDynamic on our VM and include the classes from langtools for some runtime support:
macpro:jsr335 sam$ java -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic -Xbootclasspath/p:$LANGTOOLS_JAR -javaagent:$AGENTJAR -cp . Test3
Test

That command line has everything you should ever need to execute code generated using the Lamba enhancements so using anything else is probably unnecessary. Now let’s put all of this together into the final example from the Lambda proposal itself:

The punchline is that if you read through all of this you get a bonus: here is the distribution of above build products that you can download and run on your Mac (assuming you are running a 64-bit build of snow leopard):

Hey Sam, I couldn’t find a guestbook for your blog so I figured I would just post here.

I saw your recent comment over at newton regarding groupon, and just wanted to drop a line here saying that it was helpful.

Alexander Turner

I am guessing building on Linux will be pretty much the same. How is the JSR going?

Omar

I don’t like that syntax. Even the coffeescript is better to me. Would be cool to had something like:
List orderedList = list.sortBy((current, pos){
//Some code here
});
The way of Java 8 looks more verbose than it must be. Is even clearer to have an Interface and create the body of each method:
inteface IList{
—-public List sortBy(current, pos);
}