16.7.10

Make My Java Program Supports Dynamic Language(5)

Groovy

  • Problem: Still Slow.

Groovy is my second though, and I am OK with it so far, although I have some performance concern with it. According to my testing, by primitive calculation Groovy, it is about 128 times slower than the native Java program. The difference is significant. However, due to my current use cases, I would probably use Groovy as a glue language to glue up some other Java implementation with programmatic flow control

  • ClassLoader Supports

I am very happy that Groovy provides ClassLoader supports. That means it support a transparent Class.forName().

  • Resource Loader

image

image

I am happy with the implementation. The Composite Design Pattern of Resource Loader gives me a good opportunity to build my own Resource Loader, so that I can search specific PATHs/JARs.

Scala

Scala is popular now a day. But it’s not design to embedded or into Java. http://www.scala-lang.org/node/6966 is a thread talking about Scala Scripting. And here is some except:

Here are two examples:

http://people.apache.org/~mduerig/scala4scripting/

You might probably want to watch his talk from the Scala Days as well.

Here's some code I wrote some time ago (in Java!) which uses Scala for
scripting (I'm not sure which version but definitely pre-2.8):

http://github.com/jrudolph/scolorz/blob/master/src/net/virtualvoid/graph...

Actually it is quite easy to use the Scala interpreter (beware Java
code following):

import scala.tools.nsc.Interpreter;
import scala.tools.nsc.Settings;

final Interpreter interpreter = new Interpreter(new Settings());
final Painter[] painter = new Painter[1];

interpreter.bind("painter", "Array[net.virtualvoid.graphics.Painter]", painter);
String code = "object aPainter extends net.virtualvoid.graphics.Painter{\n"
+ "def paint:Unit = {\n"
+ text.getText() + "\n}\n"
+ "}\n";

interpreter.interpret(code);
interpreter.interpret("painter(0) = aPainter;");

So I defined a Painter interface and bound an array of that variable
to receive the results. Code which I executed got wrapped by code
which implements the interface. After evaluating this, the result is
stored into the array and can be accessed afterwards.

No comments: