Package org.codehaus.groovy.control
Class CompilationUnit
java.lang.Object
org.codehaus.groovy.control.ProcessingUnit
org.codehaus.groovy.control.CompilationUnit
- Direct Known Subclasses:
JavaAwareCompilationUnit
,JavaStubCompilationUnit
The CompilationUnit collects all compilation data as it is generated by the compiler system.
You can use this object to add additional source units to the compilation, or force the
compilation to be run again (to affect only the deltas).
You can also add PhaseOperations to this compilation using the addPhaseOperation method. This is commonly used when you want to wire a new AST Transformation into the compilation.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A callback interface you can use during theclassgen
phase of compilation as the compiler traverses the ClassNode tree.static class
Deprecated.static interface
static interface
static interface
static class
Deprecated.static interface
A callback interface you can use to get a callback after every unit of the compile process.static class
Deprecated. -
Field Summary
Modifier and TypeFieldDescriptionprotected CompileUnit
The overall AST for this CompilationUnit.protected ASTTransformationsContext
The AST transformations state data.protected CompilationUnit.ClassgenCallback
A callback called during theclassgen
phase of compilationprotected ClassNodeResolver
protected boolean
True after the firstconfigure(CompilerConfiguration)
operation.protected boolean
If set, outputs a little more information during compilation when errors occur.protected CompilationUnit.ProgressCallback
A callback for use duringcompile()
protected Queue<SourceUnit>
protected ResolveVisitor
protected Map<String,
SourceUnit> The source units from which this unit is built.Fields inherited from class org.codehaus.groovy.control.ProcessingUnit
classLoader, configuration, errorCollector, phase, phaseComplete
-
Constructor Summary
ConstructorDescriptionInitializes the CompilationUnit with defaults.CompilationUnit
(GroovyClassLoader loader) Initializes the CompilationUnit with defaults except for class loader.CompilationUnit
(CompilerConfiguration configuration) Initializes the CompilationUnit with no security considerations.CompilationUnit
(CompilerConfiguration configuration, CodeSource codeSource, GroovyClassLoader loader) Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.CompilationUnit
(CompilerConfiguration configuration, CodeSource codeSource, GroovyClassLoader loader, GroovyClassLoader transformLoader) Initializes the CompilationUnit with a CodeSource for controlling security stuff, a class loader for loading classes, and a class loader for loading AST transformations. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClassNode
(ClassNode node) Adds a ClassNode directly to the unit (i.e.void
addFirstPhaseOperation
(CompilationUnit.IPrimaryClassNodeOperation op, int phase) void
addFirstPhaseOperation
(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated.void
addJavaCompilationUnits
(Set<JavaFileObject> javaCompilationUnitSet) void
addNewPhaseOperation
(CompilationUnit.ISourceUnitOperation op, int phase) void
addNewPhaseOperation
(CompilationUnit.SourceUnitOperation op, int phase) Deprecated.void
Deprecated.void
void
addPhaseOperation
(CompilationUnit.IPrimaryClassNodeOperation op, int phase) void
addPhaseOperation
(CompilationUnit.ISourceUnitOperation op, int phase) void
addPhaseOperation
(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated.void
addPhaseOperation
(CompilationUnit.SourceUnitOperation op, int phase) Deprecated.Adds a source file to the unit.addSource
(String name, InputStream stream) Adds a InputStream source to the unit.Adds a source file to the unit.addSource
(SourceUnit source) Adds a SourceUnit to the unit.void
addSources
(File[] files) Adds a set of source files to the unit.void
addSources
(String[] paths) Adds a set of file paths to the unit.void
Deprecated.void
Deprecated.void
compile()
Synonym forcompile(Phases.ALL)
.void
compile
(int throughPhase) Compiles the compilation unit from sources.void
configure
(CompilerConfiguration configuration) Configures its debugging mode and classloader classpath from a given compiler configuration.protected org.objectweb.asm.ClassVisitor
protected boolean
dequeued()
Dequeues any source units added through addSource and resets the compiler phase to initialization.getAST()
Returns the CompileUnit that roots our AST.Get the GroovyClasses generated by compile().getClassNode
(String name) Convenience routine to get the named ClassNode.Convenience routine to get the first ClassNode, for when you are sure there is only one.Returns the class loader for loading AST transformations.iterator()
Returns an iterator on the unit's SourceUnits.protected void
mark()
Updates the phase marker on all sources.void
Sets a ClassgenCallback.void
setClassNodeResolver
(ClassNodeResolver classNodeResolver) void
Sets a ProgressCallback.Methods inherited from class org.codehaus.groovy.control.ProcessingUnit
completePhase, getClassLoader, getConfiguration, getErrorCollector, getPhase, getPhaseDescription, gotoPhase, isPhaseComplete, nextPhase, setClassLoader, setConfiguration
-
Field Details
-
ast
The overall AST for this CompilationUnit. -
sources
The source units from which this unit is built. -
queuedSources
-
debug
protected boolean debugIf set, outputs a little more information during compilation when errors occur. -
configured
protected boolean configuredTrue after the firstconfigure(CompilerConfiguration)
operation. -
classgenCallback
A callback called during theclassgen
phase of compilation -
progressCallback
A callback for use duringcompile()
-
classNodeResolver
-
resolveVisitor
-
astTransformationsContext
The AST transformations state data.
-
-
Constructor Details
-
CompilationUnit
public CompilationUnit()Initializes the CompilationUnit with defaults. -
CompilationUnit
Initializes the CompilationUnit with defaults except for class loader. -
CompilationUnit
Initializes the CompilationUnit with no security considerations. -
CompilationUnit
public CompilationUnit(CompilerConfiguration configuration, CodeSource codeSource, GroovyClassLoader loader) Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes. -
CompilationUnit
public CompilationUnit(CompilerConfiguration configuration, CodeSource codeSource, GroovyClassLoader loader, GroovyClassLoader transformLoader) Initializes the CompilationUnit with a CodeSource for controlling security stuff, a class loader for loading classes, and a class loader for loading AST transformations.Note: The transform loader must be able to load compiler classes. That means
ProcessingUnit.classLoader
must be at last a parent totransformLoader
. The other loader has no such constraint.- Parameters:
transformLoader
- - the loader for transformsloader
- - loader used to resolve classes against during compilationcodeSource
- - security setting for the compilationconfiguration
- - compilation configuration
-
-
Method Details
-
addPhaseOperation
-
addPhaseOperation
-
addPhaseOperation
-
addFirstPhaseOperation
-
addNewPhaseOperation
-
configure
Configures its debugging mode and classloader classpath from a given compiler configuration. This cannot be done more than once due to limitations inURLClassLoader
.- Overrides:
configure
in classProcessingUnit
-
getAST
Returns the CompileUnit that roots our AST. -
getClasses
Get the GroovyClasses generated by compile(). -
getFirstClassNode
Convenience routine to get the first ClassNode, for when you are sure there is only one. -
getClassNode
Convenience routine to get the named ClassNode. -
getASTTransformationsContext
- Returns:
- the AST transformations current context
-
getClassNodeResolver
-
setClassNodeResolver
-
getJavaCompilationUnitSet
-
addJavaCompilationUnits
-
getTransformLoader
Returns the class loader for loading AST transformations. -
addSources
Adds a set of file paths to the unit. -
addSources
Adds a set of source files to the unit. -
addSource
Adds a source file to the unit. -
addSource
Adds a source file to the unit. -
addSource
Adds a InputStream source to the unit. -
addSource
-
addSource
Adds a SourceUnit to the unit. -
iterator
Returns an iterator on the unit's SourceUnits. -
addClassNode
Adds a ClassNode directly to the unit (i.e. without source). WARNING: the source is needed for error reporting, using this method without setting a SourceUnit will cause NullPointerExceptions -
getClassgenCallback
-
setClassgenCallback
Sets a ClassgenCallback. You can have only one, and setting it tonull
removes any existing setting. -
getProgressCallback
-
setProgressCallback
Sets a ProgressCallback. You can have only one, and setting it tonull
removes any existing setting. -
compile
Synonym forcompile(Phases.ALL)
.- Throws:
CompilationFailedException
-
compile
Compiles the compilation unit from sources.- Throws:
CompilationFailedException
-
dequeued
Dequeues any source units added through addSource and resets the compiler phase to initialization.Note: this does not mean a file is recompiled. If a SourceUnit has already passed a phase it is skipped until a higher phase is reached.
- Returns:
- true if there was a queued source
- Throws:
CompilationFailedException
-
createClassVisitor
protected org.objectweb.asm.ClassVisitor createClassVisitor() -
mark
Updates the phase marker on all sources.- Throws:
CompilationFailedException
-
addPhaseOperation
Deprecated. -
addPhaseOperation
Deprecated. -
addPhaseOperation
Deprecated. -
addFirstPhaseOperation
@Deprecated public void addFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated. -
addNewPhaseOperation
Deprecated. -
applyToSourceUnits
@Deprecated public void applyToSourceUnits(CompilationUnit.SourceUnitOperation op) throws CompilationFailedException Deprecated.- Throws:
CompilationFailedException
-
applyToPrimaryClassNodes
@Deprecated public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation op) throws CompilationFailedException Deprecated.- Throws:
CompilationFailedException
-