janino.net

org.codehaus.janino
Class SimpleCompiler

java.lang.Object
  extended by org.codehaus.janino.Cookable
      extended by org.codehaus.janino.SimpleCompiler
Direct Known Subclasses:
ClassBodyEvaluator

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(ClassLoader).
  3. Call any of the Cookable.cook(Scanner) 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 java.lang.ClassLoader BOOT_CLASS_LOADER
          A ClassLoader that finds the classes on the JVM's boot class path (e.g.
 
Constructor Summary
SimpleCompiler()
           
SimpleCompiler(Scanner scanner, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
SimpleCompiler(java.lang.String fileName)
          Equivalent to
SimpleCompiler(java.lang.String optionalFileName, java.io.InputStream is)
          Equivalent to
SimpleCompiler(java.lang.String optionalFileName, java.io.Reader in)
          Equivalent to
 
Method Summary
protected  void assertNotCooked()
          Throw an IllegalStateException if this Cookable is already cooked.
protected  Java.Type[] classesToTypes(Location location, java.lang.Class[] classes)
          Convert an array of Classes into an array ofJava.Types.
protected  Java.Type classToType(Location location, java.lang.Class optionalClass)
          Wrap a reflection Class in a Java.Type object.
protected  java.lang.ClassLoader compileToClassLoader(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation)
          Compile the given compilation unit.
 void cook(Java.CompilationUnit compilationUnit)
          Cook this compilation unit directly.
 void cook(Scanner scanner)
          To be implemented by the derived classes.
 boolean equals(java.lang.Object o)
          Two SimpleCompilers are regarded equal iff Both are objects of the same class (e.g.
 java.lang.ClassLoader getClassLoader()
          Returns a ClassLoader object through which the previously compiled classes can be accessed.
 int hashCode()
           
static void main(java.lang.String[] args)
           
 void setParentClassLoader(java.lang.ClassLoader optionalParentClassLoader)
          The "parent class loader" is used to load referenced classes.
 void setParentClassLoader(java.lang.ClassLoader optionalParentClassLoader, java.lang.Class[] auxiliaryClasses)
          Allow references to the classes loaded through this parent class loader (@see setParentClassLoader(ClassLoader)), plus the extra auxiliaryClasses.
protected  void setUpClassLoaders()
          Initializes classLoader and iClassLoader from the configured parentClassLoader and optionalAuxiliaryClasses.
 
Methods inherited from class org.codehaus.janino.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOOT_CLASS_LOADER

public static final java.lang.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(java.lang.String optionalFileName,
                      java.io.Reader in)
               throws java.io.IOException,
                      Scanner.ScanException,
                      Parser.ParseException,
                      CompileException
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, in);

Throws:
java.io.IOException
Scanner.ScanException
Parser.ParseException
CompileException
See Also:
SimpleCompiler(), Cookable.cook(String, Reader)

SimpleCompiler

public SimpleCompiler(java.lang.String optionalFileName,
                      java.io.InputStream is)
               throws java.io.IOException,
                      Scanner.ScanException,
                      Parser.ParseException,
                      CompileException
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(optionalFileName, is);

Throws:
java.io.IOException
Scanner.ScanException
Parser.ParseException
CompileException
See Also:
SimpleCompiler(), Cookable.cook(String, InputStream)

SimpleCompiler

public SimpleCompiler(java.lang.String fileName)
               throws java.io.IOException,
                      Scanner.ScanException,
                      Parser.ParseException,
                      CompileException
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.cook(fileName);

Throws:
java.io.IOException
Scanner.ScanException
Parser.ParseException
CompileException
See Also:
SimpleCompiler(), Cookable.cookFile(String)

SimpleCompiler

public SimpleCompiler(Scanner scanner,
                      java.lang.ClassLoader optionalParentClassLoader)
               throws java.io.IOException,
                      Scanner.ScanException,
                      Parser.ParseException,
                      CompileException
Equivalent to
 SimpleCompiler sc = new SimpleCompiler();
 sc.setParentClassLoader(optionalParentClassLoader);
 sc.cook(scanner);

Throws:
java.io.IOException
Scanner.ScanException
Parser.ParseException
CompileException
See Also:
SimpleCompiler(), setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

SimpleCompiler

public SimpleCompiler()
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception

setParentClassLoader

public void setParentClassLoader(java.lang.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(java.lang.ClassLoader optionalParentClassLoader,
                                 java.lang.Class[] auxiliaryClasses)
Allow references to the classes loaded through this parent class loader (@see setParentClassLoader(ClassLoader)), 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.


cook

public void cook(Scanner scanner)
          throws CompileException,
                 Parser.ParseException,
                 Scanner.ScanException,
                 java.io.IOException
Description copied from class: Cookable
To be implemented by the derived classes.

Specified by:
cook in class Cookable
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

cook

public void cook(Java.CompilationUnit compilationUnit)
          throws CompileException
Cook this compilation unit directly. See Cookable.cook(org.codehaus.janino.Scanner)

Throws:
CompileException

setUpClassLoaders

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


getClassLoader

public java.lang.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(Scanner, ClassLoader) 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(Scanner).

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


equals

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

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

classToType

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


classesToTypes

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


compileToClassLoader

protected final java.lang.ClassLoader compileToClassLoader(Java.CompilationUnit compilationUnit,
                                                           EnumeratorSet debuggingInformation)
                                                    throws CompileException
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

assertNotCooked

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


janino.net