Package org.codehaus.janino
Class ClassBodyEvaluator
java.lang.Object
org.codehaus.commons.compiler.Cookable
org.codehaus.janino.SimpleCompiler
org.codehaus.janino.ClassBodyEvaluator
- All Implemented Interfaces:
IClassBodyEvaluator,ICookable,ISimpleCompiler
- Direct Known Subclasses:
ScriptEvaluator
The
optionalClassLoader serves two purposes:
- It is used to look for classes referenced by the class body.
- It is used to load the generated Java™ class
into the JVM; directly if it is a subclass of
ByteArrayClassLoader, or by creation of a temporaryByteArrayClassLoaderif not.
-
Field Summary
FieldsFields inherited from class org.codehaus.janino.SimpleCompiler
debugLines, debugSource, debugVarsFields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAMEFields inherited from interface org.codehaus.commons.compiler.ICookable
BOOT_CLASS_LOADER, SYSTEM_PROPERTY_SOURCE_DEBUGGING_DIR, SYSTEM_PROPERTY_SOURCE_DEBUGGING_ENABLE -
Constructor Summary
ConstructorsConstructorDescriptionClassBodyEvaluator(String classBody) Equivalent toClassBodyEvaluator(String optionalFileName, InputStream is) Equivalent toClassBodyEvaluator(String optionalFileName, Reader reader) Equivalent toClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader) Equivalent toClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) Equivalent toClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) Equivalent to -
Method Summary
Modifier and TypeMethodDescriptionprotected Java.PackageMemberClassDeclarationaddPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) To the givenJava.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 exceptionsprotected final ClasscompileToClass(Java.CompilationUnit compilationUnit, String newClassName) Compile the given compilation unit, load all generated classes, and return the class with the given name.voidScans, parses and ompiles a given compilation unit from the given scanner.static ObjectcreateFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) UsecreateInstance(Reader)instead:static ObjectcreateFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedClass, Class[] implementedInterfaces, ClassLoader optionalParentClassLoader) Deprecated.createInstance(Reader reader) Scans, parses and compiles a class body from the tokens delivered by the the givenReader, then creates and returns an instance of that class.getClazz()Returns the loadedClass.protected final Java.CompilationUnitmakeCompilationUnit(Parser optionalParser) Create aJava.CompilationUnit, set the default imports, and parse the import declarations.voidsetClassName(String className) Set the name of the generated class.voidsetDefaultImports(String[] optionalDefaultImports) "Default imports" add to the system import "java.lang", i.e.voidsetExtendedClass(Class optionalExtendedType) Set a particular superclass that the generated class will extend.voidsetExtendedType(Class optionalExtendedClass) Deprecated.voidsetImplementedInterfaces(Class[] implementedTypes) Set a particular set of interfaces that the generated class will implement.voidsetImplementedTypes(Class[] implementedInterfaces) Deprecated.Methods inherited from class org.codehaus.janino.SimpleCompiler
assertNotCooked, classesToTypes, classToType, compileToClassLoader, cook, cook, equals, getClassLoader, hashCode, main, setDebuggingInformation, setParentClassLoaderMethods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile, readString
-
Field Details
-
ZERO_CLASSES
-
className
-
-
Constructor Details
-
ClassBodyEvaluator
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
- Throws:
CompileException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(String optionalFileName, InputStream is) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, is);
- Throws:
CompileExceptionIOException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(String optionalFileName, Reader reader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(optionalFileName, reader);
- Throws:
CompileExceptionIOException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, ClassLoader optionalParentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
- Throws:
CompileExceptionIOException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
- Throws:
CompileExceptionIOException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedType, Class[] implementedTypes, ClassLoader optionalParentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(optionalExtendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(optionalParentClassLoader); cbe.cook(scanner);
- Throws:
CompileExceptionIOException- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator()
-
-
Method Details
-
setDefaultImports
Description copied from interface:IClassBodyEvaluator"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.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 });- Specified by:
setDefaultImportsin interfaceIClassBodyEvaluator
-
setClassName
Description copied from interface:IClassBodyEvaluatorSet the name of the generated class. Defaults toIClassBodyEvaluator.DEFAULT_CLASS_NAME. In most cases, there is no need to set this name, because the generated class is loaded into its ownClassLoaderwhere 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.
- Specified by:
setClassNamein interfaceIClassBodyEvaluator
-
setExtendedClass
Description copied from interface:IClassBodyEvaluatorSet a particular superclass that the generated class will extend. Ifnullis passed, the generated class will extendObject.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.
- Specified by:
setExtendedClassin interfaceIClassBodyEvaluator
-
setExtendedType
Deprecated.- Specified by:
setExtendedTypein interfaceIClassBodyEvaluator
-
setImplementedInterfaces
Description copied from interface:IClassBodyEvaluatorSet a particular set of interfaces that the generated class will implement.- Specified by:
setImplementedInterfacesin interfaceIClassBodyEvaluator
-
setImplementedTypes
Deprecated.- Specified by:
setImplementedTypesin interfaceIClassBodyEvaluator
-
cook
Description copied from class:SimpleCompilerScans, parses and ompiles a given compilation unit from the given scanner. After completion,SimpleCompiler.getClassLoader()returns aClassLoaderthat allows for access to the compiled classes.- Overrides:
cookin classSimpleCompiler- Throws:
CompileExceptionIOException
-
makeCompilationUnit
protected final Java.CompilationUnit makeCompilationUnit(Parser optionalParser) throws CompileException, IOException Create aJava.CompilationUnit, set the default imports, and parse the import declarations.If the
optionalParseris given, a sequence of IMPORT directives is parsed from it and added to the compilation unit.- Throws:
CompileExceptionIOException
-
addPackageMemberClassDeclaration
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit) throws CompileException To the givenJava.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
- Returns:
- The created
Java.ClassDeclarationobject - Throws:
CompileException
-
compileToClass
protected final Class compileToClass(Java.CompilationUnit compilationUnit, String newClassName) throws CompileException Compile the given compilation unit, load all generated classes, and return the class with the given name.- Parameters:
compilationUnit-newClassName- The fully qualified class name- Returns:
- The loaded class
- Throws:
CompileException
-
getClazz
Description copied from interface:IClassBodyEvaluatorReturns the loadedClass.This method must only be called after exactly one of the
ICookable.cook(String, java.io.Reader)methods was called.- Specified by:
getClazzin interfaceIClassBodyEvaluator
-
createInstance
Description copied from interface:IClassBodyEvaluatorScans, parses and compiles a class body from the tokens delivered by the the givenReader, then creates and returns an instance of that class.- Specified by:
createInstancein interfaceIClassBodyEvaluator- Parameters:
reader- Source of class body tokens- Returns:
- An object that extends the
optionalExtendedTypeand implements the givenimplementedTypes - Throws:
CompileExceptionIOException
-
createFastClassBodyEvaluator
public static Object createFastClassBodyEvaluator(Scanner scanner, Class optionalBaseType, ClassLoader optionalParentClassLoader) throws CompileException, IOException UsecreateInstance(Reader)instead:IClassBodyEvaluator cbe =
CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator(); if (optionalBaseType != null) { if (optionalBaseType.isInterface()) { cbe.setImplementedInterfaces(new Class[] { optionalBaseType }); } else { cbe.setExtendedClass(optionalBaseType); } } cbe.setParentClassLoader(optionalParentClassLoader); cbe.createInstance(reader);- Throws:
CompileExceptionIOException- See Also:
-
createFastClassBodyEvaluator
@Deprecated public static Object createFastClassBodyEvaluator(Scanner scanner, String className, Class optionalExtendedClass, Class[] implementedInterfaces, ClassLoader optionalParentClassLoader) throws CompileException, IOException Deprecated.UsecreateInstance(Reader)instead:IClassBodyEvaluator cbe =
CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator(); cbe.setExtendedClass(optionalExtendedClass); cbe.setImplementedInterfaces(implementedInterfaces); cbe.setParentClassLoader(optionalParentClassLoader); cbe.createInstance(reader);- Throws:
CompileExceptionIOException- See Also:
-