Web Site

org.codehaus.janino
Class ClassBodyEvaluator

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

public class ClassBodyEvaluator
extends SimpleCompiler

Parses a class body and returns it as a java.lang.Class object ready for use with java.lang.reflect.

Example:

   import java.util.*;

   static private int a = 1;
   private int b = 2;

   public void func(int c, int d) {
       return func2(c, d);
   }

   private static void func2(int e, int f) {
       return e * f;
   }
 

The optionalClassLoader serves two purposes:

To set up a ClassBodyEvaluator object, proceed as follows:
  1. Create the ClassBodyEvaluator using ClassBodyEvaluator()
  2. Configure the ClassBodyEvaluator by calling any of the following methods:
  3. Call any of the Cookable.cook(Scanner) methods to scan, parse, compile and load the class body into the JVM.
Alternatively, a number of "convenience constructors" exist that execute the steps described above instantly.

To compile a class body and immediately instantiate an object, one of the createFastClassBodyEvaluator(Scanner, Class, ClassLoader) methods can be used.

The generated class may optionally extend/implement a given type; the returned instance can safely be type-casted to that optionalBaseType.

Example:

 public interface Foo {
     int bar(int a, int b);
 }
 ...
 Foo f = (Foo) ClassBodyEvaluator.createFastClassBodyEvaluator(
     new Scanner(null, new StringReader("public int bar(int a, int b) { return a + b; }")),
     Foo.class,                  // Base type to extend/implement
     (ClassLoader) null          // Use current thread's context class loader
 );
 System.out.println("1 + 2 = " + f.bar(1, 2));
 
Notice: The optionalBaseType must be accessible from the generated class, i.e. it must either be declared public, or with default accessibility in the same package as the generated class.


Field Summary
protected  java.lang.String className
           
static java.lang.String DEFAULT_CLASS_NAME
           
protected static java.lang.Class[] ZERO_CLASSES
           
 
Fields inherited from class org.codehaus.janino.SimpleCompiler
BOOT_CLASS_LOADER
 
Constructor Summary
ClassBodyEvaluator()
           
ClassBodyEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ClassBodyEvaluator(Scanner scanner, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to
ClassBodyEvaluator(java.lang.String classBody)
          Equivalent to
ClassBodyEvaluator(java.lang.String optionalFileName, java.io.InputStream is)
          Equivalent to
ClassBodyEvaluator(java.lang.String optionalFileName, java.io.Reader reader)
          Equivalent to
 
Method Summary
protected  Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit)
          To the given Java.CompilationUnit, add A class declaration with the configured name, superclass and interfaces A method declaration with the given return type, name, parameter names and values and thrown exceptions
protected  java.lang.Class compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, java.lang.String newClassName)
          Compile the given compilation unit, load all generated classes, and return the class with the given name.
 void cook(Scanner scanner)
          To be implemented by the derived classes.
static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.Class optionalBaseType, java.lang.ClassLoader optionalParentClassLoader)
          Scans, parses and compiles a class body from the tokens delivered by the the given Scanner.
static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader)
          Scans, parses and compiles a class body from the tokens delivered by the the given Scanner with no default imports.
 java.lang.Class getClazz()
          Returns the loaded Class.
protected  Java.CompilationUnit makeCompilationUnit(Scanner optionalScanner)
          Create a Java.CompilationUnit, set the default imports, and parse the import declarations.
 void setClassName(java.lang.String className)
          Set the name of the generated class.
 void setDefaultImports(java.lang.String[] optionalDefaultImports)
          "Default imports" add to the system import "java.lang", i.e.
 void setExtendedType(java.lang.Class optionalExtendedType)
          Set a particular superclass that the generated class will extend.
 void setImplementedTypes(java.lang.Class[] implementedTypes)
          Set a particular set of interfaces that the generated class will implement.
 
Methods inherited from class org.codehaus.janino.SimpleCompiler
assertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, equals, getClassLoader, hashCode, main, setParentClassLoader, setParentClassLoader, setUpClassLoaders
 
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

DEFAULT_CLASS_NAME

public static final java.lang.String DEFAULT_CLASS_NAME
See Also:
Constant Field Values

ZERO_CLASSES

protected static final java.lang.Class[] ZERO_CLASSES

className

protected java.lang.String className
Constructor Detail

ClassBodyEvaluator

public ClassBodyEvaluator(java.lang.String classBody)
                   throws CompileException,
                          Parser.ParseException,
                          Scanner.ScanException
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(classBody);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
See Also:
ClassBodyEvaluator(), Cookable.cook(String)

ClassBodyEvaluator

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

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

ClassBodyEvaluator

public ClassBodyEvaluator(java.lang.String optionalFileName,
                          java.io.Reader reader)
                   throws CompileException,
                          Parser.ParseException,
                          Scanner.ScanException,
                          java.io.IOException
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, reader);

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

ClassBodyEvaluator

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

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

ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner,
                          java.lang.Class optionalExtendedType,
                          java.lang.Class[] implementedTypes,
                          java.lang.ClassLoader optionalParentClassLoader)
                   throws CompileException,
                          Parser.ParseException,
                          Scanner.ScanException,
                          java.io.IOException
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ClassBodyEvaluator(), setExtendedType(Class), setImplementedTypes(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner,
                          java.lang.String className,
                          java.lang.Class optionalExtendedType,
                          java.lang.Class[] implementedTypes,
                          java.lang.ClassLoader optionalParentClassLoader)
                   throws CompileException,
                          Parser.ParseException,
                          Scanner.ScanException,
                          java.io.IOException
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setClassName(className);
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ClassBodyEvaluator(), setClassName(String), setExtendedType(Class), setImplementedTypes(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Scanner)

ClassBodyEvaluator

public ClassBodyEvaluator()
Method Detail

setDefaultImports

public void setDefaultImports(java.lang.String[] optionalDefaultImports)
"Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements.

Notice that JDK 5 "static imports" are also supported, as shown in the following example.

Example:

     sc.setDefaultImports(new String[] {
         "java.util.Map",                          // Single type import
         "java.io.*",                              // Type-import-on-demand
         "static java.util.Collections.EMPTY_MAP", // Single static import
         "static java.util.Collections.*",         // Static-import-on-demand
     });


setClassName

public void setClassName(java.lang.String className)
Set the name of the generated class. Defaults to DEFAULT_CLASS_NAME. In most cases, there is no need to set this name, because the generated class is loaded into its own ClassLoader where its name cannot collide with classes generated by other evaluators.

One reason to use this function is to have a class name in a non-default package, which can be relevant when types and members with DEFAULT accessibility are accessed.


setExtendedType

public void setExtendedType(java.lang.Class optionalExtendedType)
Set a particular superclass that the generated class will extend. If null is passed, the generated class will extend Object.

The common reason to set a base class for an evaluator is that the generated class can directly access the base superclass's (non-private) members.


setImplementedTypes

public void setImplementedTypes(java.lang.Class[] implementedTypes)
Set a particular set of interfaces that the generated class will implement.


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.

Overrides:
cook in class SimpleCompiler
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException

makeCompilationUnit

protected final Java.CompilationUnit makeCompilationUnit(Scanner optionalScanner)
                                                  throws Parser.ParseException,
                                                         Scanner.ScanException,
                                                         java.io.IOException
Create a Java.CompilationUnit, set the default imports, and parse the import declarations.

If the optionalScanner is given, a sequence of IMPORT directives is parsed from it and added to the compilation unit.

Throws:
Parser.ParseException
Scanner.ScanException
java.io.IOException

addPackageMemberClassDeclaration

protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location,
                                                                              Java.CompilationUnit compilationUnit)
                                                                       throws Parser.ParseException
To the given Java.CompilationUnit, add

Returns:
The created Java.ClassDeclaration object
Throws:
Parser.ParseException

compileToClass

protected final java.lang.Class compileToClass(Java.CompilationUnit compilationUnit,
                                               EnumeratorSet debuggingInformation,
                                               java.lang.String newClassName)
                                        throws CompileException
Compile the given compilation unit, load all generated classes, and return the class with the given name.

Parameters:
compilationUnit -
debuggingInformation - TODO
newClassName - The fully qualified class name
Returns:
The loaded class
Throws:
CompileException

getClazz

public java.lang.Class getClazz()
Returns the loaded Class.

This method must only be called after cook(Scanner).

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


createFastClassBodyEvaluator

public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner,
                                                            java.lang.Class optionalBaseType,
                                                            java.lang.ClassLoader optionalParentClassLoader)
                                                     throws CompileException,
                                                            Parser.ParseException,
                                                            Scanner.ScanException,
                                                            java.io.IOException
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner. The generated class has the DEFAULT_CLASS_NAME and extends the given optionalBaseType (if that is a class), and implements the given optionalBaseType (if that is an interface).

For an explanation of the "fast class body evaluator" concept, see the class description.

Parameters:
scanner - Source of class body tokens
optionalBaseType - Base type to extend/implement
optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader"
Returns:
an object that extends/implements the given optionalBaseType
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ClassBodyEvaluator

createFastClassBodyEvaluator

public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner,
                                                            java.lang.String className,
                                                            java.lang.Class optionalExtendedType,
                                                            java.lang.Class[] implementedTypes,
                                                            java.lang.ClassLoader optionalParentClassLoader)
                                                     throws CompileException,
                                                            Parser.ParseException,
                                                            Scanner.ScanException,
                                                            java.io.IOException
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner with no default imports.

For an explanation of the "fast class body evaluator" concept, see the class description.

Parameters:
scanner - Source of class body tokens
className - Name of generated class
optionalExtendedType - Class to extend
implementedTypes - Interfaces to implement
optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader"
Returns:
an object that extends the optionalExtendedType and implements the given implementedTypes
Throws:
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
See Also:
ClassBodyEvaluator

Web Site