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[])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 baseType.
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 |
|---|---|
| public static final String | DEFAULT_CLASS_NAME
Default name for the generated class. |
| Modifier and Type | Method and Description |
|---|---|
| public Object | Returns: An object that extends theextendedType and implements the given
implementedTypesSource of class body tokens reader)Scans, parses and compiles a class body from the tokens delivered by the the given |
| public Class | |
| public String[] | Returns: The default imports that were previously configured withsetDefaultImports(String.
|
| public void | |
| public void | setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)Installs an |
| public void | setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes. |
| public void | setDefaultImports(String... defaultImports)
"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. |
| public void | setExtendedClass(Class<?> extendedClass)
Sets a particular superclass that the generated class will extend. |
| public void | |
| public void | setImplementedInterfaces(Class<?>[] implementedInterfaces)
Sets a particular set of interfaces that the generated class will implement. |
| public void | setImplementedTypes(Class<?>[] implementedInterfaces)
Deprecated
Use
setImplementedInterfaces(Class[]) instead
|
| public void | setParentClassLoader(ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes. |
| public void | setWarningHandler(WarningHandler
null to indicate that no warnings be issuedBy default, warnings are discarded, but an application my install a custom |
| DEFAULT_CLASS_NAME | back to summary |
|---|---|
| public static final String DEFAULT_CLASS_NAME Default name for the generated class. | |
| createInstance | back to summary |
|---|---|
| public Object createInstance(Reader reader) throws CompileException, IOException Scans, parses and compiles a class body from the tokens delivered by the the given | |
| getClazz | back to summary |
|---|---|
public Class
| |
| getDefaultImports | back to summary |
|---|---|
public String[] getDefaultImports()
| |
| setClassName | back to summary |
|---|---|
| public void setClassName(String className) Sets the name of the generated class. Defaults to 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. | |
| setCompileErrorHandler | back to summary |
|---|---|
| public void setCompileErrorHandler(ErrorHandler compileErrorHandler) Installs an
If the given
If the given
In other words: The
| |
| setDebuggingInformation | back to summary |
|---|---|
| public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Determines what kind of debugging information is included in the generates classes. The default is typically
" | |
| setDefaultImports | back to summary |
|---|---|
| public void setDefaultImports(String... defaultImports) "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 here:
sc.setDefaultImports(
"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
);
| |
| setExtendedClass | back to summary |
|---|---|
| public void setExtendedClass(Class<?> extendedClass) Sets a particular superclass that the generated class will extend. If extendedClass is 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. | |
| setExtendedType | back to summary |
|---|---|
| public void setExtendedType(Class<?> extendedClass)
Deprecated Use
| |
| setImplementedInterfaces | back to summary |
|---|---|
| public void setImplementedInterfaces(Class<?>[] implementedInterfaces) Sets a particular set of interfaces that the generated class will implement. | |
| setImplementedTypes | back to summary |
|---|---|
| public void setImplementedTypes(Class<?>[] implementedInterfaces)
Deprecated Use
| |
| setParentClassLoader | back to summary | ||||||
|---|---|---|---|---|---|---|---|
| public void setParentClassLoader(ClassLoader parentClassLoader) The "parent class loader" is used to load referenced classes. Useful values are:
The parent class loader defaults to the current thread's context class loader. | |||||||
| setWarningHandler | back to summary |
|---|---|
| public void setWarningHandler(WarningHandler warningHandler) By default, warnings are discarded, but an application my install a custom
| |