public interface IClassBodyEvaluator extends ICookable
Class
object ready for use with
java.lang.reflect.
Example:
import java.util.*; static private int a = 1; private int b = 2; public void func(int c, int d) { return func2(c, d); } private static void func2(int e, int f) { return e * f; }
To set up an IClassBodyEvaluator
object, proceed as follows:
IClassBodyEvaluator
-implementing class.IClassBodyEvaluator
by calling any of the following methods:setDefaultImports(String[])
setClassName(String)
setExtendedClass(Class)
setImplementedInterfaces(Class[])
ICookable.setParentClassLoader(ClassLoader)
ICookable.cook(String, java.io.Reader)
methods to scan, parse, compile and load the
class body into the JVM.
To compile a class body and immediately instantiate an object, the createInstance(Reader)
methods can be
used.
The generated class may optionally extend/implement a given type; the returned instance can safely be type-cast
to that optionalBaseType
.
Example:
public interface Foo { int bar(int a, int b); } ...IClassBodyEvaluator
cbe =CompilerFactoryFactory
.getDefaultCompilerFactory
().newClassBodyEvaluator
(); Foo f = (Foo) ClassBodyEvaluator.createFastClassBodyEvaluator( new Scanner(null, new StringReader("public int bar(int a, int b) { return a + b; }")), Foo.class, // Base type to extend/implement (ClassLoader) null // Use current thread's context class loader ); System.out.println("1 + 2 = " + f.bar(1, 2));
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_CLASS_NAME
Default name for the generated class.
|
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE
Modifier and Type | Method and Description |
---|---|
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 . |
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<?> optionalExtendedClass)
Sets a particular superclass that the generated class will extend.
|
void |
setExtendedType(Class<?> optionalExtendedClass)
Deprecated.
Use
setExtendedClass(Class) instead |
void |
setImplementedInterfaces(Class<?>[] implementedInterfaces)
Sets a particular set of interfaces that the generated class will implement.
|
void |
setImplementedTypes(Class<?>[] implementedInterfaces)
Deprecated.
Use
setImplementedInterfaces(Class[]) instead |
cook, cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, setCompileErrorHandler, setDebuggingInformation, setParentClassLoader, setWarningHandler
static final String DEFAULT_CLASS_NAME
void setDefaultImports(@Nullable String[] optionalDefaultImports)
Notice 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 });
void setClassName(String className)
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.
void setExtendedClass(@Nullable Class<?> optionalExtendedClass)
null
, 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.
@Deprecated void setExtendedType(@Nullable Class<?> optionalExtendedClass)
setExtendedClass(Class)
insteadvoid setImplementedInterfaces(Class<?>[] implementedInterfaces)
@Deprecated void setImplementedTypes(Class<?>[] implementedInterfaces)
setImplementedInterfaces(Class[])
insteadClass<?> getClazz()
Class
.
This method must only be called after exactly one of the ICookable.cook(String, java.io.Reader)
methods was
called.
Object createInstance(Reader reader) throws CompileException, IOException
Reader
, then creates
and returns an instance of that class.reader
- Source of class body tokensoptionalExtendedType
and implements the given
implementedTypes
CompileException
IOException
Copyright © 2016. All rights reserved.