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.
-
-
Constructor Summary
Constructors Constructor Description SimpleCompiler()SimpleCompiler(String fileName)Equivalent toSimpleCompiler(String fileName, InputStream is)Equivalent toSimpleCompiler(String fileName, Reader in)Equivalent toSimpleCompiler(Scanner scanner, ClassLoader parentClassLoader)Equivalent to
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Java.Type[]classesToTypes(Location location, Class<?>[] classes)protected Java.TypeclassToType(Location location, Class<?> clazz)protected ClassLoadercompileToClassLoader(Java.AbstractCompilationUnit abstractCompilationUnit)Compiles the given compilation unit.voidcook(String fileName, 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.booleanequals(Object o)TwoSimpleCompilers are regarded equal iff Both are objects of the same class (e.g.Map<String,byte[]>getBytecodes()ClassFile[]getClassFiles()ClassLoadergetClassLoader()Returns aClassLoaderobject through which the previously compiled classes can be accessed.inthashCode()static voidmain(String[] args)protected Java.TypeoptionalClassToType(Location location, Class<?> clazz)EnumSet<JaninoOption>options()SimpleCompileroptions(EnumSet<JaninoOption> options)Sets the options for all future compilations.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler)By default,CompileExceptions are thrown on compile errors, but an application my install its ownErrorHandler.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)Determines what kind of debugging information is included in the generates classes.voidsetParentClassLoader(ClassLoader parentClassLoader)The "parent class loader" is used to load referenced classes.voidsetSourceVersion(int version)JANINO ignores any set "source version", and throws "NYI"CompileExceptionwhen a language element is used which is not supported.voidsetTargetVersion(int version)Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install a customWarningHandler.-
Methods inherited from class org.codehaus.commons.compiler.Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
-
-
-
Constructor Detail
-
SimpleCompiler
public SimpleCompiler(@Nullable String fileName, Reader in) throws IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName, in);- Throws:
IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cook(String, Reader)
-
SimpleCompiler
public SimpleCompiler(@Nullable String fileName, InputStream is) throws IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName, is);- Throws:
IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cook(String, InputStream)
-
SimpleCompiler
public SimpleCompiler(String fileName) throws IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.cook(fileName);- Throws:
IOExceptionCompileException- See Also:
SimpleCompiler(),Cookable.cookFile(String)
-
SimpleCompiler
public SimpleCompiler(Scanner scanner, @Nullable ClassLoader parentClassLoader) throws IOException, CompileException
Equivalent toSimpleCompiler sc = new SimpleCompiler(); sc.setParentClassLoader(parentClassLoader); sc.cook(scanner);
-
SimpleCompiler
public SimpleCompiler()
-
-
Method Detail
-
setParentClassLoader
public void setParentClassLoader(@Nullable 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 String fileName, Reader r) throws CompileException, 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:
CompileExceptionIOException
-
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 aClassLoaderthat allows for access to the compiled classes.- Throws:
CompileExceptionIOException
-
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)
JANINO ignores any set "source version", and throws "NYI"CompileExceptionwhen a language element is used which is not supported.- Specified by:
setSourceVersionin interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookableGenerates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand 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.-1means to use a default version.- Specified by:
setTargetVersionin interfaceICookable
-
getBytecodes
public Map<String,byte[]> getBytecodes()
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
getClassLoader
public 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
-
equals
public boolean equals(@Nullable 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)
- Both are objects of the same class (e.g. both are
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:ISimpleCompilerBy default,CompileExceptions are thrown on compile errors, but an application my install its ownErrorHandler.Be aware that a single problem during compilation often causes a bunch of compile errors, so a good
ErrorHandlercounts errors and throws aCompileExceptionwhen a limit is reached.If the given
ErrorHandlerthrowsCompileExceptions, then the compilation is terminated and the exception is propagated.If the given
ErrorHandlerdoes not throwCompileExceptions, then the compiler may or may not continue compilation, but must eventually throw aCompileException.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compiler must 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 customWarningHandler.- Specified by:
setWarningHandlerin interfaceISimpleCompiler- Parameters:
warningHandler-nullto 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)
-
classesToTypes
protected Java.Type[] classesToTypes(Location location, @Nullable Class<?>[] classes)
-
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
ClassLoaderinto which the compiled classes were defined - Throws:
CompileException
-
-