Class ClassBodyEvaluator
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.janino.ClassBodyEvaluator
-
- All Implemented Interfaces:
IClassBodyEvaluator,ICookable
public class ClassBodyEvaluator extends Cookable implements IClassBodyEvaluator
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringclassNameprivate java.lang.String[]defaultImportsprivate java.lang.Class<?>extendedTypeprivate java.lang.Class<?>[]implementedTypesprivate java.lang.Class<?>resultprivate SimpleCompilerscprivate intsourceVersionprivate WarningHandlerwarningHandlerprivate static java.lang.Class<?>[]ZERO_CLASSES-
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAME
-
-
Constructor Summary
Constructors Constructor Description ClassBodyEvaluator()ClassBodyEvaluator(java.lang.String classBody)Equivalent toClassBodyEvaluator(java.lang.String fileName, java.io.InputStream is)Equivalent toClassBodyEvaluator(java.lang.String fileName, java.io.Reader reader)Equivalent toClassBodyEvaluator(Scanner scanner, java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, java.lang.ClassLoader parentClassLoader)Equivalent toClassBodyEvaluator(Scanner scanner, java.lang.ClassLoader parentClassLoader)Equivalent toClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, java.lang.ClassLoader parentClassLoader)Equivalent to
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected 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 exceptionsprivate java.lang.Class<?>assertCooked()Java.Type[]classesToTypes(Location location, java.lang.Class<?>[] classes)protected Java.TypeclassToType(Location location, java.lang.Class<?> clazz)voidcook(java.lang.String fileName, java.io.Reader r)Reads, scans, parses and compiles Java tokens from the givenReader.(package private) voidcook(Java.CompilationUnit compilationUnit)voidcook(Scanner scanner)private voidcook2(Scanner scanner)static java.lang.ObjectcreateFastClassBodyEvaluator(Scanner scanner, java.lang.Class<?> baseType, java.lang.ClassLoader parentClassLoader)Deprecated.UsecreateInstance(Reader)insteadstatic java.lang.ObjectcreateFastClassBodyEvaluator(Scanner scanner, java.lang.String className, java.lang.Class<?> extendedClass, java.lang.Class<?>[] implementedInterfaces, java.lang.ClassLoader parentClassLoader)Deprecated.UsecreateInstance(Reader)insteadjava.lang.ObjectcreateInstance(java.io.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.java.util.Map<java.lang.String,byte[]>getBytecodes()java.lang.Class<?>getClazz()java.lang.String[]getDefaultImports()(package private) Java.AbstractCompilationUnit.ImportDeclaration[]makeImportDeclarations(Parser parser)protected Java.TypeoptionalClassToType(Location location, java.lang.Class<?> clazz)java.util.EnumSet<JaninoOption>options()ClassBodyEvaluatoroptions(java.util.EnumSet<JaninoOption> options)Sets the options for all future compilations.voidsetClassName(java.lang.String className)Sets the name of the generated class.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler)Installs anErrorHandlerwhich is invoked during compilation on each error.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Determines what kind of debugging information is included in the generates classes.voidsetDefaultImports(java.lang.String... defaultImports)"Default imports" add to the system import "java.lang", i.e.voidsetExtendedClass(java.lang.Class<?> extendedType)Sets a particular superclass that the generated class will extend.voidsetExtendedType(java.lang.Class<?> extendedClass)voidsetImplementedInterfaces(java.lang.Class<?>[] implementedTypes)Sets a particular set of interfaces that the generated class will implement.voidsetImplementedTypes(java.lang.Class<?>[] implementedInterfaces)voidsetParentClassLoader(java.lang.ClassLoader parentClassLoader)The "parent class loader" is used to load referenced classes.voidsetSourceVersion(int version)Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option.voidsetTargetVersion(int version)Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install a customWarningHandler.-
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
-
-
-
Field Detail
-
ZERO_CLASSES
private static final java.lang.Class<?>[] ZERO_CLASSES
-
warningHandler
@Nullable private WarningHandler warningHandler
-
sc
private final SimpleCompiler sc
-
defaultImports
private java.lang.String[] defaultImports
-
sourceVersion
private int sourceVersion
-
className
private java.lang.String className
-
extendedType
@Nullable private java.lang.Class<?> extendedType
-
implementedTypes
private java.lang.Class<?>[] implementedTypes
-
result
@Nullable private java.lang.Class<?> result
-
-
Constructor Detail
-
ClassBodyEvaluator
public ClassBodyEvaluator(java.lang.String classBody) throws CompileExceptionEquivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);- Throws:
CompileException- See Also:
ClassBodyEvaluator(),Cookable.cook(String)
-
ClassBodyEvaluator
public ClassBodyEvaluator(@Nullable java.lang.String fileName, java.io.InputStream is) throws CompileException, java.io.IOException
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(fileName, is);- Throws:
CompileExceptionjava.io.IOException- See Also:
ClassBodyEvaluator(),Cookable.cook(String, InputStream)
-
ClassBodyEvaluator
public ClassBodyEvaluator(@Nullable java.lang.String fileName, java.io.Reader reader) throws CompileException, java.io.IOException
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(fileName, reader);- Throws:
CompileExceptionjava.io.IOException- See Also:
ClassBodyEvaluator(),Cookable.cook(String, Reader)
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);- Throws:
CompileExceptionjava.io.IOException- See Also:
ClassBodyEvaluator(),SimpleCompiler.setParentClassLoader(ClassLoader),Cookable.cook(Reader)
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(extendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);- Throws:
CompileExceptionjava.io.IOException- See Also:
ClassBodyEvaluator(),setExtendedClass(Class),setImplementedInterfaces(Class[]),SimpleCompiler.setParentClassLoader(ClassLoader),Cookable.cook(Reader)
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, java.lang.String className, @Nullable java.lang.Class<?> extendedType, java.lang.Class<?>[] implementedTypes, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(extendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);- Throws:
CompileExceptionjava.io.IOException- See Also:
ClassBodyEvaluator(),setClassName(String),setExtendedClass(Class),setImplementedInterfaces(Class[]),SimpleCompiler.setParentClassLoader(ClassLoader),Cookable.cook(Reader)
-
ClassBodyEvaluator
public ClassBodyEvaluator()
-
-
Method Detail
-
setDefaultImports
public void setDefaultImports(java.lang.String... defaultImports)
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 here:
sc.setDefaultImports( "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
-
getDefaultImports
public java.lang.String[] getDefaultImports()
- Specified by:
getDefaultImportsin interfaceIClassBodyEvaluator- Returns:
- The default imports that were previously configured with
IClassBodyEvaluator.setDefaultImports(String...)
-
setClassName
public void setClassName(java.lang.String className)
Description copied from interface:IClassBodyEvaluatorSets 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
public void setExtendedClass(@Nullable java.lang.Class<?> extendedType)
Description copied from interface:IClassBodyEvaluatorSets a particular superclass that the generated class will extend. If extendedClass isnull, the generated class will extendObject.The usual reason to set a base class for an evaluator is that the generated class can directly access the superclass's (non-private) members.
- Specified by:
setExtendedClassin interfaceIClassBodyEvaluator
-
setExtendedType
public void setExtendedType(@Nullable java.lang.Class<?> extendedClass)
- Specified by:
setExtendedTypein interfaceIClassBodyEvaluator
-
setImplementedInterfaces
public void setImplementedInterfaces(java.lang.Class<?>[] implementedTypes)
Description copied from interface:IClassBodyEvaluatorSets a particular set of interfaces that the generated class will implement.- Specified by:
setImplementedInterfacesin interfaceIClassBodyEvaluator
-
setImplementedTypes
public void setImplementedTypes(java.lang.Class<?>[] implementedInterfaces)
- Specified by:
setImplementedTypesin interfaceIClassBodyEvaluator
-
setParentClassLoader
public void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader)
Description copied from interface:IClassBodyEvaluatorThe "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()The running JVM's class path Thread.currentThread().getContextClassLoader()ornullThe class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADERThe running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoaderin interfaceIClassBodyEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Description copied from interface:IClassBodyEvaluatorDetermines what kind of debugging information is included in the generates classes. The default is typically "-g:none".- Specified by:
setDebuggingInformationin interfaceIClassBodyEvaluator
-
setSourceVersion
public void setSourceVersion(int version)
Description copied from interface:ICookableSpecifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option. May be ignored by an implementation (e.g. thejaninoimplementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1means to use a default version.- Specified by:
setSourceVersionin interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookableGenerates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM.-1means to use a default version.- Specified by:
setTargetVersionin interfaceICookable
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:IClassBodyEvaluatorInstalls anErrorHandlerwhich is invoked during compilation on each error. (By default, the compilation throws aCompileExceptionon the first error and terminates.)If the given
ErrorHandlerthrows aCompileException, then the compilation terminates and the exception is propagated.If the given
ErrorHandlerdoes not throw aCompileExceptionbut completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation completes normally but errors were reported, then it will throw aCompileExceptionindicating the number of errors.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compilation will definitely throw aCompileExceptionif one or more compile errors have occurred.- Specified by:
setCompileErrorHandlerin interfaceIClassBodyEvaluator- Parameters:
compileErrorHandler-nullto restore the default behavior (throwing aCompileException)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:IClassBodyEvaluatorBy default, warnings are discarded, but an application my install a customWarningHandler.- Specified by:
setWarningHandlerin interfaceIClassBodyEvaluator- Parameters:
warningHandler-nullto indicate that no warnings be issued
-
options
public java.util.EnumSet<JaninoOption> options()
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
public ClassBodyEvaluator options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.
-
cook
public final void cook(@Nullable java.lang.String fileName, java.io.Reader r) throws CompileException, java.io.IOException
Description copied from interface:ICookableReads, scans, parses and compiles Java tokens from the givenReader.- Specified by:
cookin interfaceICookable- Specified by:
cookin classCookable- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionjava.io.IOException
-
cook
public void cook(Scanner scanner) throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
cook2
private void cook2(Scanner scanner) throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
cook
void cook(Java.CompilationUnit compilationUnit) throws CompileException
- Throws:
CompileException
-
getClazz
public java.lang.Class<?> getClazz()
- Specified by:
getClazzin interfaceIClassBodyEvaluator- Returns:
- The generated
Class
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
makeImportDeclarations
final Java.AbstractCompilationUnit.ImportDeclaration[] makeImportDeclarations(@Nullable Parser parser) throws CompileException, java.io.IOException
- Returns:
- The
default imports, concatenated with the import declarations that can be parsed from the parser - Throws:
CompileExceptionjava.io.IOException- See Also:
Parser.parseImportDeclarationBody()
-
addPackageMemberClassDeclaration
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(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 exceptions
- Returns:
- The created
Java.AbstractClassDeclarationobject
-
optionalClassToType
@Nullable protected Java.Type optionalClassToType(Location location, @Nullable java.lang.Class<?> clazz)
-
assertCooked
private java.lang.Class<?> assertCooked()
-
createInstance
public java.lang.Object createInstance(java.io.Reader reader) throws CompileException, java.io.IOExceptionDescription 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
extendedTypeand implements the givenimplementedTypes - Throws:
CompileExceptionjava.io.IOException
-
createFastClassBodyEvaluator
@Deprecated public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, @Nullable java.lang.Class<?> baseType, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateInstance(Reader)insteadUsecreateInstance(Reader)instead:IClassBodyEvaluator cbe =CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator(); if (baseType != null) { if (baseType.isInterface()) { cbe.setImplementedInterfaces(new Class[] { baseType }); } else { cbe.setExtendedClass(baseType); } } cbe.setParentClassLoader(parentClassLoader); cbe.createInstance(reader);- Throws:
CompileExceptionjava.io.IOException
-
createFastClassBodyEvaluator
@Deprecated public static java.lang.Object createFastClassBodyEvaluator(Scanner scanner, java.lang.String className, @Nullable java.lang.Class<?> extendedClass, java.lang.Class<?>[] implementedInterfaces, @Nullable java.lang.ClassLoader parentClassLoader) throws CompileException, java.io.IOException
Deprecated.UsecreateInstance(Reader)insteadUsecreateInstance(Reader)instead:IClassBodyEvaluator cbe =CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator(); cbe.setExtendedClass(extendedClass); cbe.setImplementedInterfaces(implementedInterfaces); cbe.setParentClassLoader(parentClassLoader); cbe.createInstance(reader);- Throws:
CompileExceptionjava.io.IOException- See Also:
createInstance(Reader)
-
-