Top Description Fields Constructors Methods
org.codehaus.janino

public Class SimpleCompiler

extends Cookable
implements ISimpleCompiler
Class Inheritance
All Implemented Interfaces
org.codehaus.commons.compiler.ISimpleCompiler, org.codehaus.commons.compiler.ICookable
Imports
java.io.FileInputStream, .IOException, .InputStream, .Reader, java.lang.reflect.Method, java.security.AccessController, .PrivilegedAction, java.util.ArrayList, .Collection, .Collections, .EnumSet, .HashMap, .Map, java.util.logging.Logger, org.codehaus.commons.compiler.CompileException, .Cookable, .ErrorHandler, .ISimpleCompiler, .InternalCompilerException, .Location, .WarningHandler, org.codehaus.commons.compiler.util.Disassembler, .SystemProperties, org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader, org.codehaus.commons.nullanalysis.Nullable, org.codehaus.janino.Java.AbstractCompilationUnit, .UnitCompiler.ClassFileConsumer, .Visitor.AtomVisitor, .Visitor.TypeVisitor, org.codehaus.janino.util.ClassFile

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

Field Summary

Modifier and TypeField and Description
private Collection<ClassFile>
classFiles

Null before cooking, non-null after cooking.

private ClassLoaderIClassLoader
private ErrorHandler
private boolean
private boolean
private boolean
private static final boolean
private Map<String, byte[]>
private ClassLoader
private static final Logger
private EnumSet<JaninoOption>
private ClassLoader
private int
private int
private WarningHandler

Constructor Summary

AccessConstructor and Description
public
SimpleCompiler(String fileName, Reader in)

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.cook(fileName, in);
public
SimpleCompiler(String fileName, InputStream is)

Equivalent to

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

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.cook(fileName);
public
SimpleCompiler(Scanner scanner, ClassLoader parentClassLoader)

Equivalent to

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

Method Summary

Modifier and TypeMethod and Description
private Collection<ClassFile>

Returns:

The ClassFiles that were created when this SimpleCompiler was cook(Reader)ed
assertCooked
()

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

Converts an array of Classes into an array ofJava.Types.

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

Wraps a reflection Class in a Java.Type object.

protected final ClassLoader

Returns:

The ClassLoader into which the compiled classes were defined
compileToClassLoader
(Java.AbstractCompilationUnit
The parsed compilation unit
abstractCompilationUnit
)

Compiles the given compilation unit.

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.

Scans, parses and compiles a given compilation unit from the given Reader.
public void
cook(Scanner scanner)

Scans, parses and compiles a given compilation unit from the given scanner.

public void
cook(Java.AbstractCompilationUnit abstractCompilationUnit)

Cooks this compilation unit directly.

private void
cook2(Scanner scanner)

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Two SimpleCompilers are regarded equal iff
public Map<String, byte[]>
private Map<String, byte[]>
public ClassFile[]

Returns:

The ClassFiles that were generated during cooking
getClassFiles
()

public ClassLoader
getClassLoader()

Implements org.codehaus.commons.compiler.ISimpleCompiler.getClassLoader.

Returns a ClassLoader object through which the previously compiled classes can be accessed.
private ClassLoader
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public static void
main(String[] args)

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

Wraps a reflection Class in a Java.Type object.

public EnumSet<JaninoOption>

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.

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

Implements org.codehaus.commons.compiler.ISimpleCompiler.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.ISimpleCompiler.setDebuggingInformation.

Determines what kind of debugging information is included in the generates classes.
public void
setParentClassLoader(ClassLoader parentClassLoader)

Implements org.codehaus.commons.compiler.ISimpleCompiler.setParentClassLoader.

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

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

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

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

Controls the .class files that are generated by the UnitCompiler.
public void
setWarningHandler(WarningHandler
null to indicate that no warnings be issued
warningHandler
)

Implements org.codehaus.commons.compiler.ISimpleCompiler.setWarningHandler.

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.Cookable:
cookcookcookcookcookcookcookcookFilecookFilecookFilecookFile

Field Detail

classFilesback to summary
private Collection<ClassFile> classFiles

Null before cooking, non-null after cooking.

Annotations
@Nullable
classLoaderIClassLoaderback to summary
private ClassLoaderIClassLoader classLoaderIClassLoader
Annotations
@Nullable
compileErrorHandlerback to summary
private ErrorHandler compileErrorHandler
Annotations
@Nullable
debugLinesback to summary
private boolean debugLines
debugSourceback to summary
private boolean debugSource
debugVarsback to summary
private boolean debugVars
disassembleClassFilesToStdoutback to summary
private static final boolean disassembleClassFilesToStdout
getBytecodesCacheback to summary
private Map<String, byte[]> getBytecodesCache
Annotations
@Nullable
getClassLoaderCacheback to summary
private ClassLoader getClassLoaderCache
Annotations
@Nullable
LOGGERback to summary
private static final Logger LOGGER
optionsback to summary
private EnumSet<JaninoOption> options
parentClassLoaderback to summary
private ClassLoader parentClassLoader
sourceVersionback to summary
private int sourceVersion
targetVersionback to summary
private int targetVersion
warningHandlerback to summary
private WarningHandler warningHandler
Annotations
@Nullable

Constructor Detail

SimpleCompilerback to summary
public SimpleCompiler(String fileName, Reader in) throws IOException, CompileException

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.cook(fileName, in);
See Also
SimpleCompiler(), Cookable#cook(String, Reader)
SimpleCompilerback to summary
public SimpleCompiler(String fileName, InputStream is) throws IOException, CompileException

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.cook(fileName, is);
See Also
SimpleCompiler(), Cookable#cook(String, InputStream)
SimpleCompilerback to summary
public SimpleCompiler(String fileName) throws IOException, CompileException

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.cook(fileName);
See Also
SimpleCompiler(), Cookable#cookFile(String)
SimpleCompilerback to summary
public SimpleCompiler(Scanner scanner, ClassLoader parentClassLoader) throws IOException, CompileException

Equivalent to

    SimpleCompiler sc = new SimpleCompiler();
    sc.setParentClassLoader(parentClassLoader);
    sc.cook(scanner);
See Also
SimpleCompiler(), setParentClassLoader(ClassLoader), Cookable#cook(Reader)
SimpleCompilerback to summary
public SimpleCompiler()

Method Detail

assertCookedback to summary
private Collection<ClassFile> assertCooked()
Returns:Collection<ClassFile>

The ClassFiles that were created when this SimpleCompiler was cook(Reader)ed

Exceptions
IllegalStateException:
This SimpleCompiler is not yet cooked
assertUncookedback to summary
private void assertUncooked()
Exceptions
IllegalStateException:
This SimpleCompiler is already cooked
classesToTypesback to summary
protected Java.Type[] classesToTypes(Location location, Class<?>[] classes)

Converts an array of Classes into an array ofJava.Types.

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

Wraps a reflection Class in a Java.Type object.

compileToClassLoaderback to summary
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:Java.AbstractCompilationUnit

The parsed compilation unit

Returns:ClassLoader

The ClassLoader into which the compiled classes were defined

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.

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.

Parameters
fileName:String

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

Used when reporting errors and warnings

Annotations
@Override
cookback to summary
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.

cookback to summary
public void cook(Java.AbstractCompilationUnit abstractCompilationUnit) throws CompileException

Cooks this compilation unit directly.

cook2back to summary
private void cook2(Scanner scanner) throws CompileException, IOException
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Two SimpleCompilers are regarded equal iff

Parameters
o:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

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
getBytecodes2back to summary
private Map<String, byte[]> getBytecodes2()
getClassFilesback to summary
public ClassFile[] getClassFiles()
Returns:ClassFile[]

The ClassFiles that were generated during cooking

getClassLoaderback to summary
public ClassLoader getClassLoader()

Implements org.codehaus.commons.compiler.ISimpleCompiler.getClassLoader.

Doc from org.codehaus.commons.compiler.ISimpleCompiler.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.

Annotations
@Override
getClassLoader2back to summary
private ClassLoader getClassLoader2()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
mainback to summary
public static void main(String[] args) throws Exception
optionalClassToTypeback to summary
protected Java.Type optionalClassToType(final Location location, final Class<?> clazz)

Wraps a reflection Class in a Java.Type object.

Annotations
@Nullable
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 SimpleCompiler options(EnumSet<JaninoOption> options)

Sets the options for all future compilations.

setCompileErrorHandlerback to summary
public void setCompileErrorHandler(ErrorHandler compileErrorHandler)

Implements org.codehaus.commons.compiler.ISimpleCompiler.setCompileErrorHandler.

Doc from org.codehaus.commons.compiler.ISimpleCompiler.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.ISimpleCompiler.setDebuggingInformation.

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

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

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

Implements org.codehaus.commons.compiler.ISimpleCompiler.setParentClassLoader.

Doc from org.codehaus.commons.compiler.ISimpleCompiler.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.

Controls the language elements that are accepted by the Parser.

Annotations
@Override
See Also
Parser#setSourceVersion(int)
setTargetVersionback to summary
public void setTargetVersion(int version)

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

Controls the .class files that are generated by the UnitCompiler.

Annotations
@Override
See Also
UnitCompiler#setTargetVersion(int)
setWarningHandlerback to summary
public void setWarningHandler(WarningHandler warningHandler)

Implements org.codehaus.commons.compiler.ISimpleCompiler.setWarningHandler.

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

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

Annotations
@Override