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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA callback interface you can use during theclassgenphase of compilation as the compiler traverses the ClassNode tree.static classDeprecated.static interfacePerforms an operation for each generatedGroovyClass.static interfacePerforms an operation for each primary class node in the compilation.static interfacePerforms an operation for each source unit in the compilation.static classDeprecated.static interfaceA callback interface you can use to get a callback after every unit of the compile process.static classDeprecated. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CompileUnitThe overall AST for this CompilationUnit.protected ASTTransformationsContextThe AST transformations state data.protected CompilationUnit.ClassgenCallbackA callback called during theclassgenphase of compilationprotected ClassNodeResolverprotected booleanTrue after the firstconfigure(CompilerConfiguration)operation.protected booleanIf set, outputs a little more information during compilation when errors occur.protected CompilationUnit.ProgressCallbackA callback for use duringcompile()protected Queue<SourceUnit>protected ResolveVisitorprotected 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
ConstructorsConstructorDescriptionInitializes 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 TypeMethodDescriptionvoidaddClassNode(ClassNode node) Adds a ClassNode directly to the unit (i.e.voidaddFirstPhaseOperation(CompilationUnit.IPrimaryClassNodeOperation op, int phase) Adds a primary-class operation at the front of the supplied phase queue.voidaddFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated.voidaddJavaCompilationUnits(Set<JavaFileObject> javaCompilationUnitSet) Adds Java compilation units to the joint-compilation set.voidaddNewPhaseOperation(CompilationUnit.ISourceUnitOperation op, int phase) Adds a source-unit operation that becomes active only after the current phase queue drains.voidaddNewPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase) Deprecated.voidDeprecated.voidAdds a Groovy class operation to the output phase.voidaddPhaseOperation(CompilationUnit.IPrimaryClassNodeOperation op, int phase) Adds a primary-class operation to the supplied phase.voidaddPhaseOperation(CompilationUnit.ISourceUnitOperation op, int phase) Adds a source-unit operation to the supplied phase.voidaddPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated.voidaddPhaseOperation(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 an in-memory script source to this compilation unit.Adds a source file to the unit.addSource(SourceUnit source) Adds a SourceUnit to the unit.voidaddSources(File[] files) Adds a set of source files to the unit.voidaddSources(String[] paths) Adds a set of file paths to the unit.voidDeprecated.voidDeprecated.voidcompile()Synonym forcompile(Phases.ALL).voidcompile(int throughPhase) Compiles the compilation unit from sources.voidconfigure(CompilerConfiguration configuration) Configures its debugging mode and classloader classpath from a given compiler configuration.protected org.objectweb.asm.ClassVisitorCreates the ASM class visitor used for bytecode generation.protected booleandequeued()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().Returns the callback invoked after class generation.getClassNode(String name) Convenience routine to get the named ClassNode.Returns the resolver used for on-demand class lookup.Convenience routine to get the first ClassNode, for when you are sure there is only one.Returns the queued Java compilation units used for joint compilation.Returns the callback invoked after each phase transition.Returns the class loader for loading AST transformations.iterator()Returns an iterator on the unit's SourceUnits.protected voidmark()Updates the phase marker on all sources.voidSets a ClassgenCallback.voidsetClassNodeResolver(ClassNodeResolver classNodeResolver) Replaces the resolver used for on-demand class lookup.voidSets 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 theclassgenphase 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.classLoadermust 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
Adds a Groovy class operation to the output phase.- Parameters:
op- the operation to run for generated classes
-
addPhaseOperation
Adds a source-unit operation to the supplied phase.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addPhaseOperation
Adds a primary-class operation to the supplied phase.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addFirstPhaseOperation
Adds a primary-class operation at the front of the supplied phase queue.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addNewPhaseOperation
Adds a source-unit operation that becomes active only after the current phase queue drains.- Parameters:
op- the operation to addphase- the phase in which to run it
-
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:
configurein 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
Returns the resolver used for on-demand class lookup.- Returns:
- the active class-node resolver
-
setClassNodeResolver
Replaces the resolver used for on-demand class lookup.- Parameters:
classNodeResolver- the resolver to install
-
getJavaCompilationUnitSet
Returns the queued Java compilation units used for joint compilation.- Returns:
- the tracked Java compilation units
-
addJavaCompilationUnits
Adds Java compilation units to the joint-compilation set.- Parameters:
javaCompilationUnitSet- the Java units to add
-
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
Adds an in-memory script source to this compilation unit.- Parameters:
name- the source namescriptText- the script text- Returns:
- the created source unit
-
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
Returns the callback invoked after class generation.- Returns:
- the class-generation callback, or
null
-
setClassgenCallback
Sets a ClassgenCallback. You can have only one, and setting it tonullremoves any existing setting. -
getProgressCallback
Returns the callback invoked after each phase transition.- Returns:
- the progress callback, or
null
-
setProgressCallback
Sets a ProgressCallback. You can have only one, and setting it tonullremoves 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()Creates the ASM class visitor used for bytecode generation.- Returns:
- the class visitor used to emit bytecode
-
mark
Updates the phase marker on all sources.- Throws:
CompilationFailedException
-
addPhaseOperation
Deprecated.Adapts a deprecated groovy-class operation to the current API.- Parameters:
op- the operation to add
-
addPhaseOperation
Deprecated.Adapts a deprecated source-unit operation to the current API.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addPhaseOperation
Deprecated.Adapts a deprecated primary-class operation to the current API.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addFirstPhaseOperation
@Deprecated public void addFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase) Deprecated.Adapts a deprecated primary-class operation to the front of a phase queue.- Parameters:
op- the operation to addphase- the phase in which to run it
-
addNewPhaseOperation
Deprecated.Adapts a deprecated source-unit operation to the delayed phase queue.- Parameters:
op- the operation to addphase- the phase in which to run it
-
applyToSourceUnits
@Deprecated public void applyToSourceUnits(CompilationUnit.SourceUnitOperation op) throws CompilationFailedException Deprecated.Applies a deprecated source-unit operation to all source units.- Parameters:
op- the operation to run- Throws:
CompilationFailedException- if processing fails
-
applyToPrimaryClassNodes
@Deprecated public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation op) throws CompilationFailedException Deprecated.Applies a deprecated primary-class operation to all primary class nodes.- Parameters:
op- the operation to run- Throws:
CompilationFailedException- if processing fails
-