| java.lang.Object org.codehaus.janino.Cookable org.codehaus.janino.SimpleCompiler org.codehaus.janino.ClassBodyEvaluator
All known Subclasses: org.codehaus.janino.ScriptEvaluator,
ClassBodyEvaluator | public class ClassBodyEvaluator extends SimpleCompiler (Code) | | Parses a class body and returns it as a java.lang.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;
}
The optionalClassLoader serves two purposes:
- It is used to look for classes referenced by the class body.
- It is used to load the generated JavaTM class
into the JVM; directly if it is a subclass of
ByteArrayClassLoader , or by creation of a temporary
ByteArrayClassLoader if not.
To set up a
ClassBodyEvaluator object, proceed as follows:
-
Create the
ClassBodyEvaluator using
ClassBodyEvaluator.ClassBodyEvaluator()
-
Configure the
ClassBodyEvaluator by calling any of the following methods:
-
Call any of the
org.codehaus.janino.Cookable.cook(Scanner) methods to scan,
parse, compile and load the class body into the JVM.
Alternatively, a number of "convenience constructors" exist that execute the steps described
above instantly.
To compile a class body and immediately instantiate an object, one of the
ClassBodyEvaluator.createFastClassBodyEvaluator(Scanner,Class,ClassLoader) 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.
|
Constructor Summary | |
public | ClassBodyEvaluator(String classBody) | public | ClassBodyEvaluator(String optionalFileName, InputStream is) | public | ClassBodyEvaluator(String optionalFileName, Reader reader) | public | ClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader) | public | ClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) | public | ClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) | public | ClassBodyEvaluator() |
Method Summary | |
protected Java.PackageMemberClassDeclaration | addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) | 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. | public 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 . | public 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. | public Class | getClazz() Returns the loaded
Class . | protected void | internalCook(Scanner scanner) | protected Java.CompilationUnit | makeCompilationUnit(Scanner scanner) Create a
Java.CompilationUnit , set the default imports, and parse the import
declarations. | public void | setClassName(String className) Set the name of the generated class. | public void | setDefaultImports(String[] optionalDefaultImports) "Default imports" add to the system import "java.lang", i.e. | public void | setExtendedType(Class optionalExtendedType) Set a particular superclass that the generated class will extend. | public void | setImplementedTypes(Class[] implementedTypes) Set a particular set of interfaces that the generated class will implement. |
DEFAULT_CLASS_NAME | final public static String DEFAULT_CLASS_NAME(Code) | | |
ZERO_CLASSES | final protected static Class[] ZERO_CLASSES(Code) | | |
ClassBodyEvaluator | public ClassBodyEvaluator()(Code) | | |
compileToClass | protected Class compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName) throws CompileException(Code) | | Compile the given compilation unit, load all generated classes, and
return the class with the given name.
Parameters: compilationUnit - Parameters: debuggingInformation - TODO Parameters: newClassName - The fully qualified class name The loaded class |
createFastClassBodyEvaluator | public static Object createFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code) | | Scans, parses and compiles a class body from the tokens delivered by the the given
Scanner .
The generated class has the
ClassBodyEvaluator.DEFAULT_CLASS_NAME and extends the given
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 Parameters: optionalBaseType - Base type to extend/implement Parameters: optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader" an object that extends/implements the given optionalBaseType See Also: ClassBodyEvaluator |
createFastClassBodyEvaluator | public static Object createFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code) | | Scans, parses and compiles a class body from the tokens delivered by the the given
Scanner with no default imports.
For an explanation of the "fast class body evaluator" concept, see the class description.
Parameters: scanner - Source of class body tokens Parameters: className - Name of generated class Parameters: optionalExtendedType - Class to extend Parameters: implementedTypes - Interfaces to implement Parameters: optionalParentClassLoader - Used to load referenced classes, defaults to the current thread's "context class loader" an object that extends the optionalExtendedType and implements the given implementedTypes See Also: ClassBodyEvaluator |
setClassName | public void setClassName(String className)(Code) | | Set the name of the generated class. Defaults to
ClassBodyEvaluator.DEFAULT_CLASS_NAME . In most cases,
there is no need to set this name, because the generated class is loaded into its own
java.lang.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.
|
setDefaultImports | public void setDefaultImports(String[] optionalDefaultImports)(Code) | | "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.
Example: sc.setDefaultImports(new String[] { "java.util.Map", "java.io.*" });
|
setExtendedType | public void setExtendedType(Class optionalExtendedType)(Code) | | Set a particular superclass that the generated class will extend. If 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.
|
setImplementedTypes | public void setImplementedTypes(Class[] implementedTypes)(Code) | | Set a particular set of interfaces that the generated class will implement.
|
Methods inherited from org.codehaus.janino.Cookable | final public void cook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String optionalFileName, InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cook(String s) throws CompileException, Parser.ParseException, Scanner.ScanException(Code)(Java Doc) final public void cookFile(File file) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(File file, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(String fileName) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) final public void cookFile(String fileName, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc) abstract protected void internalCook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc)
|
|
|