Dynamic Languages and Java/Compilers in Java

From JVMLanguages

Table of contents

Jython

Jython is a compiler that allows you to execute Python source code in a Java Virtual Machine. Jython started as a XXX project called JPython, and changed its name when it was open-sourced by XXX.

The Jython project is actually composed of two distinct components:

jython
The jython component looks like an interpreter because it takes Python source code and evaluates it one statement at a time. However, what it really does behind the scenes is convert each statement to Java bytecode and then execute this bytecode.
jythonc
jythonc, on the other hand, is a code-generator that takes Python source code and generates Java source code.

It's not clear why Jython supports both bytecode and source-code generation. However, the ability to either evaluate source code at run-time or generate Java code is commendable. The rest of this section will refer to jython.

Invoking Jython

   import org.python.util.PythonInterpreter;
   import org.python.core.*;

   public class SimpleEmbedded {
       public static void main(String []args)
   	throws PyException
       {
   	PythonInterpreter interp = new PythonInterpreter();

   	interp.exec("import sys");
   	interp.exec("print sys");

   	interp.set("a", new PyInteger(42));
   	interp.exec("print a");
   	interp.exec("x = 2+2");
   	PyObject x = interp.get("x");

   	System.out.println("x: "+x);
       }
   }

Supplying Configuration Parameters

   See http://jython.sourceforge.net/docs/registry.html#finding. 

Interacting with Java objects from Python

Like BeanShell, Jython also provides the ability to override Java's access control checking when you call Java methods or fields. To turn on accessibility, set the Jython property python.security.respectJavaAccessibility to false.

Interacting with Python objects from Java

BSF Support

A BSFEngine for Jython is included with the BSF library. You do not need to register Jython as a scripting language; it is available under the name jython.

Unlike most other BSFEngine implementations, the Jython engine does not do any type conversions. It will return Jython-specific data structures such as PyInteger, PyFloat, and PyString.

Although jythonc supports generating Java source code, the BSF engine for Jython does not implement this. Consequently, BSF will generate a Java source file that simply invokes jython through BSF.

Groovy

Invoking Groovy

Interacting with Java objects from Groovy

Interacting with Groovy objects from Java

BSF Support

org.codehaus.groovy.bsf.GroovyEngine acts as a BSFEngine for Groovy.

It is usually registered as follows:

   BSFManager.registerScriptingEngine(
   	"groovy", 
   	"org.codehaus.groovy.bsf.GroovyEngine", 
   	new String[] { "groovy", "gy" }
   );

JSR-223 Support

The implementation of JSR-223 available from Sun comes with an EngineFactory for Groovy called com.sun.script.groovy.GroovyScriptEngineFactory. It's unclear at this point whether the Groovy team will be taking ownership of this code and deploying it along with future versions of Groovy.

Kawa

Kawa is a scheme compiler for Java. Like Jython, Kawa translates source code into Java bytecode one statement at a time, so it can be used in the same way that you should use an interpreter.

Invoking Scheme Scripts

Interacting with Java objects from Scheme

Interacting with Scheme objects from Java

BSF Support

A BSFEngine implementation has been written for Kawa, although it is not currently part of either the Kawa or BSF distribution. It is available from XXXXX.

Should drop a line to the Kawa guys recommending they add it.

Other Compilers

There are a number of other languages with compilers available for Java.

Nice

Nice is an attempt to integrate many of the dynamic techniques that have been more common in functions languages like ML. Some of these, like parametric classes (i.e., generics) and enumerations are available in Java as of version 1.5. Others, like the ability to pass named fields into constructors, are present simply for convenience and maintainability. Still other features, such as value dispatch and design by contract, remain useful features that are rarely seen outside of academia.

One of Nice's nicest features is the extra type checking done to ensure that NullPointerException and ClassCastException exceptions will never occur at run-time.

Kiev

ObjectScript

Smalltalk Compiler

Ruby Compiler

The JRuby project discussed in the previous chapter is currently implementing a compiler that will generate Java bytecode.

Source-Level Integration

When you have a compiler that can emit Java bytecode, one question that arises is, do you need a programatic interface to that project at all? jythonc lets you do something similar to this, since at compile-time you can decide to convert your Python source code into Java source code and compile it along with the rest of your application. But wouldn't it be better if you could tell the Java class loading mechanism to invoke this compiler on demand whenever it encounters source code in one of the supported languages.

Dawid Weiss, in an article to JavaWorld, took this even further -- he did the same thing with an interpreter rather than a compiler. He created a Java ClassLoader implementation that can detect BeanShell source code in the classpath and generate a proxy class on-the-fly that forwards all method calls on to the BeanShell interpreter.

See http://www.javaworld.com/javaworld/jw-10-2003/jw-1003-beanshell.html

Continue on to Chapter 7: Bridges in Java.