org.codehaus.janino

Class SimpleCompiler

public class SimpleCompiler extends Cookable

A simplified version of Compiler that can compile only a single compilation unit. (A "compilation unit" is the characters stored in a ".java" file.)

Opposed to a normal ".java" file, you can declare multiple public classes here.

To set up a SimpleCompiler object, proceed as follows:

  1. Create the SimpleCompiler using SimpleCompiler
  2. Optionally set an alternate parent class loader through setParentClassLoader.
  3. Call any of the cook methods to scan, parse, compile and load the compilation unit into the JVM.
Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly.
Field Summary
static ClassLoaderBOOT_CLASS_LOADER
A ClassLoader that finds the classes on the JVM's boot class path (e.g.
Constructor Summary
SimpleCompiler(String optionalFileName, Reader in)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, in);
SimpleCompiler(String optionalFileName, InputStream is)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, is);
SimpleCompiler(String fileName)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(fileName);
SimpleCompiler(Scanner scanner, ClassLoader optionalParentClassLoader)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.setParentClassLoader(optionalParentClassLoader);
 sc.cook(scanner);
SimpleCompiler()
Method Summary
protected voidassertNotCooked()
Throw an IllegalStateException if this Cookable is already cooked.
protected Java.Type[]classesToTypes(Location location, Class[] classes)
Convert an array of Classes into an array ofTypes.
protected Java.TypeclassToType(Location location, Class optionalClass)
Wrap a reflection Class in a Type object.
protected ClassLoadercompileToClassLoader(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation)
Compile the given compilation unit.
voidcook(Scanner scanner)
voidcook(Java.CompilationUnit compilationUnit)
Cook this compilation unit directly.
booleanequals(Object o)
Two SimpleCompilers are regarded equal iff
  • Both are objects of the same class (e.g. both are ScriptEvaluators)
  • Both generated functionally equal classes as seen by equals
ClassLoadergetClassLoader()
Returns a ClassLoader object through which the previously compiled classes can be accessed.
inthashCode()
static voidmain(String[] args)
voidsetParentClassLoader(ClassLoader optionalParentClassLoader)
The "parent class loader" is used to load referenced classes.
voidsetParentClassLoader(ClassLoader optionalParentClassLoader, Class[] auxiliaryClasses)
Allow references to the classes loaded through this parent class loader (@see setParentClassLoader), plus the extra auxiliaryClasses.
protected voidsetUpClassLoaders()
Initializes classLoader and iClassLoader from the configured parentClassLoader and optionalAuxiliaryClasses.

Field Detail

BOOT_CLASS_LOADER

public static final ClassLoader BOOT_CLASS_LOADER
A ClassLoader that finds the classes on the JVM's boot class path (e.g. java.io.*), but not the classes on the JVM's class path.

Constructor Detail

SimpleCompiler

public SimpleCompiler(String optionalFileName, Reader in)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, in);

See Also: SimpleCompiler Cookable

SimpleCompiler

public SimpleCompiler(String optionalFileName, InputStream is)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, is);

See Also: SimpleCompiler Cookable

SimpleCompiler

public SimpleCompiler(String fileName)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(fileName);

See Also: SimpleCompiler cookFile

SimpleCompiler

public SimpleCompiler(Scanner scanner, ClassLoader optionalParentClassLoader)
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.setParentClassLoader(optionalParentClassLoader);
 sc.cook(scanner);

See Also: SimpleCompiler setParentClassLoader cook

SimpleCompiler

public SimpleCompiler()

Method Detail

assertNotCooked

protected void assertNotCooked()
Throw an IllegalStateException if this Cookable is already cooked.

classesToTypes

protected Java.Type[] classesToTypes(Location location, Class[] classes)
Convert an array of Classes into an array ofTypes.

classToType

protected Java.Type classToType(Location location, Class optionalClass)
Wrap a reflection Class in a Type object.

compileToClassLoader

protected final ClassLoader compileToClassLoader(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation)
Compile the given compilation unit. (A "compilation unit" is typically the contents of a JavaTM source file.)

Parameters: compilationUnit The parsed compilation unit debuggingInformation What kind of debugging information to generate in the class file

Returns: The ClassLoader into which the compiled classes were defined

Throws: CompileException

cook

public void cook(Scanner scanner)

cook

public void cook(Java.CompilationUnit compilationUnit)
Cook this compilation unit directly. See Cookable

equals

public boolean equals(Object o)
Two SimpleCompilers are regarded equal iff

getClassLoader

public ClassLoader getClassLoader()
Returns a ClassLoader object through which the previously compiled classes can be accessed. This ClassLoader can be used for subsequent calls to SimpleCompiler in order to compile compilation units that use types (e.g. declare derived types) declared in the previous one.

This method must only be called after cook.

This method must not be called for instances of derived classes.

hashCode

public int hashCode()

main

public static void main(String[] args)

setParentClassLoader

public void setParentClassLoader(ClassLoader optionalParentClassLoader)
The "parent class loader" is used to load referenced classes. Useful values are:
System.getSystemClassLoader() The running JVM's class path
Thread.currentThread().getContextClassLoader() or null The class loader effective for the invoking thread
BOOT_CLASS_LOADER The running JVM's boot class path
The parent class loader defaults to the current thread's context class loader.

setParentClassLoader

public void setParentClassLoader(ClassLoader optionalParentClassLoader, Class[] auxiliaryClasses)
Allow references to the classes loaded through this parent class loader (@see setParentClassLoader), plus the extra auxiliaryClasses.

Notice that the auxiliaryClasses must either be loadable through the optionalParentClassLoader (in which case they have no effect), or no class with the same name must be loadable through the optionalParentClassLoader.

setUpClassLoaders

protected final void setUpClassLoaders()
Initializes classLoader and iClassLoader from the configured parentClassLoader and optionalAuxiliaryClasses. These are needed by SimpleCompiler and friends which are used when creating the AST.