Top Fields Constructors Methods
org.codehaus.janino

public Class ClassBodyEvaluator

extends Cookable
implements IClassBodyEvaluator
Class Inheritance
All Implemented Interfaces
org.codehaus.commons.compiler.IClassBodyEvaluator, org.codehaus.commons.compiler.ICookable
Imports
java.io.IOException, .InputStream, .Reader, .StringReader, java.util.ArrayList, .EnumSet, .List, .Map, org.codehaus.commons.compiler.CompileException, .CompilerFactoryFactory, .Cookable, .ErrorHandler, .IClassBodyEvaluator, .ICompilerFactory, .InternalCompilerException, .Location, .WarningHandler, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.Java.AbstractCompilationUnit, .Java.CompilationUnit

Field Summary

Modifier and TypeField and Description
private String
private String[]
private Class<?>
private Class<?>[]
private Class<?>
private final SimpleCompiler
private int
private WarningHandler
private static final Class<?>[]

Constructor Summary

AccessConstructor and Description
public
ClassBodyEvaluator(String classBody)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(classBody);
public
ClassBodyEvaluator(String fileName, InputStream is)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(fileName, is);
public
ClassBodyEvaluator(String fileName, Reader reader)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(fileName, reader);
public
ClassBodyEvaluator(Scanner scanner, ClassLoader parentClassLoader)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);
public
ClassBodyEvaluator(Scanner scanner, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setExtendedType(extendedType);
    cbe.setImplementedTypes(implementedTypes);
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);
public
ClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader)

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(className);
    cbe.setExtendedType(extendedType);
    cbe.setImplementedTypes(implementedTypes);
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);
public

Method Summary

Modifier and TypeMethod and Description
protected Java.PackageMemberClassDeclaration

Returns:

The created Java.AbstractClassDeclaration object
addPackageMemberClassDeclaration
(Location location, Java.CompilationUnit compilationUnit)

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
private Class<?>
public Java.Type[]
classesToTypes(Location location, Class<?>[] classes)

protected Java.Type
classToType(final Location location, final Class<?> clazz)

public final void
cook(String
Used when reporting errors and warnings
fileName
,
Reader r)

Implements abstract org.codehaus.commons.compiler.Cookable.cook.

Implements org.codehaus.commons.compiler.ICookable.cook.

Reads, scans, parses and compiles Java tokens from the given Reader.
public void
cook(Scanner scanner)

pack-priv void
cook(Java.CompilationUnit compilationUnit)

private void
cook2(Scanner scanner)

public static Object
createFastClassBodyEvaluator(Scanner scanner, Class<?> baseType, ClassLoader parentClassLoader)

Deprecated Use createInstance(Reader) instead
Use createInstance(Reader) instead:
    IClassBodyEvaluator cbe = CompilerFactoryFactory.
public static Object
createFastClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedClass, Class<?>[] implementedInterfaces, ClassLoader parentClassLoader)

Deprecated Use createInstance(Reader) instead
Use createInstance(Reader) instead:
    IClassBodyEvaluator cbe = CompilerFactoryFactory.
public Object
createInstance(Reader
Source of class body tokens
reader
)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.createInstance.

Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.
public Map<String, byte[]>
public Class<?>
public String[]
pack-priv final Java.AbstractCompilationUnit.ImportDeclaration[]

Returns:

The default imports, concatenated with the import declarations that can be parsed from the parser
makeImportDeclarations
(Parser parser)

protected Java.Type
optionalClassToType(final Location location, final Class<?> clazz)

public EnumSet<JaninoOption>

Returns:

A reference to the currently effective compilation options; changes to it take effect immediately
options
()

public ClassBodyEvaluator
options(EnumSet<JaninoOption> options)

Sets the options for all future compilations.

public void
setClassName(String className)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setClassName.

Sets the name of the generated class.
public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
compileErrorHandler
)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setCompileErrorHandler.

Installs an ErrorHandler which is invoked during compilation on each error.
public void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setDebuggingInformation.

Determines what kind of debugging information is included in the generates classes.
public void
setDefaultImports(String... defaultImports)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setDefaultImports.

"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.
public void
setExtendedClass(Class<?> extendedType)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setExtendedClass.

Sets a particular superclass that the generated class will extend.
public void
public void
setImplementedInterfaces(Class<?>[] implementedTypes)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setImplementedInterfaces.

Sets a particular set of interfaces that the generated class will implement.
public void
public void
setParentClassLoader(ClassLoader parentClassLoader)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setParentClassLoader.

The "parent class loader" is used to load referenced classes.
public void
setSourceVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setSourceVersion.

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option.
public void
setTargetVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setTargetVersion.

Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option.
public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setWarningHandler.

By default, warnings are discarded, but an application my install a custom WarningHandler.
Inherited from org.codehaus.commons.compiler.Cookable:
cookcookcookcookcookcookcookcookFilecookFilecookFilecookFile

Field Detail

classNameback to summary
private String className
defaultImportsback to summary
private String[] defaultImports
extendedTypeback to summary
private Class<?> extendedType
Annotations
@Nullable
implementedTypesback to summary
private Class<?>[] implementedTypes
resultback to summary
private Class<?> result
Annotations
@Nullable
scback to summary
private final SimpleCompiler sc
sourceVersionback to summary
private int sourceVersion
warningHandlerback to summary
private WarningHandler warningHandler
Annotations
@Nullable
ZERO_CLASSESback to summary
private static final Class<?>[] ZERO_CLASSES

Constructor Detail

ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(String classBody) throws CompileException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(classBody);
See Also
ClassBodyEvaluator(), Cookable#cook(String)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(String fileName, InputStream is) throws CompileException, IOException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(fileName, is);
See Also
ClassBodyEvaluator(), Cookable#cook(String, InputStream)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(String fileName, Reader reader) throws CompileException, IOException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.cook(fileName, reader);
See Also
ClassBodyEvaluator(), Cookable#cook(String, Reader)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(Scanner scanner, ClassLoader parentClassLoader) throws CompileException, IOException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);
See Also
ClassBodyEvaluator(), SimpleCompiler#setParentClassLoader(ClassLoader), Cookable#cook(Reader)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(Scanner scanner, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader) throws CompileException, IOException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setExtendedType(extendedType);
    cbe.setImplementedTypes(implementedTypes);
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);
See Also
ClassBodyEvaluator(), setExtendedClass(Class), setImplementedInterfaces(Class[]), SimpleCompiler#setParentClassLoader(ClassLoader), Cookable#cook(Reader)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader) throws CompileException, IOException

Equivalent to

    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
    cbe.setClassName(className);
    cbe.setExtendedType(extendedType);
    cbe.setImplementedTypes(implementedTypes);
    cbe.setParentClassLoader(parentClassLoader);
    cbe.cook(scanner);

See Also
ClassBodyEvaluator(), setClassName(String), setExtendedClass(Class), setImplementedInterfaces(Class[]), SimpleCompiler#setParentClassLoader(ClassLoader), Cookable#cook(Reader)
ClassBodyEvaluatorback to summary
public ClassBodyEvaluator()

Method Detail

addPackageMemberClassDeclarationback to summary
protected Java.PackageMemberClassDeclaration addPackageMemberClassDeclaration(Location location, Java.CompilationUnit compilationUnit)

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
Returns:Java.PackageMemberClassDeclaration

The created Java.AbstractClassDeclaration object

assertCookedback to summary
private Class<?> assertCooked()
classesToTypesback to summary
public Java.Type[] classesToTypes(Location location, Class<?>[] classes)
classToTypeback to summary
protected Java.Type classToType(final Location location, final Class<?> clazz)
cookback to summary
public final void cook(String fileName, Reader r) throws CompileException, IOException

Implements abstract org.codehaus.commons.compiler.Cookable.cook.

Implements org.codehaus.commons.compiler.ICookable.cook.

Doc from org.codehaus.commons.compiler.ICookable.cook.

Reads, scans, parses and compiles Java tokens from the given Reader.

Parameters
fileName:String

Used when reporting errors and warnings

Annotations
@Override
cookback to summary
public void cook(Scanner scanner) throws CompileException, IOException
cookback to summary
pack-priv void cook(Java.CompilationUnit compilationUnit) throws CompileException
cook2back to summary
private void cook2(Scanner scanner) throws CompileException, IOException
createFastClassBodyEvaluatorback to summary
public static Object createFastClassBodyEvaluator(Scanner scanner, Class<?> baseType, ClassLoader parentClassLoader) throws CompileException, IOException

Deprecated

Use createInstance(Reader) instead

Use createInstance(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);
Annotations
@Deprecated
createFastClassBodyEvaluatorback to summary
public static Object createFastClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedClass, Class<?>[] implementedInterfaces, ClassLoader parentClassLoader) throws CompileException, IOException

Deprecated

Use createInstance(Reader) instead

Use createInstance(Reader) instead:

    IClassBodyEvaluator cbe = CompilerFactoryFactory.getDefaultCompilerFactory().newClassBodyEvaluator();
    cbe.setExtendedClass(extendedClass);
    cbe.setImplementedInterfaces(implementedInterfaces);
    cbe.setParentClassLoader(parentClassLoader);
    cbe.createInstance(reader);
Annotations
@Deprecated
See Also
createInstance(Reader)
createInstanceback to summary
public Object createInstance(Reader reader) throws CompileException, IOException

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.createInstance.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.createInstance.

Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.

Parameters
reader:Reader

Source of class body tokens

Returns:Object

An object that extends the extendedType and implements the given implementedTypes

Annotations
@Override
getBytecodesback to summary
public Map<String, byte[]> getBytecodes()

Implements org.codehaus.commons.compiler.ICookable.getBytecodes.

Returns:Map<String, byte[]>

Doc from org.codehaus.commons.compiler.ICookable.getBytecodes.

The generated Java bytecode; maps class name to bytes

Annotations
@Override
getClazzback to summary
public Class<?> getClazz()

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.getClazz.

Returns:Class<?>

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.getClazz.

The generated Class

Annotations
@Override
getDefaultImportsback to summary
public String[] getDefaultImports()

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.getDefaultImports.

Returns:String[]

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.getDefaultImports.

The default imports that were previously configured with setDefaultImports(String...)

Annotations
@Override
makeImportDeclarationsback to summary
pack-priv final Java.AbstractCompilationUnit.ImportDeclaration[] makeImportDeclarations(Parser parser) throws CompileException, IOException
Returns:Java.AbstractCompilationUnit.ImportDeclaration[]

The default imports, concatenated with the import declarations that can be parsed from the parser

See Also
Parser#parseImportDeclarationBody()
optionalClassToTypeback to summary
protected Java.Type optionalClassToType(final Location location, final Class<?> clazz)
Annotations
@Nullable
See Also
SimpleCompiler#optionalClassToType(Location, Class)
optionsback to summary
public EnumSet<JaninoOption> options()
Returns:EnumSet<JaninoOption>

A reference to the currently effective compilation options; changes to it take effect immediately

optionsback to summary
public ClassBodyEvaluator options(EnumSet<JaninoOption> options)

Sets the options for all future compilations.

setClassNameback to summary
public void setClassName(String className)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setClassName.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setClassName.

Sets the name of the generated class. Defaults to 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.

Annotations
@Override
setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler compileErrorHandler)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setCompileErrorHandler.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setCompileErrorHandler.

Installs an ErrorHandler which is invoked during compilation on each error. (By default, the compilation throws a CompileException on the first error and terminates.)

If the given ErrorHandler throws a CompileException, then the compilation terminates and the exception is propagated.

If the given ErrorHandler does not throw a CompileException 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 a CompileException indicating the number of errors.

In other words: The ErrorHandler may throw a CompileException or not, but the compilation will definitely throw a CompileException if one or more compile errors have occurred.

Parameters
compileErrorHandler:ErrorHandler

null to restore the default behavior (throwing a CompileException)

Annotations
@Override
setDebuggingInformationback to summary
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setDebuggingInformation.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setDebuggingInformation.

Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none".

Annotations
@Override
setDefaultImportsback to summary
public void setDefaultImports(String... defaultImports)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setDefaultImports.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setDefaultImports.

"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
    );
Annotations
@Override
setExtendedClassback to summary
public void setExtendedClass(Class<?> extendedType)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setExtendedClass.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setExtendedClass.

Sets a particular superclass that the generated class will extend. If extendedClass is null, the generated class will extend Object.

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.

Annotations
@Override
setExtendedTypeback to summary
public void setExtendedType(Class<?> extendedClass)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setExtendedType.

Annotations
@Override
setImplementedInterfacesback to summary
public void setImplementedInterfaces(Class<?>[] implementedTypes)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setImplementedInterfaces.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setImplementedInterfaces.

Sets a particular set of interfaces that the generated class will implement.

Annotations
@Override
setImplementedTypesback to summary
public void setImplementedTypes(Class<?>[] implementedInterfaces)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setImplementedTypes.

Annotations
@Override
setParentClassLoaderback to summary
public void setParentClassLoader(ClassLoader parentClassLoader)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setParentClassLoader.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setParentClassLoader.

The "parent class loader" is used to load referenced classes. Useful values are:

System.getSystemClassLoader() The running JVM's class path
Thread.currentThread().getContextClassLoader() or null 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.

Annotations
@Override
setSourceVersionback to summary
public void setSourceVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setSourceVersion.

Doc from org.codehaus.commons.compiler.ICookable.setSourceVersion.

Specifies the version of source code accepted, in analogy with JAVAC's -source command line option. May be ignored by an implementation (e.g. the janino 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.

Annotations
@Override
setTargetVersionback to summary
public void setTargetVersion(int version)

Implements org.codehaus.commons.compiler.ICookable.setTargetVersion.

Doc from org.codehaus.commons.compiler.ICookable.setTargetVersion.

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.

Annotations
@Override
setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)

Implements org.codehaus.commons.compiler.IClassBodyEvaluator.setWarningHandler.

Doc from org.codehaus.commons.compiler.IClassBodyEvaluator.setWarningHandler.

By default, warnings are discarded, but an application my install a custom WarningHandler.

Parameters
warningHandler:WarningHandler

null to indicate that no warnings be issued

Annotations
@Override