public class ClassBodyEvaluator extends SimpleCompiler implements IClassBodyEvaluator
optionalClassLoader serves two purposes:
ByteArrayClassLoader, or by creation of a temporary
ByteArrayClassLoader if not.
A number of "convenience constructors" exist that execute the setup steps instantly.
DEFAULT_CLASS_NAMEBOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE| Constructor and Description |
|---|
ClassBodyEvaluator() |
ClassBodyEvaluator(Scanner scanner,
Class<?> optionalExtendedType,
Class<?>[] implementedTypes,
ClassLoader optionalParentClassLoader)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setExtendedType(optionalExtendedType);
cbe.setImplementedTypes(implementedTypes);
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
|
ClassBodyEvaluator(Scanner scanner,
ClassLoader optionalParentClassLoader)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
|
ClassBodyEvaluator(Scanner scanner,
String className,
Class<?> optionalExtendedType,
Class<?>[] implementedTypes,
ClassLoader optionalParentClassLoader)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setClassName(className);
cbe.setExtendedType(optionalExtendedType);
cbe.setImplementedTypes(implementedTypes);
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
|
ClassBodyEvaluator(String classBody)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(classBody);
|
ClassBodyEvaluator(String optionalFileName,
InputStream is)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(optionalFileName, is);
|
ClassBodyEvaluator(String optionalFileName,
Reader reader)
Equivalent to
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(optionalFileName, reader);
|
| Modifier and Type | Method and Description |
|---|---|
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 Class<?> |
compileToClass(Java.CompilationUnit compilationUnit)
Compiles the given compilation unit, load all generated classes, and return the class with the given name.
|
void |
cook(Scanner scanner)
Scans, parses and ompiles a given compilation unit from the given scanner.
|
static Object |
createFastClassBodyEvaluator(Scanner scanner,
Class<?> optionalBaseType,
ClassLoader optionalParentClassLoader)
Use
createInstance(Reader) instead:
IClassBodyEvaluator cbe = CompilerFactoryFactory. |
static Object |
createFastClassBodyEvaluator(Scanner scanner,
String className,
Class<?> optionalExtendedClass,
Class<?>[] implementedInterfaces,
ClassLoader optionalParentClassLoader)
Deprecated.
Use
createInstance(Reader) instead |
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 Java.CompilationUnit |
makeCompilationUnit(Parser optionalParser)
Creates a
Java.CompilationUnit, set the default imports, and parse the import declarations. |
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, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, optionalClassToType, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandlercook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readStringclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitcook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandlerpublic ClassBodyEvaluator(String classBody) throws CompileException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(classBody);
CompileExceptionClassBodyEvaluator(),
Cookable.cook(String)public ClassBodyEvaluator(@Nullable String optionalFileName, InputStream is) throws CompileException, IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(optionalFileName, is);
CompileExceptionIOExceptionClassBodyEvaluator(),
Cookable.cook(String, InputStream)public ClassBodyEvaluator(@Nullable String optionalFileName, Reader reader) throws CompileException, IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.cook(optionalFileName, reader);
CompileExceptionIOExceptionClassBodyEvaluator(),
ICookable.cook(String, Reader)public ClassBodyEvaluator(Scanner scanner, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
public ClassBodyEvaluator(Scanner scanner, @Nullable Class<?> optionalExtendedType, Class<?>[] implementedTypes, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setExtendedType(optionalExtendedType);
cbe.setImplementedTypes(implementedTypes);
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
public ClassBodyEvaluator(Scanner scanner, String className, @Nullable Class<?> optionalExtendedType, Class<?>[] implementedTypes, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator();
cbe.setClassName(className);
cbe.setExtendedType(optionalExtendedType);
cbe.setImplementedTypes(implementedTypes);
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.cook(scanner);
public ClassBodyEvaluator()
public 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 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 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(Scanner scanner) throws CompileException, IOException
SimpleCompilerSimpleCompiler.getClassLoader() returns a ClassLoader that allows for access to the compiled classes.cook in class SimpleCompilerCompileExceptionIOExceptionprotected final Java.CompilationUnit makeCompilationUnit(@Nullable Parser optionalParser) throws CompileException, IOException
Java.CompilationUnit, set the default imports, and parse the import declarations.
If the optionalParser is given, a sequence of IMPORT directives is parsed from it and added to the
compilation unit.
CompileExceptionIOExceptionprotected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws CompileException
Java.CompilationUnit, add
Java.AbstractClassDeclaration objectCompileExceptionprotected final Class<?> compileToClass(Java.CompilationUnit compilationUnit) throws CompileException
compilationUnit - CompileExceptionpublic 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 IClassBodyEvaluatorpublic 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
implementedTypesCompileExceptionIOExceptionpublic static Object createFastClassBodyEvaluator(Scanner scanner, @Nullable Class<?> optionalBaseType, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, 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);
CompileExceptionIOExceptioncreateInstance(Reader)@Deprecated public static Object createFastClassBodyEvaluator(Scanner scanner, String className, @Nullable Class<?> optionalExtendedClass, Class<?>[] implementedInterfaces, @Nullable ClassLoader optionalParentClassLoader) throws CompileException, IOException
createInstance(Reader) insteadcreateInstance(Reader) instead:
IClassBodyEvaluator cbe = CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator();
cbe.setExtendedClass(optionalExtendedClass);
cbe.setImplementedInterfaces(implementedInterfaces);
cbe.setParentClassLoader(optionalParentClassLoader);
cbe.createInstance(reader);
CompileExceptionIOExceptioncreateInstance(Reader)