Class ClassBodyEvaluator
- All Implemented Interfaces:
IClassBodyEvaluator
,ICookable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
private String[]
private Class
<?> private Class<?>[]
private Class
<?> private final SimpleCompiler
private int
private WarningHandler
private static final Class<?>[]
Fields inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator
DEFAULT_CLASS_NAME
-
Constructor Summary
ConstructorsConstructorDescriptionClassBodyEvaluator
(String classBody) Equivalent toClassBodyEvaluator
(String fileName, InputStream is) Equivalent toClassBodyEvaluator
(String fileName, Reader reader) Equivalent toClassBodyEvaluator
(Scanner scanner, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader) Equivalent toClassBodyEvaluator
(Scanner scanner, ClassLoader parentClassLoader) Equivalent toClassBodyEvaluator
(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader) Equivalent to -
Method Summary
Modifier and TypeMethodDescriptionprotected 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 exceptionsprivate Class
<?> classesToTypes
(Location location, Class<?>[] classes) protected Java.Type
classToType
(Location location, Class<?> clazz) final void
Reads, scans, parses and compiles Java tokens from the givenReader
.(package private) void
cook
(Java.CompilationUnit compilationUnit) void
private void
static Object
createFastClassBodyEvaluator
(Scanner scanner, Class<?> baseType, ClassLoader parentClassLoader) Deprecated.static Object
createFastClassBodyEvaluator
(Scanner scanner, String className, Class<?> extendedClass, Class<?>[] implementedInterfaces, ClassLoader parentClassLoader) Deprecated.UsecreateInstance(Reader)
insteadcreateInstance
(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.Class
<?> getClazz()
String[]
(package private) final Java.AbstractCompilationUnit.ImportDeclaration[]
makeImportDeclarations
(Parser parser) protected Java.Type
optionalClassToType
(Location location, Class<?> clazz) options()
options
(EnumSet<JaninoOption> options) Sets the options for all future compilations.void
setClassName
(String className) Sets the name of the generated class.void
setCompileErrorHandler
(ErrorHandler compileErrorHandler) Installs anErrorHandler
which is invoked during compilation on each error.void
setDebuggingInformation
(boolean debugSource, boolean debugLines, boolean debugVars) Determines what kind of debugging information is included in the generates classes.void
setDefaultImports
(String... defaultImports) "Default imports" add to the system import "java.lang", i.e.void
setExtendedClass
(Class<?> extendedType) Sets a particular superclass that the generated class will extend.void
setExtendedType
(Class<?> extendedClass) void
setImplementedInterfaces
(Class<?>[] implementedTypes) Sets a particular set of interfaces that the generated class will implement.void
setImplementedTypes
(Class<?>[] implementedInterfaces) void
setParentClassLoader
(ClassLoader parentClassLoader) The "parent class loader" is used to load referenced classes.void
setSourceVersion
(int version) Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option.void
setTargetVersion
(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option.void
setWarningHandler
(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 Details
-
ZERO_CLASSES
-
warningHandler
-
sc
-
defaultImports
-
sourceVersion
private int sourceVersion -
className
-
extendedType
-
implementedTypes
-
result
-
-
Constructor Details
-
ClassBodyEvaluator
Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(classBody);
- Throws:
CompileException
- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(@Nullable String fileName, InputStream is) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(fileName, is);
- Throws:
CompileException
IOException
- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(@Nullable String fileName, Reader reader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.cook(fileName, reader);
- Throws:
CompileException
IOException
- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);
- Throws:
CompileException
IOException
- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setExtendedType(extendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);
- Throws:
CompileException
IOException
- See Also:
-
ClassBodyEvaluator
public ClassBodyEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException Equivalent toClassBodyEvaluator cbe = new ClassBodyEvaluator(); cbe.setClassName(className); cbe.setExtendedType(extendedType); cbe.setImplementedTypes(implementedTypes); cbe.setParentClassLoader(parentClassLoader); cbe.cook(scanner);
- Throws:
CompileException
IOException
- 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 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:
setDefaultImports
in interfaceIClassBodyEvaluator
-
getDefaultImports
- Specified by:
getDefaultImports
in interfaceIClassBodyEvaluator
- Returns:
- The default imports that were previously configured with
IClassBodyEvaluator.setDefaultImports(String...)
-
setClassName
Description copied from interface:IClassBodyEvaluator
Sets 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 ownClassLoader
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.
- Specified by:
setClassName
in interfaceIClassBodyEvaluator
-
setExtendedClass
Description copied from interface:IClassBodyEvaluator
Sets 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:
setExtendedClass
in interfaceIClassBodyEvaluator
-
setExtendedType
- Specified by:
setExtendedType
in interfaceIClassBodyEvaluator
-
setImplementedInterfaces
Description copied from interface:IClassBodyEvaluator
Sets a particular set of interfaces that the generated class will implement.- Specified by:
setImplementedInterfaces
in interfaceIClassBodyEvaluator
-
setImplementedTypes
- Specified by:
setImplementedTypes
in interfaceIClassBodyEvaluator
-
setParentClassLoader
Description copied from interface:IClassBodyEvaluator
The "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()
The running JVM's class path Thread.currentThread().getContextClassLoader()
ornull
The class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADER
The running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoader
in interfaceIClassBodyEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Description copied from interface:IClassBodyEvaluator
Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none
".- Specified by:
setDebuggingInformation
in interfaceIClassBodyEvaluator
-
setSourceVersion
public void setSourceVersion(int version) Description copied from interface:ICookable
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option. May be ignored by an implementation (e.g. thejanino
implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1
means to use a default version.- Specified by:
setSourceVersion
in interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version) Description copied from interface:ICookable
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command 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.-1
means to use a default version.- Specified by:
setTargetVersion
in interfaceICookable
-
setCompileErrorHandler
Description copied from interface:IClassBodyEvaluator
Installs anErrorHandler
which is invoked during compilation on each error. (By default, the compilation throws aCompileException
on the first error and terminates.)If the given
ErrorHandler
throws aCompileException
, then the compilation terminates and the exception is propagated.If the given
ErrorHandler
does not throw aCompileException
but 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 aCompileException
indicating the number of errors.In other words: The
ErrorHandler
may throw aCompileException
or not, but the compilation will definitely throw aCompileException
if one or more compile errors have occurred.- Specified by:
setCompileErrorHandler
in interfaceIClassBodyEvaluator
- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
)
-
setWarningHandler
Description copied from interface:IClassBodyEvaluator
By default, warnings are discarded, but an application my install a customWarningHandler
.- Specified by:
setWarningHandler
in interfaceIClassBodyEvaluator
- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
options
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
Sets the options for all future compilations. -
cook
Description copied from interface:ICookable
Reads, scans, parses and compiles Java tokens from the givenReader
.- Specified by:
cook
in interfaceICookable
- Specified by:
cook
in classCookable
- Parameters:
fileName
- Used when reporting errors and warnings- Throws:
CompileException
IOException
-
cook
- Throws:
CompileException
IOException
-
cook2
- Throws:
CompileException
IOException
-
cook
- Throws:
CompileException
-
getClazz
- Specified by:
getClazz
in interfaceIClassBodyEvaluator
- Returns:
- The generated
Class
-
getBytecodes
- Specified by:
getBytecodes
in interfaceICookable
- Returns:
- The generated Java bytecode; maps class name to bytes
-
makeImportDeclarations
final Java.AbstractCompilationUnit.ImportDeclaration[] makeImportDeclarations(@Nullable Parser parser) throws CompileException, IOException - Returns:
- The
default imports
, concatenated with the import declarations that can be parsed from the parser - Throws:
CompileException
IOException
- See Also:
-
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.AbstractClassDeclaration
object
-
optionalClassToType
- See Also:
-
classToType
-
classesToTypes
-
assertCooked
-
createInstance
Description copied from interface:IClassBodyEvaluator
Scans, 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:
createInstance
in interfaceIClassBodyEvaluator
- Parameters:
reader
- Source of class body tokens- Returns:
- An object that extends the
extendedType
and implements the givenimplementedTypes
- Throws:
CompileException
IOException
-
createFastClassBodyEvaluator
@Deprecated public static Object createFastClassBodyEvaluator(Scanner scanner, @Nullable Class<?> baseType, @Nullable ClassLoader parentClassLoader) throws CompileException, 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:
CompileException
IOException
-
createFastClassBodyEvaluator
@Deprecated public static Object createFastClassBodyEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedClass, Class<?>[] implementedInterfaces, @Nullable ClassLoader parentClassLoader) throws CompileException, 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:
CompileException
IOException
- See Also:
-
createInstance(Reader)
instead