|
||||||||||
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.commons.compiler.jdk.SimpleCompiler
org.codehaus.commons.compiler.jdk.ClassBodyEvaluator
public class ClassBodyEvaluator
To set up a ClassBodyEvaluator
object, proceed as described for IClassBodyEvaluator
. Alternatively, a number of "convenience constructors" exist that execute
the described steps instantly.
Notice that this implementation of IClassBodyEvaluator
is prone to "Java
injection", i.e. an application could get more than one class body compiled by passing a
bogus input document.
Also notice that the parsing of leading IMPORT declarations is heuristic and has certain
limitations; see parseImportDeclarations(Reader)
.
IClassBodyEvaluator
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()
|
Method Summary | |
---|---|
void |
cook(java.lang.String optionalFileName,
java.io.Reader r)
Reads, scans, parses and compiles Java tokens from the given Reader . |
protected void |
cook(java.lang.String optionalFileName,
java.lang.String[] imports,
java.io.Reader r)
|
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 static java.lang.String[] |
parseImportDeclarations(java.io.Reader r)
Heuristically parse IMPORT declarations at the beginning of the character stream produced by the given Reader . |
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.commons.compiler.jdk.SimpleCompiler |
---|
assertCooked, assertNotCooked, cook, getClassLoader, setDebuggingInformation, setParentClassLoader, setParentClassLoader |
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, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.codehaus.commons.compiler.ICookable |
---|
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setDebuggingInformation, setParentClassLoader |
Constructor Detail |
---|
public ClassBodyEvaluator()
Method Detail |
---|
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 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 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(java.lang.String optionalFileName, java.io.Reader r) throws CompileException, java.io.IOException
ICookable
Reader
.
cook
in interface ICookable
cook
in class SimpleCompiler
optionalFileName
- Used when reporting errors and warnings.
CompileException
java.io.IOException
protected void cook(java.lang.String optionalFileName, java.lang.String[] imports, java.io.Reader r) throws CompileException, java.io.IOException
imports
- E.g. "java.io.*" or "static java.util.Arrays.asList"r
- The class body to cook, without leading IMPORT declarations
CompileException
java.io.IOException
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
Class
created by the preceding call to Cookable.cook(Reader)
protected static java.lang.String[] parseImportDeclarations(java.io.Reader r) throws java.io.IOException
Reader
. After this method returns, all characters up to and including
that last IMPORT declaration have been read from the Reader
.
This method does not handle comments and string literals correctly, i.e. if a pattern that looks like an IMPORT declaration appears within a comment or a string literal, it will be taken as an IMPORT declaration.
r
- A Reader
that supports MARK, e.g. a BufferedReader
{ "java.util.*", "static java.util.Map.Entry" }
java.io.IOException
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
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |