jdtc Module

jdtc module provides functions to compile Java code on the fly.

High Level Function

loadJava( javaCode )

loadJava() compiles the specified java code and imports the resulting class, which can then be accessed by the name. This function returns a boolean value that indicates whether the compilation succeeded.

javaCode should be either of the following type.

loadJava("public class A {} ")
A     ==> class A

loadJava(getFile("/tmp/B.java"))
B()       ==> B@XXXX

When an object array or a Collection is specified, each element is passed to the compiler.

x = list()
x.add(getFile("A.java"))
x.add(getFile("B.java"))
loadJava(x)

When a Map object is specified, the key should be class names and the value should be a compilation unit; i.e. String, File, URL, Reader, or InputStream.

m = map()
m.put("A", "public class A {}")
m.put("B", "public class B extends A {}")
loadJava(m)

Low Level Function

jc = javaCompiler( { classLoader } )
jc . compile( javaCode )

javaCompiler() returns a handle object that provides compile() method. If classLoader is specified, a child classloader is created for classloading, otherwise, the value of Context.getClassLoader() is used by default.

compile() method compiles the specified Java code and returns a boolean value that indicates whether it succeeded. javaCode should be either of one that can be passed to loadJava() function described above.

jc = javaCompiler()
jc.compile("public class Hello {}")    ==> true

The object returned by javaCompiler() can be used as if it were a Map object that maps a class name to the class.

jc["Hello"]    ==> class Hello
jc.Hello       ==> class Hello

Attributes

jc . classLoader

The ClassLoader to which the compiled code is loaded. This attribute is read-only.

When the class loader is selected in the executing context, the classes defined in the class loader can be accessed by their names.

getContext().setClassLoader(jc.classLoader)
Hello

The value of classLoader attribute can be used as if it were a Map object that maps a class name to the class.

jc.classLoader.Hello       ==> class Hello
jc.classLoader["Hello"]       ==> class Hello
jc . sourcePaths

The source search paths for subsequent compilations. When assigning a value to the attribute, it should be either of a CLASSPATH-style string or an array of File/URL objects.

jc.sourcePaths = "/home/src.jar:."

jc.sourcePaths = [getURL("jar:file:/home/src.jar!/project/src")]