public class ClassBodyEvaluator extends SimpleCompiler implements IClassBodyEvaluator
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).
IClassBodyEvaluatorDEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ClassBodyEvaluator() |
| Modifier and Type | Method and Description |
|---|---|
void |
cook(String optionalFileName,
Reader r)
Reads, scans, parses and compiles Java tokens from the given
Reader. |
protected void |
cook(String optionalFileName,
String[] imports,
Reader r) |
Object |
createInstance(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. |
Class<?> |
getClazz()
Returns the loaded
Class. |
protected static String[] |
parseImportDeclarations(Reader r)
Heuristically parses IMPORT declarations at the beginning of the character stream produced by the given
Reader. |
void |
setClassName(String className)
Sets the name of the generated class.
|
void |
setDefaultImports(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(Class<?> optionalExtendedType)
Sets a particular superclass that the generated class will extend.
|
void |
setExtendedType(Class<?> optionalExtendedClass)
Deprecated.
Use
setExtendedClass(Class) instead |
void |
setImplementedInterfaces(Class<?>[] implementedTypes)
Sets a particular set of interfaces that the generated class will implement.
|
void |
setImplementedTypes(Class<?>[] implementedInterfaces)
Deprecated.
Use
setImplementedInterfaces(Class[]) instead |
assertNotCooked, getClassLoader, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setParentClassLoader, setWarningHandlercook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandlerpublic void setClassName(String className)
IClassBodyEvaluatorIClassBodyEvaluator.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 IClassBodyEvaluatorpublic void setDefaultImports(@Nullable String[] optionalDefaultImports)
IClassBodyEvaluatorNotice that JDK 5 "static imports" are also supported, as shown here:
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 IClassBodyEvaluatorpublic void setExtendedClass(@Nullable Class<?> optionalExtendedType)
IClassBodyEvaluatornull, the generated class will extend Object.
The usual reason to set a base class for an evaluator is that the generated class can directly access the superclass's (non-private) members.
setExtendedClass in interface IClassBodyEvaluator@Deprecated public void setExtendedType(@Nullable Class<?> optionalExtendedClass)
setExtendedClass(Class) insteadsetExtendedType in interface IClassBodyEvaluatorpublic void setImplementedInterfaces(Class<?>[] implementedTypes)
IClassBodyEvaluatorsetImplementedInterfaces in interface IClassBodyEvaluator@Deprecated public void setImplementedTypes(Class<?>[] implementedInterfaces)
setImplementedInterfaces(Class[]) insteadsetImplementedTypes in interface IClassBodyEvaluatorpublic void cook(@Nullable String optionalFileName, Reader r) throws CompileException, IOException
ICookableReader.cook in interface ICookablecook in class SimpleCompileroptionalFileName - Used when reporting errors and warningsCompileExceptionIOExceptionprotected void cook(@Nullable String optionalFileName, String[] imports, Reader r) throws CompileException, IOException
imports - E.g. "java.io.*" or "static java.util.Arrays.asList"r - The class body to cook, without leading IMPORT declarationsCompileExceptionIOExceptionpublic Class<?> getClazz()
IClassBodyEvaluatorClass.
This method must only be called after exactly one of the ICookable.cook(String, java.io.Reader) methods was
called.
getClazz in interface IClassBodyEvaluatorClass created by the preceding call to Cookable.cook(Reader)protected static String[] parseImportDeclarations(Reader r) throws 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" }IOExceptionpublic Object createInstance(Reader reader) throws CompileException, IOException
IClassBodyEvaluatorReader, then creates
and returns an instance of that class.createInstance in interface IClassBodyEvaluatorreader - Source of class body tokensoptionalExtendedType and implements the given
implementedTypesCompileExceptionIOException