janino.net

org.codehaus.commons.compiler
Interface IClassBodyEvaluator

All Superinterfaces:
ICookable
All Known Subinterfaces:
IExpressionEvaluator, IScriptEvaluator
All Known Implementing Classes:
ClassBodyEvaluator, ClassBodyEvaluator, ExpressionEvaluator, ExpressionEvaluator, ScriptEvaluator, ScriptEvaluator

public interface IClassBodyEvaluator
extends ICookable

Parses a class body and returns it as a 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;
   }
 

To set up an IClassBodyEvaluator object, proceed as follows:

  1. Create an IClassBodyEvaluator-implementing class.
  2. Configure the IClassBodyEvaluator by calling any of the following methods:
  3. Call any of the ICookable.cook(String, java.io.Reader) methods to scan, parse, compile and load the class body into the JVM.
To compile a class body and immediately instantiate an object, the createInstance(Reader) 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);
 }
 ...
 IClassBodyEvaluator cbe = CompilerFactoryFactory.getDefaultCompilerFactory(). newClassBodyEvaluator();
 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));
 


Field Summary
static  DEFAULT_CLASS_NAME
          Default name for the generated class.
 
Fields inherited from interface org.codehaus.commons.compiler.ICookable
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE
 
Method Summary
  createInstance( reader)
          Scans, parses and compiles a class body from the tokens delivered by the the given , then creates and returns an instance of that class.
  getClazz()
          Returns the loaded .
 void setClassName( className)
          Set the name of the generated class.
 void setDefaultImports([] 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.
 void setExtendedClass( optionalExtendedClass)
          Set a particular superclass that the generated class will extend.
 void setExtendedType( optionalExtendedClass)
          Deprecated. Use setExtendedClass(Class) instead.
 void setImplementedInterfaces([] implementedInterfaces)
          Set a particular set of interfaces that the generated class will implement.
 void setImplementedTypes([] implementedInterfaces)
          Deprecated. Use setImplementedInterfaces(Class[]) instead.
 
Methods inherited from interface org.codehaus.commons.compiler.ICookable
cook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
 

Field Detail

DEFAULT_CLASS_NAME

static final  DEFAULT_CLASS_NAME
Default name for the generated class.

See Also:
Constant Field Values
Method Detail

setDefaultImports

void setDefaultImports([] 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

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


setExtendedClass

void setExtendedClass( optionalExtendedClass)
Set a particular superclass that the generated class will extend. If null is passed, the generated class will extend .

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.


setExtendedType


void setExtendedType( optionalExtendedClass)
Deprecated. Use setExtendedClass(Class) instead.


setImplementedInterfaces

void setImplementedInterfaces([] implementedInterfaces)
Set a particular set of interfaces that the generated class will implement.


setImplementedTypes


void setImplementedTypes([] implementedInterfaces)
Deprecated. Use setImplementedInterfaces(Class[]) instead.


getClazz

 getClazz()
Returns the loaded .

This method must only be called after exactly one of the ICookable.cook(String, java.io.Reader) methods was called.


createInstance

 createInstance( reader)
                      throws CompileException,
                             
Scans, parses and compiles a class body from the tokens delivered by the the given , then creates and returns an instance of that class.

Parameters:
reader - Source of class body tokens
Returns:
An object that extends the optionalExtendedType and implements the given implementedTypes
Throws:
CompileException

janino.net