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.
- Author:
- Chris Poirier, Jochen Theodorou, Roshan Dawrani
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A callback interface you can use to "accompany" the classgen() code as it traverses the ClassNode tree.static class
static class
An callback interface for use in the applyToPrimaryClassNodes loop driver.static class
A callback interface you can use to get a callback after every unit of the compile process.static class
An callback interface for use in the applyToSourceUnits loop driver. -
Field Summary
Modifier and TypeFieldDescriptionprotected CompileUnit
protected ASTTransformationsContext
protected CompilationUnit.ClassgenCallback
protected ClassNodeResolver
protected Map
protected boolean
protected boolean
protected List<GroovyClass>
protected OptimizerVisitor
protected CompilationUnit.ProgressCallback
protected LinkedList<SourceUnit>
protected ResolveVisitor
protected Map<String,
SourceUnit> protected StaticImportVisitor
protected Map
protected Map
protected Verifier
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 security, GroovyClassLoader loader) Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.CompilationUnit
(CompilerConfiguration configuration, CodeSource security, 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 (ie.void
addFirstPhaseOperation
(CompilationUnit.PrimaryClassNodeOperation op, int phase) void
addNewPhaseOperation
(CompilationUnit.SourceUnitOperation op, int phase) void
void
addPhaseOperation
(CompilationUnit.PrimaryClassNodeOperation op, int phase) void
addPhaseOperation
(CompilationUnit.SourceUnitOperation op, int phase) 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
void
A loop driver for applying operations to all primary ClassNodes in our AST.void
A loop driver for applying operations to all SourceUnits.void
compile()
Synonym for compile(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 add 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.Get the source summariesReturns the class loader for loading AST transformations.boolean
isPublicClass
(String className) 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, nextPhase, setClassLoader, setConfiguration
-
Field Details
-
astTransformationsContext
-
sources
-
summariesBySourceName
-
summariesByPublicClassName
-
classSourcesByPublicClassName
-
names
-
queuedSources
-
ast
-
generatedClasses
-
verifier
-
debug
protected boolean debug -
configured
protected boolean configured -
classgenCallback
-
progressCallback
-
resolveVisitor
-
staticImportVisitor
-
optimizer
-
classNodeResolver
-
-
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 security, 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 security, 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 CompilationUnit.class.classLoader must be at last a parent to transformLoader. The other loader has no such constraint.- Parameters:
transformLoader
- - the loader for transformsloader
- - loader used to resolve classes against during compilationsecurity
- - security setting for the compilationconfiguration
- - compilation configuration
-
-
Method Details
-
getTransformLoader
Returns the class loader for loading AST transformations.- Returns:
- - the transform class loader
-
addPhaseOperation
-
addPhaseOperation
-
addFirstPhaseOperation
-
addPhaseOperation
-
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. -
getSummariesBySourceName
Get the source summaries -
getSummariesByPublicClassName
-
getClassSourcesByPublicClassName
-
isPublicClass
-
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
-
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 (ie. without source). WARNING: the source is needed for error reporting, using this method without setting a SourceUnit will cause NullPinterExceptions -
setClassgenCallback
Sets a ClassgenCallback. You can have only one, and setting it to null removes any existing setting. -
setProgressCallback
Sets a ProgressCallback. You can have only one, and setting it to null removes any existing setting. -
getClassgenCallback
-
getProgressCallback
-
compile
Synonym for compile(Phases.ALL).- Throws:
CompilationFailedException
-
compile
Compiles the compilation unit from sources.- Throws:
CompilationFailedException
-
dequeued
Dequeues any source units add 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
-
applyToSourceUnits
public void applyToSourceUnits(CompilationUnit.SourceUnitOperation body) throws CompilationFailedException A loop driver for applying operations to all SourceUnits. Automatically skips units that have already been processed through the current phase.- Throws:
CompilationFailedException
-
applyToPrimaryClassNodes
public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation body) throws CompilationFailedException A loop driver for applying operations to all primary ClassNodes in our AST. Automatically skips units that have already been processed through the current phase.- Throws:
CompilationFailedException
-
applyToGeneratedGroovyClasses
public void applyToGeneratedGroovyClasses(CompilationUnit.GroovyClassOperation body) throws CompilationFailedException - Throws:
CompilationFailedException
-
getClassNodeResolver
-
setClassNodeResolver
-