Package org.codehaus.janino

# Class SimpleCompiler

java.lang.Object
org.codehaus.commons.compiler.Cookable
org.codehaus.janino.SimpleCompiler

All Implemented Interfaces:
  `ICookable`, `ISimpleCompiler`

public class SimpleCompiler extends Cookable implements ISimpleCompiler
To set up a `SimpleCompiler` object, proceed as described for `ISimpleCompiler`. Alternatively, a number of "convenience constructors" exist that execute the described steps instantly.

## Constructor Summary

Constructors
Constructor
Description
`SimpleCompiler()`
`SimpleCompiler(String fileName)`
Equivalent to
`SimpleCompiler(String fileName, InputStream is)`
Equivalent to
`SimpleCompiler(String fileName, Reader in)`
Equivalent to
`SimpleCompiler(Scanner scanner, ClassLoader parentClassLoader)`
Equivalent to

## Method Summary

Modifier and Type
Method
Description
`protected Java.Type[]`
`classesToTypes(Location location, Class<?>[] classes)`
Converts an array of `Class`es into an array of`Java.Type`s.
`protected Java.Type`
`classToType(Location location, Class<?> clazz)`
Wraps a reflection `Class` in a `Java.Type` object.
`protected final ClassLoader`
`compileToClassLoader(Java.AbstractCompilationUnit abstractCompilationUnit)`
Compiles the given compilation unit.
`final void`
`cook(String fileName, Reader r)`
Scans, parses and compiles a given compilation unit from the given `Reader`.
`void`
`cook(Java.AbstractCompilationUnit abstractCompilationUnit)`
Cooks this compilation unit directly.
`void`
`cook(Scanner scanner)`
Scans, parses and compiles a given compilation unit from the given scanner.
`boolean`
`equals(Object o)`
Two `SimpleCompiler`s are regarded equal iff Both are objects of the same class (e.g.
`Map<String,byte[]>`
`getBytecodes()`
`ClassFile[]`
`getClassFiles()`
`ClassLoader`
`getClassLoader()`
Returns a `ClassLoader` object through which the previously compiled classes can be accessed.
`int`
`hashCode()`
`static void`
`main(String[] args)`
`protected Java.Type`
`optionalClassToType(Location location, Class<?> clazz)`
Wraps a reflection `Class` in a `Java.Type` object.
`EnumSet<JaninoOption>`
`options()`
`SimpleCompiler`
`options(EnumSet<JaninoOption> options)`
Sets the options for all future compilations.
`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`
`setParentClassLoader(ClassLoader parentClassLoader)`
The "parent class loader" is used to load referenced classes.
`void`
`setSourceVersion(int version)`
Controls the language elements that are accepted by the `Parser`.
`void`
`setTargetVersion(int version)`
Controls the .class files that are generated by the `UnitCompiler`.
`void`
`setWarningHandler(WarningHandler warningHandler)`
By default, warnings are discarded, but an application my install a custom `WarningHandler` which is invoked for each warning.

### 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, finalize, getClass, 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

### SimpleCompiler

public SimpleCompiler(@Nullable String fileName, Reader in) throws IOException, CompileException
Equivalent to

         SimpleCompiler sc = new SimpleCompiler();
         sc.cook(fileName, in);

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

### SimpleCompiler

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

         SimpleCompiler sc = new SimpleCompiler();
         sc.cook(fileName, is);

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

### SimpleCompiler

public SimpleCompiler(String fileName) throws IOException, CompileException
Equivalent to

         SimpleCompiler sc = new SimpleCompiler();
         sc.cook(fileName);

Throws:
  `IOException`
  `CompileException`
See Also:
  - `SimpleCompiler()`
  - `Cookable.cookFile(String)`

### SimpleCompiler

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

         SimpleCompiler sc = new SimpleCompiler();
         sc.setParentClassLoader(parentClassLoader);
         sc.cook(scanner);

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

### SimpleCompiler

public SimpleCompiler()

## Method Details

### main

public static void main(String[] args) throws Exception

Throws:
  `Exception`

### setParentClassLoader

public void setParentClassLoader(@Nullable ClassLoader parentClassLoader)
Description copied from interface: `ISimpleCompiler`
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 `ISimpleCompiler`

### setDebuggingInformation

public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Description copied from interface: `ISimpleCompiler`
Determines what kind of debugging information is included in the generates classes. The default is typically "`-g:none`".

Specified by:
  `setDebuggingInformation` in interface `ISimpleCompiler`

### cook

public final void cook(@Nullable String fileName, Reader r) throws CompileException, IOException
Scans, parses and compiles a given compilation unit from the given `Reader`. After completion, `getClassLoader()` returns a `ClassLoader` that allows for access to the compiled classes.

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
Scans, parses and compiles a given compilation unit from the given scanner. After completion, `getClassLoader()` returns a `ClassLoader` that allows for access to the compiled classes.

Throws:
  `CompileException`
  `IOException`

### cook

public void cook(Java.AbstractCompilationUnit abstractCompilationUnit) throws CompileException
Cooks this compilation unit directly.

Throws:
  `CompileException`

### getClassFiles

public ClassFile[] getClassFiles()

Returns:
  The `ClassFile`s that were generated during cooking

### setSourceVersion

public void setSourceVersion(int version)
Controls the language elements that are accepted by the `Parser`.

Specified by:
  `setSourceVersion` in interface `ICookable`
See Also:
  - `Parser.setSourceVersion(int)`

### setTargetVersion

public void setTargetVersion(int version)
Controls the .class files that are generated by the `UnitCompiler`.

Specified by:
  `setTargetVersion` in interface `ICookable`
See Also:
  - `UnitCompiler.setTargetVersion(int)`

### getBytecodes

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

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

### getClassLoader

public ClassLoader getClassLoader()
Description copied from interface: `ISimpleCompiler`
Returns a `ClassLoader` object through which the previously compiled classes can be accessed. This `ClassLoader` can be used for subsequent `ISimpleCompiler`s in order to compile compilation units that use types (e.g. declare derived types) declared in the previous one.

This method must only be called after exactly one of the `ICookable.cook(String, java.io.Reader)` methods was called.

Specified by:
  `getClassLoader` in interface `ISimpleCompiler`

### equals

public boolean equals(@Nullable Object o)
Two `SimpleCompiler`s are regarded equal iff

- Both are objects of the same class (e.g. both are `ScriptEvaluator`s)
- Both generated functionally equal classes as seen by `Object.equals(Object)`

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`

### setCompileErrorHandler

public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface: `ISimpleCompiler`
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 `ISimpleCompiler`
Parameters:
  `compileErrorHandler` - `null` to restore the default behavior (throwing a `CompileException`)

### setWarningHandler

public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface: `ISimpleCompiler`
By default, warnings are discarded, but an application my install a custom `WarningHandler` which is invoked for each warning. If, for some untypical reason, that warning handler wants to terminate the compilation as quickly as possible, then it would throw a `CompileException`.

Specified by:
  `setWarningHandler` in interface `ISimpleCompiler`
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 SimpleCompiler options(EnumSet<JaninoOption> options)
Sets the options for all future compilations.

### optionalClassToType

@Nullable protected Java.Type optionalClassToType(Location location, @Nullable Class<?> clazz)
Wraps a reflection `Class` in a `Java.Type` object.

### classToType

protected Java.Type classToType(Location location, Class<?> clazz)
Wraps a reflection `Class` in a `Java.Type` object.

### classesToTypes

protected Java.Type[] classesToTypes(Location location, @Nullable Class<?>[] classes)
Converts an array of `Class`es into an array of`Java.Type`s.

### compileToClassLoader

protected final ClassLoader compileToClassLoader(Java.AbstractCompilationUnit abstractCompilationUnit) throws CompileException
Compiles the given compilation unit. (A "compilation unit" is typically the contents of a Java source file.)

Parameters:
  `abstractCompilationUnit` - The parsed compilation unit
Returns:
  The `ClassLoader` into which the compiled classes were defined
Throws:
  `CompileException`