|
Web Site | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.janino.Cookable
org.codehaus.janino.SimpleCompiler
org.codehaus.janino.ClassBodyEvaluator
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:
ByteArrayClassLoader
, or by creation of a temporary
ByteArrayClassLoader
if not.
ClassBodyEvaluator
object, proceed as follows:
ClassBodyEvaluator
using ClassBodyEvaluator()
ClassBodyEvaluator
by calling any of the following methods:
Cookable.cook(Scanner)
methods to scan,
parse, compile and load the class body into the JVM.
To compile a class body and immediately instantiate an object, one of the
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.
Field Summary | |
protected java.lang.String |
className
|
static java.lang.String |
DEFAULT_CLASS_NAME
|
protected static java.lang.Class[] |
ZERO_CLASSES
|
Fields inherited from class org.codehaus.janino.SimpleCompiler |
BOOT_CLASS_LOADER |
Constructor Summary | |
ClassBodyEvaluator()
|
|
ClassBodyEvaluator(Scanner scanner,
java.lang.Class optionalExtendedType,
java.lang.Class[] implementedTypes,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
|
ClassBodyEvaluator(Scanner scanner,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
|
ClassBodyEvaluator(Scanner scanner,
java.lang.String className,
java.lang.Class optionalExtendedType,
java.lang.Class[] implementedTypes,
java.lang.ClassLoader optionalParentClassLoader)
Equivalent to |
|
ClassBodyEvaluator(java.lang.String classBody)
Equivalent to |
|
ClassBodyEvaluator(java.lang.String optionalFileName,
java.io.InputStream is)
Equivalent to |
|
ClassBodyEvaluator(java.lang.String optionalFileName,
java.io.Reader reader)
Equivalent to |
Method Summary | |
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 java.lang.Class |
compileToClass(Java.CompilationUnit compilationUnit,
EnumeratorSet debuggingInformation,
java.lang.String newClassName)
Compile the given compilation unit, load all generated classes, and return the class with the given name. |
void |
cook(Scanner scanner)
To be implemented by the derived classes. |
static java.lang.Object |
createFastClassBodyEvaluator(Scanner scanner,
java.lang.Class optionalBaseType,
java.lang.ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner . |
static java.lang.Object |
createFastClassBodyEvaluator(Scanner scanner,
java.lang.String className,
java.lang.Class optionalExtendedType,
java.lang.Class[] implementedTypes,
java.lang.ClassLoader optionalParentClassLoader)
Scans, parses and compiles a class body from the tokens delivered by the the given Scanner with no default imports. |
java.lang.Class |
getClazz()
Returns the loaded Class . |
protected Java.CompilationUnit |
makeCompilationUnit(Scanner optionalScanner)
Create a Java.CompilationUnit , set the default imports, and parse the import
declarations. |
void |
setClassName(java.lang.String className)
Set the name of the generated class. |
void |
setDefaultImports(java.lang.String[] optionalDefaultImports)
"Default imports" add to the system import "java.lang", i.e. |
void |
setExtendedType(java.lang.Class optionalExtendedType)
Set a particular superclass that the generated class will extend. |
void |
setImplementedTypes(java.lang.Class[] implementedTypes)
Set a particular set of interfaces that the generated class will implement. |
Methods inherited from class org.codehaus.janino.SimpleCompiler |
classesToTypes, classToType, compileToClassLoader, equals, getClassLoader, hashCode, main, setParentClassLoader, setParentClassLoader, setUpClassLoaders |
Methods inherited from class org.codehaus.janino.Cookable |
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String DEFAULT_CLASS_NAME
protected static final java.lang.Class[] ZERO_CLASSES
protected java.lang.String className
Constructor Detail |
public ClassBodyEvaluator(java.lang.String classBody) throws CompileException, Parser.ParseException, Scanner.ScanException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
ClassBodyEvaluator()
,
Cookable.cook(String)
public ClassBodyEvaluator(java.lang.String optionalFileName, java.io.InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, is);
ClassBodyEvaluator()
,
Cookable.cook(String, InputStream)
public ClassBodyEvaluator(java.lang.String optionalFileName, java.io.Reader reader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, reader);
ClassBodyEvaluator()
,
Cookable.cook(String, Reader)
public ClassBodyEvaluator(Scanner scanner, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator()
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Scanner)
public ClassBodyEvaluator(Scanner scanner, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator()
,
setExtendedType(Class)
,
setImplementedTypes(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Scanner)
public ClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
ClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
ClassBodyEvaluator()
,
setClassName(String)
,
setExtendedType(Class)
,
setImplementedTypes(Class[])
,
SimpleCompiler.setParentClassLoader(ClassLoader)
,
Cookable.cook(Scanner)
public ClassBodyEvaluator()
Method Detail |
public void setDefaultImports(java.lang.String[] optionalDefaultImports)
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 });
public void setClassName(java.lang.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.
public void setExtendedType(java.lang.Class optionalExtendedType)
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.
public void setImplementedTypes(java.lang.Class[] implementedTypes)
public void cook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
Cookable
cook
in class SimpleCompiler
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
protected final Java.CompilationUnit makeCompilationUnit(Scanner optionalScanner) throws Parser.ParseException, Scanner.ScanException, java.io.IOException
Java.CompilationUnit
, set the default imports, and parse the import
declarations.
If the optionalScanner
is given, a sequence of IMPORT directives is parsed
from it and added to the compilation unit.
Parser.ParseException
Scanner.ScanException
java.io.IOException
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws Parser.ParseException
Java.CompilationUnit
, add
Java.ClassDeclaration
object
Parser.ParseException
protected final java.lang.Class compileToClass(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, java.lang.String newClassName) throws CompileException
compilationUnit
- debuggingInformation
- TODOnewClassName
- The fully qualified class name
CompileException
public java.lang.Class getClazz()
Class
.
This method must only be called after cook(Scanner)
.
This method must not be called for instances of derived classes.
public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.Class optionalBaseType, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
Scanner
.
The generated class has the 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.
scanner
- Source of class body tokensoptionalBaseType
- Base type to extend/implementoptionalParentClassLoader
- Used to load referenced classes, defaults to the current thread's "context class loader"
optionalBaseType
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
ClassBodyEvaluator
public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class optionalExtendedType, java.lang.Class[] implementedTypes, java.lang.ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, java.io.IOException
Scanner
with no default imports.
For an explanation of the "fast class body evaluator" concept, see the class description.
scanner
- Source of class body tokensclassName
- Name of generated classoptionalExtendedType
- Class to extendimplementedTypes
- Interfaces to implementoptionalParentClassLoader
- Used to load referenced classes, defaults to the current thread's "context class loader"
optionalExtendedType
and implements the given implementedTypes
CompileException
Parser.ParseException
Scanner.ScanException
java.io.IOException
ClassBodyEvaluator
|
Web Site | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |