Class ClassBodyEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.jdk.SimpleCompiler
-
- org.codehaus.commons.compiler.jdk.ClassBodyEvaluator
-
- All Implemented Interfaces:
IClassBodyEvaluator
,ICookable
,ISimpleCompiler
- Direct Known Subclasses:
ScriptEvaluator
public class ClassBodyEvaluator extends SimpleCompiler implements IClassBodyEvaluator
To set up aClassBodyEvaluator
object, proceed as described forIClassBodyEvaluator
. 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)
.- See Also:
IClassBodyEvaluator
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
className
private java.lang.Class<?>[]
implementedTypes
private static java.util.regex.Pattern
IMPORT_STATEMENT_PATTERN
private java.lang.String[]
optionalDefaultImports
private java.lang.Class<?>
optionalExtendedType
private java.lang.Class<?>
result
-
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
Constructors Constructor Description ClassBodyEvaluator()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
cook(java.lang.String optionalFileName, java.io.Reader r)
Reads, scans, parses and compiles Java tokens from the givenReader
.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 givenReader
, then creates and returns an instance of that class.java.lang.Class<?>
getClazz()
Returns the loadedClass
.protected static java.lang.String[]
parseImportDeclarations(java.io.Reader r)
Heuristically parse IMPORT declarations at the beginning of the character stream produced by the givenReader
.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, getClassLoader, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, 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, 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, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
-
-
-
-
Field Detail
-
optionalDefaultImports
private java.lang.String[] optionalDefaultImports
-
className
private java.lang.String className
-
optionalExtendedType
private java.lang.Class<?> optionalExtendedType
-
implementedTypes
private java.lang.Class<?>[] implementedTypes
-
result
private java.lang.Class<?> result
-
IMPORT_STATEMENT_PATTERN
private static final java.util.regex.Pattern IMPORT_STATEMENT_PATTERN
-
-
Method Detail
-
setClassName
public void setClassName(java.lang.String className)
Description copied from interface:IClassBodyEvaluator
Set the name of the generated class. Defaults toIClassBodyEvaluator.DEFAULT_CLASS_NAME
. In most cases, there is no need to set this name, because the generated class is loaded into its ownClassLoader
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 interfaceIClassBodyEvaluator
-
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 interfaceIClassBodyEvaluator
-
setExtendedClass
public void setExtendedClass(java.lang.Class optionalExtendedType)
Description copied from interface:IClassBodyEvaluator
Set a particular superclass that the generated class will extend. Ifnull
is passed, the generated class will extendObject
.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 interfaceIClassBodyEvaluator
-
setExtendedType
@Deprecated public void setExtendedType(java.lang.Class optionalExtendedClass)
Deprecated.- Specified by:
setExtendedType
in interfaceIClassBodyEvaluator
-
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 interfaceIClassBodyEvaluator
-
setImplementedTypes
@Deprecated public void setImplementedTypes(java.lang.Class[] implementedInterfaces)
Deprecated.- Specified by:
setImplementedTypes
in interfaceIClassBodyEvaluator
-
cook
public void cook(java.lang.String optionalFileName, java.io.Reader r) throws CompileException, java.io.IOException
Description copied from interface:ICookable
Reads, scans, parses and compiles Java tokens from the givenReader
.- Specified by:
cook
in interfaceICookable
- Overrides:
cook
in classSimpleCompiler
- Parameters:
optionalFileName
- Used when reporting errors and warnings.- Throws:
CompileException
java.io.IOException
- See Also:
ICookable.cook(String, Reader)
-
cook
protected void cook(java.lang.String optionalFileName, java.lang.String[] imports, java.io.Reader r) throws CompileException, java.io.IOException
- Parameters:
imports
- E.g. "java.io.*" or "static java.util.Arrays.asList"r
- The class body to cook, without leading IMPORT declarations- Throws:
CompileException
java.io.IOException
-
getClazz
public java.lang.Class<?> getClazz()
Description copied from interface:IClassBodyEvaluator
Returns the loadedClass
.This method must only be called after exactly one of the
ICookable.cook(String, java.io.Reader)
methods was called.- Specified by:
getClazz
in interfaceIClassBodyEvaluator
- Returns:
- The
Class
created by the preceding call toCookable.cook(Reader)
-
parseImportDeclarations
protected static java.lang.String[] parseImportDeclarations(java.io.Reader r) throws java.io.IOException
Heuristically parse IMPORT declarations at the beginning of the character stream produced by the givenReader
. After this method returns, all characters up to and including that last IMPORT declaration have been read from theReader
.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.
- Parameters:
r
- AReader
that supports MARK, e.g. aBufferedReader
- Returns:
- The parsed imports, e.g.
{ "java.util.*", "static java.util.Map.Entry" }
- Throws:
java.io.IOException
-
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 givenReader
, then creates and returns an instance of that class.- Specified by:
createInstance
in interfaceIClassBodyEvaluator
- Parameters:
reader
- Source of class body tokens- Returns:
- An object that extends the
optionalExtendedType
and implements the givenimplementedTypes
- Throws:
CompileException
java.io.IOException
-
-