|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.commons.compiler.Cookable
org.codehaus.janino.SimpleCompiler
org.codehaus.janino.ClassBodyEvaluator
public class ClassBodyEvaluator
The optionalClassLoader
serves two purposes:
ByteArrayClassLoader
, or by creation of a temporary
ByteArrayClassLoader
if not.
Field Summary | |
---|---|
protected java.lang.String |
className
|
protected static java.lang.Class[] |
ZERO_CLASSES
|
Fields inherited from class org.codehaus.janino.SimpleCompiler |
---|
debugLines, debugSource, debugVars |
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(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,
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)
|
static java.lang.Object |
createFastClassBodyEvaluator(Scanner scanner,
java.lang.Class optionalBaseType,
java.lang.ClassLoader optionalParentClassLoader)
Use createInstance(Reader) instead: |
static java.lang.Object |
createFastClassBodyEvaluator(Scanner scanner,
java.lang.String className,
java.lang.Class optionalExtendedClass,
java.lang.Class[] implementedInterfaces,
java.lang.ClassLoader optionalParentClassLoader)
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(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 |
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, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setUpClassLoaders |
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, setDebuggingInformation, setParentClassLoader |
Field Detail |
---|
protected static final java.lang.Class[] ZERO_CLASSES
protected java.lang.String className
Constructor Detail |
---|
public ClassBodyEvaluator(java.lang.String classBody) throws CompileException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
CompileException
ClassBodyEvaluator()
,
Cookable.cook(String)
public ClassBodyEvaluator(java.lang.String optionalFileName, java.io.InputStream is) throws CompileException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, is);
CompileException
java.io.IOException
ClassBodyEvaluator()
,
Cookable.cook(String, InputStream)
public ClassBodyEvaluator(java.lang.String optionalFileName, java.io.Reader reader) throws CompileException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, reader);
CompileException
java.io.IOException
ClassBodyEvaluator()
,
Cookable.cook(String, Reader)
public ClassBodyEvaluator(Scanner scanner, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
CompileException
java.io.IOException
ClassBodyEvaluator()
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Reader)
public ClassBodyEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
CompileException
java.io.IOException
ClassBodyEvaluator()
,
setExtendedClass(Class)
,
setImplementedInterfaces(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Reader)
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
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
CompileException
java.io.IOException
ClassBodyEvaluator()
,
setClassName(String)
,
setExtendedClass(Class)
,
setImplementedInterfaces(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Reader)
public ClassBodyEvaluator()
Method Detail |
---|
public void setDefaultImports(java.lang.String[] optionalDefaultImports)
IClassBodyEvaluator
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 });
setDefaultImports
in interface IClassBodyEvaluator
public void setClassName(java.lang.String className)
IClassBodyEvaluator
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.
setClassName
in interface IClassBodyEvaluator
public void setExtendedClass(java.lang.Class optionalExtendedType)
IClassBodyEvaluator
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.
setExtendedClass
in interface IClassBodyEvaluator
public void setExtendedType(java.lang.Class optionalExtendedClass)
IClassBodyEvaluator
IClassBodyEvaluator.setExtendedClass(Class)
instead.
setExtendedType
in interface IClassBodyEvaluator
public void setImplementedInterfaces(java.lang.Class[] implementedTypes)
IClassBodyEvaluator
setImplementedInterfaces
in interface IClassBodyEvaluator
public void setImplementedTypes(java.lang.Class[] implementedInterfaces)
IClassBodyEvaluator
IClassBodyEvaluator.setImplementedInterfaces(Class[])
instead.
setImplementedTypes
in interface IClassBodyEvaluator
public void cook(Scanner scanner) throws CompileException, java.io.IOException
cook
in class SimpleCompiler
CompileException
java.io.IOException
protected final Java.CompilationUnit makeCompilationUnit(Scanner optionalScanner) throws CompileException, java.io.IOException
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.
CompileException
java.io.IOException
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws CompileException
Java.CompilationUnit
, add
Java.ClassDeclaration
object
CompileException
protected final java.lang.Class compileToClass(Java.CompilationUnit compilationUnit, java.lang.String newClassName) throws CompileException
compilationUnit
- newClassName
- The fully qualified class name
CompileException
public java.lang.Class getClazz()
IClassBodyEvaluator
Class
.
This method must only be called after exactly one of the ICookable.cook(String,
java.io.Reader)
methods was called.
getClazz
in interface IClassBodyEvaluator
public java.lang.Object createInstance(java.io.Reader reader) throws CompileException, java.io.IOException
IClassBodyEvaluator
Reader
, then creates
and returns an instance of that class.
createInstance
in interface IClassBodyEvaluator
reader
- Source of class body tokens
optionalExtendedType
and implements the given
implementedTypes
CompileException
java.io.IOException
public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.Class optionalBaseType, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, java.io.IOException
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);
CompileException
java.io.IOException
createInstance(Reader)
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
createInstance(Reader)
instead:
IClassBodyEvaluator cbe =CompilerFactoryFactory
.getDefaultCompilerFactory
().newClassBodyEvaluator
(); cbe.setExtendedClass
(optionalExtendedClass); cbe.setImplementedInterfaces
(implementedInterfaces); cbe.setParentClassLoader
(optionalParentClassLoader); cbe.createInstance
(reader);
CompileException
java.io.IOException
createInstance(Reader)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |