org.codehaus.janino
public class ClassBodyEvaluator extends SimpleCompiler
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; }
The optionalClassLoader
serves two purposes:
To compile a class body and immediately instantiate an object, one of the ClassBodyEvaluator methods can be used.
The generated class may optionally extend/implement a given type; the returned instance can
safely be type-casted to that optionalBaseType
.
Example:
public interface Foo { int bar(int a, int b); } ... 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));Notice: The
optionalBaseType
must be accessible from the generated class,
i.e. it must either be declared public
, or with default accessibility in the
same package as the generated class.
Field Summary | |
---|---|
protected String | className |
static String | DEFAULT_CLASS_NAME |
protected static Class[] | ZERO_CLASSES |
Constructor Summary | |
---|---|
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); | |
ClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader)
Equivalent to ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner); | |
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, 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() |
Method Summary | |
---|---|
protected Java.PackageMemberClassDeclaration | addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit)
To the given CompilationUnit, add
|
protected Class | compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName)
Compile the given compilation unit, load all generated classes, and
return the class with the given name. |
void | cook(Scanner scanner) |
static Object | createFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given
Scanner.
|
static Object | createFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given
Scanner with no default imports.
|
Class | getClazz()
Returns the loaded Class.
|
protected Java.CompilationUnit | makeCompilationUnit(Scanner optionalScanner)
Create a CompilationUnit, set the default imports, and parse the import
declarations.
|
void | setClassName(String className)
Set 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 | setExtendedType(Class optionalExtendedType)
Set a particular superclass that the generated class will extend. |
void | setImplementedTypes(Class[] implementedTypes)
Set a particular set of interfaces that the generated class will implement. |
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
See Also: ClassBodyEvaluator cook
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, is);
See Also: ClassBodyEvaluator Cookable
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, reader);
See Also: ClassBodyEvaluator Cookable
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
See Also: ClassBodyEvaluator setParentClassLoader cook
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
See Also: ClassBodyEvaluator setExtendedType (Class[])
setParentClassLoader cook
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
See Also: ClassBodyEvaluator setClassName setExtendedType (Class[])
setParentClassLoader cook
Returns: The created ClassDeclaration object
Parameters: compilationUnit debuggingInformation TODO newClassName The fully qualified class name
Returns: The loaded class
optionalBaseType
(if that is a class), and implements the given
optionalBaseType
(if that is an interface).
For an explanation of the "fast class body evaluator" concept, see the class description.
Parameters: scanner Source of class body tokens optionalBaseType Base type to extend/implement optionalParentClassLoader Used to load referenced classes, defaults to the current thread's "context class loader"
Returns: an object that extends/implements the given optionalBaseType
See Also: ClassBodyEvaluator
For an explanation of the "fast class body evaluator" concept, see the class description.
Parameters: scanner Source of class body tokens className Name of generated class optionalExtendedType Class to extend implementedTypes Interfaces to implement optionalParentClassLoader Used to load referenced classes, defaults to the current thread's "context class loader"
Returns: an object that extends the optionalExtendedType
and implements the given implementedTypes
See Also: ClassBodyEvaluator
This method must only be called after cook.
This method must not be called for instances of derived classes.
If the optionalScanner
is given, a sequence of IMPORT directives is parsed
from it and added to the compilation unit.
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.
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 });
null
is
passed, the generated class will extend Object.
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.