Top Description Methods
org.codehaus.commons.compiler

public Interface ISimpleCompiler

extends ICookable
Imports
java.io.Reader, org.codehaus.commons.compiler.lang.ClassLoaders, org.codehaus.commons.nullanalysis.Nullable

A simplified Java compiler that can compile only a single compilation unit. (A "compilation unit" is the document stored in a ".java" file.)

Opposed to a normal ".java" file, you can declare multiple public classes here.

To set up an ISimpleCompiler object, proceed as follows:

  1. Create an ISimpleCompiler-implementing object
  2. Optionally set an alternate parent class loader through setParentClassLoader(ClassLoader).
  3. Call any of the ICookable#cook(String, Reader) methods to scan, parse, compile and load the compilation unit into the JVM.
  4. Call getClassLoader() to obtain a ClassLoader that you can use to access the compiled classes.

Comptibility notice:

The methods setPermissions(Permissions permissions) and void setNoPermissions() were removed in version 3.1.1 (2020-03-09) and replaced by the Sandbox.

Method Summary

Modifier and TypeMethod and Description
public ClassLoader
getClassLoader()

Returns a ClassLoader object through which the previously compiled classes can be accessed.

public void
setCompileErrorHandler(ErrorHandler
null to restore the default behavior (throwing a CompileException)
compileErrorHandler
)

Installs an ErrorHandler which is invoked during compilation on each error.

public void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)

Determines what kind of debugging information is included in the generates classes.

public void
setParentClassLoader(ClassLoader parentClassLoader)

The "parent class loader" is used to load referenced classes.

public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

By default, warnings are discarded, but an application my install a custom WarningHandler which is invoked for each warning.

Inherited from org.codehaus.commons.compiler.ICookable:
cookcookcookcookcookcookcookcookcookFilecookFilecookFilecookFilegetBytecodessetSourceVersionsetTargetVersion

Method Detail

getClassLoaderback to summary
public ClassLoader getClassLoader()

Returns a ClassLoader object through which the previously compiled classes can be accessed. This ClassLoader can be used for subsequent ISimpleCompilers 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 cook(String, java.io.Reader) methods was called.

setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler compileErrorHandler)

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)

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

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

setParentClassLoaderback to summary
public void setParentClassLoader(ClassLoader parentClassLoader)

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.

setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)

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.

Parameters
warningHandler:WarningHandler

null to indicate that no warnings be issued