Class ClassBodyEvaluator
- All Implemented Interfaces:
IClassBodyEvaluator,ICookable,ISimpleCompiler
- Direct Known Subclasses:
ScriptEvaluator
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).
- See Also:
-
Field Summary
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAMEFields inherited from interface org.codehaus.commons.compiler.ICookable
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidReads, scans, parses and compiles Java tokens from the givenReader.protected voidcreateInstance(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.Class<?> getClazz()Returns the loadedClass.protected static String[]Heuristically parse IMPORT declarations at the beginning of the character stream produced by the givenReader.voidsetClassName(String className) Set the name of the generated class.voidsetDefaultImports(String[] optionalDefaultImports) "Default imports" add to the system import "java.lang", i.e.voidsetExtendedClass(Class optionalExtendedType) Set a particular superclass that the generated class will extend.voidsetExtendedType(Class optionalExtendedClass) Deprecated.voidsetImplementedInterfaces(Class[] implementedTypes) Set a particular set of interfaces that the generated class will implement.voidsetImplementedTypes(Class[] implementedInterfaces) Deprecated.Methods inherited from class org.codehaus.commons.compiler.jdk.SimpleCompiler
assertCooked, assertNotCooked, cook, getClassLoader, setDebuggingInformation, setParentClassLoader, setParentClassLoaderMethods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readString
-
Constructor Details
-
ClassBodyEvaluator
public ClassBodyEvaluator()
-
-
Method Details
-
setClassName
Description copied from interface:IClassBodyEvaluatorSet 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 ownClassLoaderwhere 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:
setClassNamein interfaceIClassBodyEvaluator
-
setDefaultImports
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:
setDefaultImportsin interfaceIClassBodyEvaluator
-
setExtendedClass
Description copied from interface:IClassBodyEvaluatorSet a particular superclass that the generated class will extend. Ifnullis 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:
setExtendedClassin interfaceIClassBodyEvaluator
-
setExtendedType
Deprecated.- Specified by:
setExtendedTypein interfaceIClassBodyEvaluator
-
setImplementedInterfaces
Description copied from interface:IClassBodyEvaluatorSet a particular set of interfaces that the generated class will implement.- Specified by:
setImplementedInterfacesin interfaceIClassBodyEvaluator
-
setImplementedTypes
Deprecated.- Specified by:
setImplementedTypesin interfaceIClassBodyEvaluator
-
cook
Description copied from interface:ICookableReads, scans, parses and compiles Java tokens from the givenReader.- Specified by:
cookin interfaceICookable- Overrides:
cookin classSimpleCompiler- Parameters:
optionalFileName- Used when reporting errors and warnings.- Throws:
CompileExceptionIOException- See Also:
-
cook
protected void cook(String optionalFileName, String[] imports, Reader r) throws CompileException, IOException - Parameters:
imports- E.g. "java.io.*" or "static java.util.Arrays.asList"r- The class body to cook, without leading IMPORT declarations- Throws:
CompileExceptionIOException
-
getClazz
Description copied from interface:IClassBodyEvaluatorReturns the loadedClass.This method must only be called after exactly one of the
ICookable.cook(String, java.io.Reader)methods was called.- Specified by:
getClazzin interfaceIClassBodyEvaluator- Returns:
- The
Classcreated by the preceding call toCookable.cook(Reader)
-
parseImportDeclarations
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- AReaderthat supports MARK, e.g. aBufferedReader- Returns:
- The parsed imports, e.g.
{ "java.util.*", "static java.util.Map.Entry" } - Throws:
IOException
-
createInstance
Description copied from interface:IClassBodyEvaluatorScans, 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:
createInstancein interfaceIClassBodyEvaluator- Parameters:
reader- Source of class body tokens- Returns:
- An object that extends the
optionalExtendedTypeand implements the givenimplementedTypes - Throws:
CompileExceptionIOException
-