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 aSimpleCompilerobject, proceed as described forISimpleCompiler. Alternatively, a number of "convenience constructors" exist that execute the described steps instantly.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<ClassFile>classFilesNullbefore cooking, non-nullafter cooking.private ClassLoaderIClassLoaderclassLoaderIClassLoaderprivate ErrorHandlercompileErrorHandlerprivate booleandebugLinesprivate booleandebugSourceprivate booleandebugVarsprivate static booleandisassembleClassFilesToStdoutprivate java.util.Map<java.lang.String,byte[]>getBytecodesCacheprivate java.lang.ClassLoadergetClassLoaderCacheprivate static java.util.logging.LoggerLOGGERprivate java.util.EnumSet<JaninoOption>optionsprivate java.lang.ClassLoaderparentClassLoaderprivate intsourceVersionprivate inttargetVersionprivate WarningHandlerwarningHandler
-
Constructor Summary
Constructors Constructor Description SimpleCompiler()SimpleCompiler(java.lang.String fileName)Equivalent toSimpleCompiler(java.lang.String fileName, java.io.InputStream is)Equivalent toSimpleCompiler(java.lang.String fileName, java.io.Reader in)Equivalent toSimpleCompiler(Scanner scanner, java.lang.ClassLoader parentClassLoader)Equivalent to
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.util.Collection<ClassFile>assertCooked()private voidassertUncooked()protected Java.Type[]classesToTypes(Location location, java.lang.Class<?>[] classes)Converts an array ofClasses into an array ofJava.Types.protected Java.TypeclassToType(Location location, java.lang.Class<?> clazz)Wraps a reflectionClassin aJava.Typeobject.protected java.lang.ClassLoadercompileToClassLoader(Java.AbstractCompilationUnit abstractCompilationUnit)Compiles the given compilation unit.voidcook(java.lang.String fileName, java.io.Reader r)Scans, parses and compiles a given compilation unit from the givenReader.voidcook(Java.AbstractCompilationUnit abstractCompilationUnit)Cooks this compilation unit directly.voidcook(Scanner scanner)Scans, parses and compiles a given compilation unit from the given scanner.private voidcook2(Scanner scanner)booleanequals(java.lang.Object o)TwoSimpleCompilers are regarded equal iff Both are objects of the same class (e.g.java.util.Map<java.lang.String,byte[]>getBytecodes()private java.util.Map<java.lang.String,byte[]>getBytecodes2()ClassFile[]getClassFiles()java.lang.ClassLoadergetClassLoader()Returns aClassLoaderobject through which the previously compiled classes can be accessed.private java.lang.ClassLoadergetClassLoader2()inthashCode()static voidmain(java.lang.String[] args)protected Java.TypeoptionalClassToType(Location location, java.lang.Class<?> clazz)Wraps a reflectionClassin aJava.Typeobject.java.util.EnumSet<JaninoOption>options()SimpleCompileroptions(java.util.EnumSet<JaninoOption> options)Sets the options for all future compilations.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler)Installs anErrorHandlerwhich is invoked during compilation on each error.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Determines what kind of debugging information is included in the generates classes.voidsetParentClassLoader(java.lang.ClassLoader parentClassLoader)The "parent class loader" is used to load referenced classes.voidsetSourceVersion(int version)Controls the language elements that are accepted by theParser.voidsetTargetVersion(int version)Controls the .class files that are generated by theUnitCompiler.voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install a customWarningHandlerwhich 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
-
-
-
-
Field Detail
-
disassembleClassFilesToStdout
private static final boolean disassembleClassFilesToStdout
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
parentClassLoader
private java.lang.ClassLoader parentClassLoader
-
classLoaderIClassLoader
@Nullable private ClassLoaderIClassLoader classLoaderIClassLoader
-
compileErrorHandler
@Nullable private ErrorHandler compileErrorHandler
-
warningHandler
@Nullable private WarningHandler warningHandler
-
debugSource
private boolean debugSource
-
debugLines
private boolean debugLines
-
debugVars
private boolean debugVars
-
sourceVersion
private int sourceVersion
-
targetVersion
private int targetVersion
-
options
private java.util.EnumSet<JaninoOption> options
-
classFiles
@Nullable private java.util.Collection<ClassFile> classFiles
Nullbefore cooking, non-nullafter cooking.
-
getBytecodesCache
@Nullable private java.util.Map<java.lang.String,byte[]> getBytecodesCache
-
getClassLoaderCache
@Nullable private java.lang.ClassLoader getClassLoaderCache
-
-
Constructor Detail
-
SimpleCompiler
public SimpleCompiler(@Nullable java.lang.String fileName, java.io.Reader in) throws java.io.IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName, in);- Throws:
java.io.IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cook(String, Reader)
-
SimpleCompiler
public SimpleCompiler(@Nullable java.lang.String fileName, java.io.InputStream is) throws java.io.IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName, is);- Throws:
java.io.IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cook(String, InputStream)
-
SimpleCompiler
public SimpleCompiler(java.lang.String fileName) throws java.io.IOException, CompileExceptionEquivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName);- Throws:
java.io.IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cookFile(String)
-
SimpleCompiler
public SimpleCompiler(Scanner scanner, @Nullable java.lang.ClassLoader parentClassLoader) throws java.io.IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.setParentClassLoader(parentClassLoader); sc.cook(scanner);- Throws:
java.io.IOExceptionCompileException- See Also:
SimpleCompiler(),setParentClassLoader(ClassLoader),Cookable.cook(Reader)
-
SimpleCompiler
public SimpleCompiler()
-
-
Method Detail
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception- Throws:
java.lang.Exception
-
setParentClassLoader
public void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader)
Description copied from interface:ISimpleCompilerThe "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()The running JVM's class path Thread.currentThread().getContextClassLoader()ornullThe class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADERThe running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoaderin interfaceISimpleCompiler
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Description copied from interface:ISimpleCompilerDetermines what kind of debugging information is included in the generates classes. The default is typically "-g:none".- Specified by:
setDebuggingInformationin interfaceISimpleCompiler
-
cook
public final void cook(@Nullable java.lang.String fileName, java.io.Reader r) throws CompileException, java.io.IOException
Scans, parses and compiles a given compilation unit from the givenReader. After completion,getClassLoader()returns aClassLoaderthat allows for access to the compiled classes.- Specified by:
cookin interfaceICookable- Specified by:
cookin classCookable- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionjava.io.IOException
-
cook
public void cook(Scanner scanner) throws CompileException, java.io.IOException
Scans, parses and compiles a given compilation unit from the given scanner. After completion,getClassLoader()returns aClassLoaderthat allows for access to the compiled classes.- Throws:
CompileExceptionjava.io.IOException
-
cook2
private void cook2(Scanner scanner) throws CompileException, java.io.IOException
- Throws:
CompileExceptionjava.io.IOException
-
cook
public void cook(Java.AbstractCompilationUnit abstractCompilationUnit) throws CompileException
Cooks this compilation unit directly.- Throws:
CompileException
-
getClassFiles
public ClassFile[] getClassFiles()
- Returns:
- The
ClassFiles that were generated during cooking
-
setSourceVersion
public void setSourceVersion(int version)
Controls the language elements that are accepted by theParser.- Specified by:
setSourceVersionin interfaceICookable- See Also:
Parser.setSourceVersion(int)
-
setTargetVersion
public void setTargetVersion(int version)
Controls the .class files that are generated by theUnitCompiler.- Specified by:
setTargetVersionin interfaceICookable- See Also:
UnitCompiler.setTargetVersion(int)
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
getBytecodes2
private java.util.Map<java.lang.String,byte[]> getBytecodes2()
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
Description copied from interface:ISimpleCompilerReturns aClassLoaderobject through which the previously compiled classes can be accessed. ThisClassLoadercan be used for subsequentISimpleCompilers 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:
getClassLoaderin interfaceISimpleCompiler
-
getClassLoader2
private java.lang.ClassLoader getClassLoader2()
-
equals
public boolean equals(@Nullable java.lang.Object o)
TwoSimpleCompilers are regarded equal iff- Both are objects of the same class (e.g. both are
ScriptEvaluators) - Both generated functionally equal classes as seen by
Object.equals(Object)
- Overrides:
equalsin classjava.lang.Object
- Both are objects of the same class (e.g. both are
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:ISimpleCompilerInstalls anErrorHandlerwhich is invoked during compilation on each error. (By default, the compilation throws aCompileExceptionon the first error and terminates.)If the given
ErrorHandlerthrows aCompileException, then the compilation terminates and the exception is propagated.If the given
ErrorHandlerdoes not throw aCompileExceptionbut 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 aCompileExceptionindicating the number of errors.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compilation will definitely throw aCompileExceptionif one or more compile errors have occurred.- Specified by:
setCompileErrorHandlerin interfaceISimpleCompiler- Parameters:
compileErrorHandler-nullto restore the default behavior (throwing aCompileException)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:ISimpleCompilerBy default, warnings are discarded, but an application my install a customWarningHandlerwhich 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 aCompileException.- Specified by:
setWarningHandlerin interfaceISimpleCompiler- Parameters:
warningHandler-nullto indicate that no warnings be issued
-
options
public java.util.EnumSet<JaninoOption> options()
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
public SimpleCompiler options(java.util.EnumSet<JaninoOption> options)
Sets the options for all future compilations.
-
optionalClassToType
@Nullable protected Java.Type optionalClassToType(Location location, @Nullable java.lang.Class<?> clazz)
Wraps a reflectionClassin aJava.Typeobject.
-
classToType
protected Java.Type classToType(Location location, java.lang.Class<?> clazz)
Wraps a reflectionClassin aJava.Typeobject.
-
classesToTypes
protected Java.Type[] classesToTypes(Location location, @Nullable java.lang.Class<?>[] classes)
Converts an array ofClasses into an array ofJava.Types.
-
compileToClassLoader
protected final java.lang.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
ClassLoaderinto which the compiled classes were defined - Throws:
CompileException
-
assertUncooked
private void assertUncooked()
- Throws:
java.lang.IllegalStateException- This SimpleCompiler is already cooked
-
assertCooked
private java.util.Collection<ClassFile> assertCooked()
- Returns:
- The
ClassFiles that were created when thisSimpleCompilerwasCookable.cook(Reader)ed - Throws:
java.lang.IllegalStateException- This SimpleCompiler is not yet cooked
-
-