janino.net

org.codehaus.janino
Class ClassBodyEvaluator

java.lang.Object
  extended by org.codehaus.commons.compiler.Cookable
      extended by org.codehaus.janino.SimpleCompiler
          extended by org.codehaus.janino.ClassBodyEvaluator
All Implemented Interfaces:
IClassBodyEvaluator, ICookable, ISimpleCompiler
Direct Known Subclasses:
ScriptEvaluator

public class ClassBodyEvaluator
extends SimpleCompiler
implements IClassBodyEvaluator

The optionalClassLoader serves two purposes:

A number of "convenience constructors" exist that execute the setup steps instantly.


Field Summary
 
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAME
 
Fields inherited from interface org.codehaus.commons.compiler.ICookable
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE
 
Constructor Summary
ClassBodyEvaluator()
           
ClassBodyEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator(Scanner scanner, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator(java.lang.String classBody)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
ClassBodyEvaluator(java.lang.String optionalFileName, java.io.InputStream is)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, is);
ClassBodyEvaluator(java.lang.String optionalFileName, java.io.Reader reader)
          Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, reader);
 
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)
          Compile the given compilation unit, load all generated classes, and return the class with the given name.
 void cook(Scanner scanner)
          Scans, parses and ompiles a given compilation unit from the given scanner.
static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.Class optionalBaseType, java.lang.ClassLoader optionalParentClassLoader)
          Use createInstance(Reader) instead: IClassBodyEvaluator cbe = CompilerFactoryFactory.
static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedClass, java.lang.Class[] implementedInterfaces, java.lang.ClassLoader optionalParentClassLoader)
          Deprecated. Use createInstance(Reader) instead.
 java.lang.Object createInstance(java.io.Reader reader)
          Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.
 java.lang.Class getClazz()
          Returns the loaded Class.
protected  Java.CompilationUnit makeCompilationUnit(Parser optionalParser)
          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. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements.
 void setExtendedClass(java.lang.Class optionalExtendedType)
          Set a particular superclass that the generated class will extend.
 void setExtendedType(java.lang.Class optionalExtendedClass)
          Deprecated.  
 void setImplementedInterfaces(java.lang.Class[] implementedTypes)
          Set a particular set of interfaces that the generated class will implement.
 void setImplementedTypes(java.lang.Class[] implementedInterfaces)
          Deprecated.  
 
Methods inherited from class org.codehaus.janino.SimpleCompiler
assertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
 
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
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
 

Constructor Detail

ClassBodyEvaluator

public ClassBodyEvaluator(java.lang.String classBody)
                   throws CompileException
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(classBody);

Throws:
CompileException
See Also:
ClassBodyEvaluator(), Cookable.cook(String)

ClassBodyEvaluator

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

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

ClassBodyEvaluator

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

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

ClassBodyEvaluator

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

Throws:
CompileException
java.io.IOException
See Also:
ClassBodyEvaluator(), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Reader)

ClassBodyEvaluator

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

Throws:
CompileException
java.io.IOException
See Also:
ClassBodyEvaluator(), setExtendedClass(Class), setImplementedInterfaces(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Reader)

ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner,
                          java.lang.String className,
                          java.lang.Class optionalExtendedType,
                          java.lang.Class[] implementedTypes,
                          java.lang.ClassLoader optionalParentClassLoader)
                   throws CompileException,
                          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
java.io.IOException
See Also:
ClassBodyEvaluator(), setClassName(String), setExtendedClass(Class), setImplementedInterfaces(Class[]), SimpleCompiler.setParentClassLoader(ClassLoader), Cookable.cook(Reader)

ClassBodyEvaluator

public ClassBodyEvaluator()
Method Detail

setDefaultImports

public void setDefaultImports(java.lang.String[] optionalDefaultImports)
Description copied from interface: IClassBodyEvaluator
"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
     });

Specified by:
setDefaultImports in interface IClassBodyEvaluator

setClassName

public void setClassName(java.lang.String className)
Description copied from interface: IClassBodyEvaluator
Set the name of the generated class. Defaults to IClassBodyEvaluator.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.

Specified by:
setClassName in interface IClassBodyEvaluator

setExtendedClass

public void setExtendedClass(java.lang.Class optionalExtendedType)
Description copied from interface: IClassBodyEvaluator
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.

Specified by:
setExtendedClass in interface IClassBodyEvaluator

setExtendedType

@Deprecated
public void setExtendedType(java.lang.Class optionalExtendedClass)
Deprecated. 

Specified by:
setExtendedType in interface IClassBodyEvaluator

setImplementedInterfaces

public void setImplementedInterfaces(java.lang.Class[] implementedTypes)
Description copied from interface: IClassBodyEvaluator
Set a particular set of interfaces that the generated class will implement.

Specified by:
setImplementedInterfaces in interface IClassBodyEvaluator

setImplementedTypes

@Deprecated
public void setImplementedTypes(java.lang.Class[] implementedInterfaces)
Deprecated. 

Specified by:
setImplementedTypes in interface IClassBodyEvaluator

cook

public void cook(Scanner scanner)
          throws CompileException,
                 java.io.IOException
Description copied from class: SimpleCompiler
Scans, parses and ompiles a given compilation unit from the given scanner. After completion, SimpleCompiler.getClassLoader() returns a ClassLoader that allows for access to the compiled classes.

Overrides:
cook in class SimpleCompiler
Throws:
CompileException
java.io.IOException

makeCompilationUnit

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

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

Throws:
CompileException
java.io.IOException

addPackageMemberClassDeclaration

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

Returns:
The created Java.ClassDeclaration object
Throws:
CompileException

compileToClass

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

Parameters:
compilationUnit -
Returns:
The loaded class
Throws:
CompileException

getClazz

public java.lang.Class getClazz()
Description copied from interface: IClassBodyEvaluator
Returns the loaded Class.

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

Specified by:
getClazz in interface IClassBodyEvaluator

createInstance

public java.lang.Object createInstance(java.io.Reader reader)
                                throws CompileException,
                                       java.io.IOException
Description copied from interface: IClassBodyEvaluator
Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.

Specified by:
createInstance in interface IClassBodyEvaluator
Parameters:
reader - Source of class body tokens
Returns:
An object that extends the optionalExtendedType and implements the given implementedTypes
Throws:
CompileException
java.io.IOException

createFastClassBodyEvaluator

public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner,
                                                            java.lang.Class optionalBaseType,
                                                            java.lang.ClassLoader optionalParentClassLoader)
                                                     throws CompileException,
                                                            java.io.IOException
Use createInstance(Reader) instead:
 IClassBodyEvaluator cbe = CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator();
 if (optionalBaseType != null) {
     if (optionalBaseType.isInterface()) {
         cbe.setImplementedInterfaces(new Class[] { optionalBaseType });
     } else {
         cbe.setExtendedClass(optionalBaseType);
     }
 }
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.createInstance(reader);
 

Throws:
CompileException
java.io.IOException
See Also:
createInstance(Reader)

createFastClassBodyEvaluator

@Deprecated
public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner,
                                                                       java.lang.String className,
                                                                       java.lang.Class optionalExtendedClass,
                                                                       java.lang.Class[] implementedInterfaces,
                                                                       java.lang.ClassLoader optionalParentClassLoader)
                                                     throws CompileException,
                                                            java.io.IOException
Deprecated. Use createInstance(Reader) instead.

Use createInstance(Reader) instead:
 IClassBodyEvaluator cbe = CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator();
 cbe.setExtendedClass(optionalExtendedClass);
 cbe.setImplementedInterfaces(implementedInterfaces);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.createInstance(reader);
 

Throws:
CompileException
java.io.IOException
See Also:
createInstance(Reader)

janino.net