Java Doc for ClassBodyEvaluator.java in  » Scripting » jacl » org » codehaus » janino » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » jacl » org.codehaus.janino 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


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:
  1. Create the ClassBodyEvaluator using ClassBodyEvaluator.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 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.


Field Summary
final public static  StringDEFAULT_CLASS_NAME
    
final protected static  Class[]ZERO_CLASSES
    
protected  StringclassName
    

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.PackageMemberClassDeclarationaddPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit)
    
protected  ClasscompileToClass(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  ObjectcreateFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader)
     Scans, parses and compiles a class body from the tokens delivered by the the given Scanner .
public static  ObjectcreateFastClassBodyEvaluator(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  ClassgetClazz()
     Returns the loaded Class .
protected  voidinternalCook(Scanner scanner)
    
protected  Java.CompilationUnitmakeCompilationUnit(Scanner scanner)
     Create a Java.CompilationUnit , set the default imports, and parse the import declarations.
public  voidsetClassName(String className)
     Set the name of the generated class.
public  voidsetDefaultImports(String[] optionalDefaultImports)
     "Default imports" add to the system import "java.lang", i.e.
public  voidsetExtendedType(Class optionalExtendedType)
     Set a particular superclass that the generated class will extend.
public  voidsetImplementedTypes(Class[] implementedTypes)
     Set a particular set of interfaces that the generated class will implement.

Field Detail
DEFAULT_CLASS_NAME
final public static String DEFAULT_CLASS_NAME(Code)



ZERO_CLASSES
final protected static Class[] ZERO_CLASSES(Code)



className
protected String className(Code)




Constructor Detail
ClassBodyEvaluator
public ClassBodyEvaluator(String classBody) throws CompileException, Parser.ParseException, Scanner.ScanException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(classBody);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   Cookable.cook(String)



ClassBodyEvaluator
public ClassBodyEvaluator(String optionalFileName, InputStream is) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, is);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   Cookable.cook(StringInputStream)



ClassBodyEvaluator
public ClassBodyEvaluator(String optionalFileName, Reader reader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.cook(optionalFileName, reader);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   Cookable.cook(StringReader)



ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   SimpleCompiler.setParentClassLoader(ClassLoader)
See Also:   Cookable.cook(Scanner)



ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   ClassBodyEvaluator.setExtendedType(Class)
See Also:   ClassBodyEvaluator.setImplementedTypes(Class[])
See Also:   SimpleCompiler.setParentClassLoader(ClassLoader)
See Also:   Cookable.cook(Scanner)



ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)
Equivalent to
 ClassBodyEvaluator cbe = new ClassBodyEvaluator();
 cbe.setClassName(className);
 cbe.setExtendedType(optionalExtendedType);
 cbe.setImplementedTypes(implementedTypes);
 cbe.setParentClassLoader(optionalParentClassLoader);
 cbe.cook(scanner);

See Also:   ClassBodyEvaluator.ClassBodyEvaluator()
See Also:   ClassBodyEvaluator.setClassName(String)
See Also:   ClassBodyEvaluator.setExtendedType(Class)
See Also:   ClassBodyEvaluator.setImplementedTypes(Class[])
See Also:   SimpleCompiler.setParentClassLoader(ClassLoader)
See Also:   Cookable.cook(Scanner)



ClassBodyEvaluator
public ClassBodyEvaluator()(Code)




Method Detail
addPackageMemberClassDeclaration
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws Parser.ParseException(Code)
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

Parameters:
  location -
Parameters:
  compilationUnit -
Parameters:
  className -
Parameters:
  implementedTypes - The created Java.ClassDeclaration object
throws:
  Parser.ParseException -



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




getClazz
public Class getClazz()(Code)
Returns the loaded Class .

This method must only be called after ClassBodyEvaluator.cook(Scanner) .

This method must not be called for instances of derived classes.




internalCook
protected void internalCook(Scanner scanner) throws CompileException, ParseException, ScanException, IOException(Code)



makeCompilationUnit
protected Java.CompilationUnit makeCompilationUnit(Scanner scanner) throws Parser.ParseException, Scanner.ScanException, IOException(Code)
Create a Java.CompilationUnit , set the default imports, and parse the import declarations.



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.



Fields inherited from org.codehaus.janino.SimpleCompiler
IClassLoader icloader(Code)(Java Doc)
boolean noloadSimpleCompiler(Code)(Java Doc)

Methods inherited from org.codehaus.janino.SimpleCompiler
protected Java.Type classToType(Location location, Class optionalClass)(Code)(Java Doc)
protected Java.Type[] classesToTypes(Location location, Class[] classes)(Code)(Java Doc)
public ClassFile[] compile(String javasrc)(Code)(Java Doc)
protected ClassLoader compileToClassLoader(Java.CompilationUnit compilationUnit, EnumeratorSet debuggingInformation) throws CompileException(Code)(Java Doc)
public boolean equals(Object o)(Code)(Java Doc)
public ClassLoader getClassLoader()(Code)(Java Doc)
public int hashCode()(Code)(Java Doc)
protected void internalCook(Scanner scanner) throws CompileException, Parser.ParseException, Scanner.ScanException, IOException(Code)(Java Doc)
public static void main(String[] args) throws Exception(Code)(Java Doc)
public void setParentClassLoader(ClassLoader optionalParentClassLoader)(Code)(Java Doc)

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)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.