Logo Search packages:      
Sourcecode: janino version File versions  Download package

org::codehaus::janino::ClassBodyEvaluator Class Reference

Inheritance diagram for org::codehaus::janino::ClassBodyEvaluator:

org::codehaus::janino::SimpleCompiler org::codehaus::janino::Cookable org::codehaus::janino::ScriptEvaluator org::codehaus::janino::ExpressionEvaluator

List of all members.


Detailed Description

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:

To set up a ClassBodyEvaluator object, proceed as follows:
  1. Create the ClassBodyEvaluator using ClassBodyEvaluator()
  2. Configure the ClassBodyEvaluator by calling any of the following methods:
  3. 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 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.

Definition at line 111 of file ClassBodyEvaluator.java.


Public Member Functions

 ClassBodyEvaluator (Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
 ClassBodyEvaluator (Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
 ClassBodyEvaluator (Scanner scanner, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
 ClassBodyEvaluator (String optionalFileName, Reader reader) throws CompileException, ParseException, ScanException, IOException
 ClassBodyEvaluator (String optionalFileName, InputStream is) throws CompileException, ParseException, ScanException, IOException
 ClassBodyEvaluator (String classBody) throws CompileException, ParseException, ScanException
final void cook (String s) throws CompileException, Parser.ParseException, Scanner.ScanException
final void cook (String optionalFileName, InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (InputStream is, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (String optionalFileName, InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (String optionalFileName, Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cook (Reader r) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
void cook (Scanner scanner) throws CompileException, ParseException, ScanException, IOException
final void cookFile (String fileName, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (String fileName) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (File file, String optionalEncoding) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
final void cookFile (File file) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException
boolean equals (Object o)
ClassLoader getClassLoader ()
Class getClazz ()
int hashCode ()
void setClassName (String className)
void setDefaultImports (String[] optionalDefaultImports)
void setExtendedType (Class optionalExtendedType)
void setImplementedTypes (Class[] implementedTypes)
void setParentClassLoader (ClassLoader optionalParentClassLoader, Class[] auxiliaryClasses)
void setParentClassLoader (ClassLoader optionalParentClassLoader)

Static Public Member Functions

static Object createFastClassBodyEvaluator (Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
static Object createFastClassBodyEvaluator (Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) throws CompileException, ParseException, ScanException, IOException
static void main (String[] args) throws Exception

Static Public Attributes

static final ClassLoader BOOT_CLASS_LOADER = new ClassLoader(null) {}
static final String DEFAULT_CLASS_NAME = "SC"

Protected Member Functions

Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration (Location location, Java.CompilationUnit compilationUnit) throws ParseException
Java.Type[] classesToTypes (Location location, Class[] classes)
Java.Type classToType (Location location, final Class optionalClass)
final Class compileToClass (Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation, String newClassName) throws CompileException
final ClassLoader compileToClassLoader (Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation) throws CompileException
final Java.CompilationUnit makeCompilationUnit (Scanner optionalScanner) throws ParseException, ScanException, IOException
final void setUpClassLoaders ()

Protected Attributes

String className = ClassBodyEvaluator.DEFAULT_CLASS_NAME

Static Protected Attributes

static final Class[] ZERO_CLASSES = new Class[0]

Private Attributes

Class[] implementedTypes = ClassBodyEvaluator.ZERO_CLASSES
String[] optionalDefaultImports = null
Class optionalExtendedType = null
Class result = null

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index