Class SimpleCompiler
- java.lang.Object
-
- org.codehaus.commons.compiler.Cookable
-
- org.codehaus.commons.compiler.jdk.SimpleCompiler
-
- All Implemented Interfaces:
ICookable
,ISimpleCompiler
public class SimpleCompiler extends Cookable implements ISimpleCompiler
The JDK-based implementation ofISimpleCompiler
.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<java.lang.String,byte[]>
bytecodes
private ErrorHandler
compileErrorHandler
private boolean
debugLines
private boolean
debugSource
private boolean
debugVars
private java.lang.ClassLoader
getClassLoaderCache
private java.util.SortedSet<Location>
offsets
private java.lang.ClassLoader
parentClassLoader
private int
sourceVersion
private int
targetVersion
private LineAndColumnTracker
tracker
private WarningHandler
warningHandler
-
Constructor Summary
Constructors Constructor Description SimpleCompiler()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
addOffset(java.lang.String fileName)
Derived classes call this method to "reset" the current line and column number at the currently read input character, and also changes the "file name" (seecook(String, Reader)
).private java.util.Map<java.lang.String,byte[]>
assertCooked()
void
cook(java.lang.String fileName, java.io.Reader r)
Reads, scans, parses and compiles Java tokens from the givenReader
.java.util.Map<java.lang.String,byte[]>
getBytecodes()
java.lang.ClassLoader
getClassLoader()
Returns aClassLoader
object through which the previously compiled classes can be accessed.void
setCompileErrorHandler(ErrorHandler compileErrorHandler)
By default,CompileException
s are thrown on compile errors, but an application my install its ownErrorHandler
.void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes.void
setParentClassLoader(java.lang.ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes.void
setParentClassLoader(java.lang.ClassLoader parentClassLoader, java.lang.Class<?>[] auxiliaryClasses)
Deprecated.Auxiliary classes never really worked...void
setSourceVersion(int version)
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option.void
setTargetVersion(int version)
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option.void
setWarningHandler(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
-
-
-
-
Field Detail
-
parentClassLoader
private java.lang.ClassLoader parentClassLoader
-
debugSource
private boolean debugSource
-
debugLines
private boolean debugLines
-
debugVars
private boolean debugVars
-
sourceVersion
private int sourceVersion
-
targetVersion
private int targetVersion
-
compileErrorHandler
@Nullable private ErrorHandler compileErrorHandler
-
warningHandler
@Nullable private WarningHandler warningHandler
-
bytecodes
@Nullable java.util.Map<java.lang.String,byte[]> bytecodes
-
tracker
private final LineAndColumnTracker tracker
-
offsets
private final java.util.SortedSet<Location> offsets
-
getClassLoaderCache
@Nullable private java.lang.ClassLoader getClassLoaderCache
-
-
Method Detail
-
setSourceVersion
public void setSourceVersion(int version)
Description copied from interface:ICookable
Specifies the version of source code accepted, in analogy with JAVAC's-source
command line option. May be ignored by an implementation (e.g. thejanino
implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1
means to use a default version.- Specified by:
setSourceVersion
in interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version)
Description copied from interface:ICookable
Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command 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.-1
means to use a default version.- Specified by:
setTargetVersion
in interfaceICookable
-
getBytecodes
public java.util.Map<java.lang.String,byte[]> getBytecodes()
- Specified by:
getBytecodes
in interfaceICookable
- Returns:
- The generated Java bytecode; maps class name to bytes
-
assertCooked
private java.util.Map<java.lang.String,byte[]> assertCooked()
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
Description copied from interface:ISimpleCompiler
Returns aClassLoader
object through which the previously compiled classes can be accessed. ThisClassLoader
can be used for subsequentISimpleCompiler
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 interfaceISimpleCompiler
-
cook
public void cook(@Nullable java.lang.String fileName, java.io.Reader r) throws CompileException, java.io.IOException
Description copied from interface:ICookable
Reads, scans, parses and compiles Java tokens from the givenReader
.- Specified by:
cook
in interfaceICookable
- Specified by:
cook
in classCookable
- Parameters:
fileName
- Used when reporting errors and warnings- Throws:
CompileException
java.io.IOException
-
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 interfaceISimpleCompiler
-
setParentClassLoader
public void setParentClassLoader(@Nullable java.lang.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()
ornull
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 interfaceISimpleCompiler
-
setParentClassLoader
@Deprecated public void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader, java.lang.Class<?>[] auxiliaryClasses)
Deprecated.Auxiliary classes never really worked... don't use them.
-
setCompileErrorHandler
public void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Description copied from interface:ISimpleCompiler
By default,CompileException
s 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
ErrorHandler
counts errors and throws aCompileException
when a limit is reached.If the given
ErrorHandler
throwsCompileException
s, then the compilation is terminated and the exception is propagated.If the given
ErrorHandler
does not throwCompileException
s, then the compiler may or may not continue compilation, but must eventually throw aCompileException
.In other words: The
ErrorHandler
may throw aCompileException
or not, but the compiler must definitely throw aCompileException
if one or more compile errors have occurred.- Specified by:
setCompileErrorHandler
in interfaceISimpleCompiler
- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
Description copied from interface:ISimpleCompiler
By default, warnings are discarded, but an application my install a customWarningHandler
.- Specified by:
setWarningHandler
in interfaceISimpleCompiler
- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
addOffset
protected void addOffset(@Nullable java.lang.String fileName)
Derived classes call this method to "reset" the current line and column number at the currently read input character, and also changes the "file name" (seecook(String, Reader)
).
-
-