Package org.codehaus.janino

# 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 inherited from interface org.codehaus.commons.compiler.IClassBodyEvaluator

`DEFAULT_CLASS_NAME`

## Constructor Summary

Constructors
Constructor
Description
`ClassBodyEvaluator()`
`ClassBodyEvaluator(String classBody)`
Equivalent to
`ClassBodyEvaluator(String fileName, InputStream is)`
Equivalent to
`ClassBodyEvaluator(String fileName, Reader reader)`
Equivalent to
`ClassBodyEvaluator(Scanner scanner, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader)`
Equivalent to
`ClassBodyEvaluator(Scanner scanner, ClassLoader parentClassLoader)`
Equivalent to
`ClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedType, Class<?>[] implementedTypes, ClassLoader parentClassLoader)`
Equivalent to

## Method Summary

Modifier and Type
Method
Description
`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
`Java.Type[]`
`classesToTypes(Location location, Class<?>[] classes)`
`protected Java.Type`
`classToType(Location location, Class<?> clazz)`
`final void`
`cook(String fileName, Reader r)`
Reads, scans, parses and compiles Java tokens from the given `Reader`.
`void`
`cook(Scanner scanner)`
`static Object`
`createFastClassBodyEvaluator(Scanner scanner, Class<?> baseType, ClassLoader parentClassLoader)`
Deprecated.
Use `createInstance(Reader)` instead
`static Object`
`createFastClassBodyEvaluator(Scanner scanner, String className, Class<?> extendedClass, Class<?>[] implementedInterfaces, ClassLoader parentClassLoader)`
Deprecated.
Use `createInstance(Reader)` instead
`Object`
`createInstance(Reader reader)`
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.
`Map<String,byte[]>`
`getBytecodes()`
`Class<?>`
`getClazz()`
`String[]`
`getDefaultImports()`
`protected Java.Type`
`optionalClassToType(Location location, Class<?> clazz)`
`EnumSet<JaninoOption>`
`options()`
`ClassBodyEvaluator`
`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 an `ErrorHandler` 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 custom `WarningHandler`.

### Methods inherited from class org.codehaus.commons.compiler.Cookable

`cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.codehaus.commons.compiler.ICookable

`cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile`

## Constructor Details

### ClassBodyEvaluator

public ClassBodyEvaluator(String classBody) throws CompileException
Equivalent to

         ClassBodyEvaluator cbe = new ClassBodyEvaluator();
         cbe.cook(classBody);

Throws:
  `CompileException`
See Also:
  - `ClassBodyEvaluator()`
  - `Cookable.cook(String)`

### ClassBodyEvaluator

public ClassBodyEvaluator(@Nullable String fileName, InputStream is) throws CompileException, IOException
Equivalent to

         ClassBodyEvaluator cbe = new ClassBodyEvaluator();
         cbe.cook(fileName, is);

Throws:
  `CompileException`
  `IOException`
See Also:
  - `ClassBodyEvaluator()`
  - `Cookable.cook(String, InputStream)`

### ClassBodyEvaluator

public ClassBodyEvaluator(@Nullable String fileName, Reader reader) throws CompileException, IOException
Equivalent to

         ClassBodyEvaluator cbe = new ClassBodyEvaluator();
         cbe.cook(fileName, reader);

Throws:
  `CompileException`
  `IOException`
See Also:
  - `ClassBodyEvaluator()`
  - `Cookable.cook(String, Reader)`

### ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Equivalent to

         ClassBodyEvaluator cbe = new ClassBodyEvaluator();
         cbe.setParentClassLoader(parentClassLoader);
         cbe.cook(scanner);

Throws:
  `CompileException`
  `IOException`
See Also:
  - `ClassBodyEvaluator()`
  - `SimpleCompiler.setParentClassLoader(ClassLoader)`
  - `Cookable.cook(Reader)`

### ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, @Nullable ClassLoader parentClassLoader) throws CompileException, IOException
Equivalent to

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

Throws:
  `CompileException`
  `IOException`
See Also:
  - `ClassBodyEvaluator()`
  - `setExtendedClass(Class)`
  - `setImplementedInterfaces(Class[])`
  - `SimpleCompiler.setParentClassLoader(ClassLoader)`
  - `Cookable.cook(Reader)`

### ClassBodyEvaluator

public ClassBodyEvaluator(Scanner scanner, String className, @Nullable Class<?> extendedType, Class<?>[] implementedTypes, @Nullable 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);

Throws:
  `CompileException`
  `IOException`
See Also:
  - `ClassBodyEvaluator()`
  - `setClassName(String)`
  - `setExtendedClass(Class)`
  - `setImplementedInterfaces(Class[])`
  - `SimpleCompiler.setParentClassLoader(ClassLoader)`
  - `Cookable.cook(Reader)`

### ClassBodyEvaluator

public ClassBodyEvaluator()

## Method Details

### setDefaultImports

public void setDefaultImports(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:
  `setDefaultImports` in interface `IClassBodyEvaluator`

### getDefaultImports

public String[] getDefaultImports()

Specified by:
  `getDefaultImports` in interface `IClassBodyEvaluator`
Returns:
  The default imports that were previously configured with `IClassBodyEvaluator.setDefaultImports(String...)`

### setClassName

public void setClassName(String className)
Description copied from interface: `IClassBodyEvaluator`
Sets the name of the generated class. Defaults to `IClassBodyEvaluator.DEFAULT_CLASS_NAME`. In most cases, there is no need to set this name, because the generated class is loaded into its own `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.

Specified by:
  `setClassName` in interface `IClassBodyEvaluator`

### setExtendedClass

public void setExtendedClass(@Nullable Class<?> extendedType)
Description copied from interface: `IClassBodyEvaluator`
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.

Specified by:
  `setExtendedClass` in interface `IClassBodyEvaluator`

### setExtendedType

public void setExtendedType(@Nullable Class<?> extendedClass)

Specified by:
  `setExtendedType` in interface `IClassBodyEvaluator`

### setImplementedInterfaces

public void setImplementedInterfaces(Class<?>[] implementedTypes)
Description copied from interface: `IClassBodyEvaluator`
Sets a particular set of interfaces that the generated class will implement.

Specified by:
  `setImplementedInterfaces` in interface `IClassBodyEvaluator`

### setImplementedTypes

public void setImplementedTypes(Class<?>[] implementedInterfaces)

Specified by:
  `setImplementedTypes` in interface `IClassBodyEvaluator`

### setParentClassLoader

public void setParentClassLoader(@Nullable ClassLoader parentClassLoader)
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()` 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.

Specified by:
  `setParentClassLoader` in interface `IClassBodyEvaluator`

### 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 interface `IClassBodyEvaluator`

### 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. 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.

Specified by:
  `setSourceVersion` in interface `ICookable`

### 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 interface `ICookable`

### setCompileErrorHandler

public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface: `IClassBodyEvaluator`
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.

Specified by:
  `setCompileErrorHandler` in interface `IClassBodyEvaluator`
Parameters:
  `compileErrorHandler` - `null` to restore the default behavior (throwing a `CompileException`)

### setWarningHandler

public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface: `IClassBodyEvaluator`
By default, warnings are discarded, but an application my install a custom `WarningHandler`.

Specified by:
  `setWarningHandler` in interface `IClassBodyEvaluator`
Parameters:
  `warningHandler` - `null` to indicate that no warnings be issued

### options

public EnumSet<JaninoOption> options()

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.

### cook

public final void cook(@Nullable String fileName, Reader r) throws CompileException, IOException
Description copied from interface: `ICookable`
Reads, scans, parses and compiles Java tokens from the given `Reader`.

Specified by:
  `cook` in interface `ICookable`
Specified by:
  `cook` in class `Cookable`
Parameters:
  `fileName` - Used when reporting errors and warnings
Throws:
  `CompileException`
  `IOException`

### cook

public void cook(Scanner scanner) throws CompileException, IOException

Throws:
  `CompileException`
  `IOException`

### getClazz

public Class<?> getClazz()

Specified by:
  `getClazz` in interface `IClassBodyEvaluator`
Returns:
  The generated `Class`

### getBytecodes

public Map<String,byte[]> getBytecodes()

Specified by:
  `getBytecodes` in interface `ICookable`
Returns:
  The generated Java bytecode; maps class name to bytes

### addPackageMemberClassDeclaration

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:
  The created `Java.AbstractClassDeclaration` object

### optionalClassToType

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

See Also:
  - `SimpleCompiler.optionalClassToType(Location, Class)`

### classToType

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

### classesToTypes

public Java.Type[] classesToTypes(Location location, Class<?>[] classes)

### createInstance

public Object createInstance(Reader reader) throws CompileException, IOException
Description copied from interface: `IClassBodyEvaluator`
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.

Specified by:
  `createInstance` in interface `IClassBodyEvaluator`
Parameters:
  `reader` - Source of class body tokens
Returns:
  An object that extends the `extendedType` and implements the given `implementedTypes`
Throws:
  `CompileException`
  `IOException`

### createFastClassBodyEvaluator

@Deprecated public static Object createFastClassBodyEvaluator(Scanner scanner, @Nullable Class<?> baseType, @Nullable 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);

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.
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);

Throws:
  `CompileException`
  `IOException`
See Also:
  - `createInstance(Reader)`