org.codehaus.janino

Class ClassBodyEvaluator

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 cook 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 ClassBodyEvaluator 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 StringclassName
static StringDEFAULT_CLASS_NAME
protected static Class[]ZERO_CLASSES
Constructor Summary
ClassBodyEvaluator(String classBody)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(classBody);
ClassBodyEvaluator(String optionalFileName, InputStream is)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, is);
ClassBodyEvaluator(String optionalFileName, Reader reader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, reader);
ClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);
ClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);
ClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setClassName(className);
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);
ClassBodyEvaluator()
Method Summary
protected Java.PackageMemberClassDeclarationaddPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit)
To the given 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 ClasscompileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName)
Compile the given compilation unit, load all generated classes, and return the class with the given name.
voidcook(Scanner scanner)
static ObjectcreateFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner.
static ObjectcreateFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner with no default imports.
ClassgetClazz()
Returns the loaded Class.
protected Java.CompilationUnitmakeCompilationUnit(Scanner optionalScanner)
Create a CompilationUnit, set the default imports, and parse the import declarations.
voidsetClassName(String className)
Set the name of the generated class.
voidsetDefaultImports(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.
voidsetExtendedType(Class optionalExtendedType)
Set a particular superclass that the generated class will extend.
voidsetImplementedTypes(Class[] implementedTypes)
Set a particular set of interfaces that the generated class will implement.

Field Detail

className

protected String className

DEFAULT_CLASS_NAME

public static final String DEFAULT_CLASS_NAME

ZERO_CLASSES

protected static final Class[] ZERO_CLASSES

Constructor Detail

ClassBodyEvaluator

public ClassBodyEvaluator(String classBody)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(classBody);

See Also: ClassBodyEvaluator cook

ClassBodyEvaluator

public ClassBodyEvaluator(String optionalFileName, InputStream is)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, is);

See Also: ClassBodyEvaluator Cookable

ClassBodyEvaluator

public ClassBodyEvaluator(String optionalFileName, Reader reader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, reader);

See Also: ClassBodyEvaluator Cookable

ClassBodyEvaluator

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

See Also: ClassBodyEvaluator setParentClassLoader cook

ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

See Also: ClassBodyEvaluator setExtendedType (Class[]) setParentClassLoader cook

ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setClassName(className);
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

See Also: ClassBodyEvaluator setClassName setExtendedType (Class[]) setParentClassLoader cook

ClassBodyEvaluator

public ClassBodyEvaluator()

Method Detail

addPackageMemberClassDeclaration

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

Returns: The created ClassDeclaration object

compileToClass

protected final Class compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName)
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

cook

public void cook(Scanner scanner)

createFastClassBodyEvaluator

public static Object createFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader)
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

See Also: ClassBodyEvaluator

createFastClassBodyEvaluator

public static Object createFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
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

See Also: ClassBodyEvaluator

getClazz

public Class getClazz()
Returns the loaded Class.

This method must only be called after cook.

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

makeCompilationUnit

protected final Java.CompilationUnit makeCompilationUnit(Scanner optionalScanner)
Create a 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.

setClassName

public void setClassName(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 java.lang.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.

setDefaultImports

public void setDefaultImports(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
     });

setExtendedType

public void setExtendedType(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(Class[] implementedTypes)
Set a particular set of interfaces that the generated class will implement.