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
public class CompilationUnit extends ProcessingUnit
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 SummaryNested Classes Modifier and Type Class Description static interfaceCompilationUnit.ClassgenCallbackA callback interface you can use during theclassgenphase of compilation as the compiler traverses the ClassNode tree.static classCompilationUnit.GroovyClassOperationDeprecated.static interfaceCompilationUnit.IGroovyClassOperationstatic interfaceCompilationUnit.IPrimaryClassNodeOperationstatic interfaceCompilationUnit.ISourceUnitOperationstatic classCompilationUnit.PrimaryClassNodeOperationDeprecated.static interfaceCompilationUnit.ProgressCallbackA callback interface you can use to get a callback after every unit of the compile process.static classCompilationUnit.SourceUnitOperationDeprecated.
- 
Field SummaryFields Modifier and Type Field Description protected CompileUnitastThe overall AST for this CompilationUnit.protected ASTTransformationsContextastTransformationsContextThe AST transformations state data.protected CompilationUnit.ClassgenCallbackclassgenCallbackA callback called during theclassgenphase of compilationprotected ClassNodeResolverclassNodeResolverprotected booleanconfiguredTrue after the firstconfigure(CompilerConfiguration)operation.protected booleandebugIf set, outputs a little more information during compilation when errors occur.protected CompilationUnit.ProgressCallbackprogressCallbackA callback for use duringcompile()protected java.util.Queue<SourceUnit>queuedSourcesprotected ResolveVisitorresolveVisitorprotected java.util.Map<java.lang.String,SourceUnit>sourcesThe source units from which this unit is built.Fields inherited from class org.codehaus.groovy.control.ProcessingUnitclassLoader, configuration, errorCollector, phase, phaseComplete
- 
Constructor SummaryConstructors Constructor Description CompilationUnit()Initializes 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, java.security.CodeSource codeSource, GroovyClassLoader loader)Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.CompilationUnit(CompilerConfiguration configuration, java.security.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 SummaryModifier and Type Method Description voidaddClassNode(ClassNode node)Adds a ClassNode directly to the unit (ie.voidaddFirstPhaseOperation(CompilationUnit.IPrimaryClassNodeOperation op, int phase)voidaddFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)Deprecated.voidaddJavaCompilationUnits(java.util.Set<javax.tools.JavaFileObject> javaCompilationUnitSet)voidaddNewPhaseOperation(CompilationUnit.ISourceUnitOperation op, int phase)voidaddNewPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)Deprecated.voidaddPhaseOperation(CompilationUnit.GroovyClassOperation op)Deprecated.voidaddPhaseOperation(CompilationUnit.IGroovyClassOperation op)voidaddPhaseOperation(CompilationUnit.IPrimaryClassNodeOperation op, int phase)voidaddPhaseOperation(CompilationUnit.ISourceUnitOperation op, int phase)voidaddPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)Deprecated.voidaddPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)Deprecated.SourceUnitaddSource(java.io.File file)Adds a source file to the unit.SourceUnitaddSource(java.lang.String name, java.io.InputStream stream)Adds a InputStream source to the unit.SourceUnitaddSource(java.lang.String name, java.lang.String scriptText)SourceUnitaddSource(java.net.URL url)Adds a source file to the unit.SourceUnitaddSource(SourceUnit source)Adds a SourceUnit to the unit.voidaddSources(java.io.File[] files)Adds a set of source files to the unit.voidaddSources(java.lang.String[] paths)Adds a set of file paths to the unit.voidapplyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation op)Deprecated.voidapplyToSourceUnits(CompilationUnit.SourceUnitOperation op)Deprecated.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.ClassVisitorcreateClassVisitor()protected booleandequeued()Dequeues any source units add through addSource and resets the compiler phase to initialization.CompileUnitgetAST()Returns the CompileUnit that roots our AST.ASTTransformationsContextgetASTTransformationsContext()java.util.List<GroovyClass>getClasses()Get the GroovyClasses generated by compile().CompilationUnit.ClassgenCallbackgetClassgenCallback()ClassNodegetClassNode(java.lang.String name)Convenience routine to get the named ClassNode.ClassNodeResolvergetClassNodeResolver()ClassNodegetFirstClassNode()Convenience routine to get the first ClassNode, for when you are sure there is only one.java.util.Set<javax.tools.JavaFileObject>getJavaCompilationUnitSet()CompilationUnit.ProgressCallbackgetProgressCallback()GroovyClassLoadergetTransformLoader()Returns the class loader for loading AST transformations.java.util.Iterator<SourceUnit>iterator()Returns an iterator on the unit's SourceUnits.protected voidmark()Updates the phase marker on all sources.voidsetClassgenCallback(CompilationUnit.ClassgenCallback visitor)Sets a ClassgenCallback.voidsetClassNodeResolver(ClassNodeResolver classNodeResolver)voidsetProgressCallback(CompilationUnit.ProgressCallback callback)Sets a ProgressCallback.Methods inherited from class org.codehaus.groovy.control.ProcessingUnitcompletePhase, getClassLoader, getConfiguration, getErrorCollector, getPhase, getPhaseDescription, gotoPhase, isPhaseComplete, nextPhase, setClassLoader, setConfigurationMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
- 
Field Details- 
astThe overall AST for this CompilationUnit.
- 
sourcesThe source units from which this unit is built.
- 
queuedSources
- 
debugprotected boolean debugIf set, outputs a little more information during compilation when errors occur.
- 
configuredprotected boolean configuredTrue after the firstconfigure(CompilerConfiguration)operation.
- 
classgenCallbackA callback called during theclassgenphase of compilation
- 
progressCallbackA callback for use duringcompile()
- 
classNodeResolver
- 
resolveVisitor
- 
astTransformationsContextThe AST transformations state data.
 
- 
- 
Constructor Details- 
CompilationUnitpublic CompilationUnit()Initializes the CompilationUnit with defaults.
- 
CompilationUnitInitializes the CompilationUnit with defaults except for class loader.
- 
CompilationUnitInitializes the CompilationUnit with no security considerations.
- 
CompilationUnitpublic CompilationUnit(CompilerConfiguration configuration, java.security.CodeSource codeSource, GroovyClassLoader loader)Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.
- 
CompilationUnitpublic CompilationUnit(CompilerConfiguration configuration, java.security.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 transforms
- loader- - loader used to resolve classes against during compilation
- codeSource- - security setting for the compilation
- configuration- - compilation configuration
 
 
- 
- 
Method Details- 
addPhaseOperation
- 
addPhaseOperation
- 
addPhaseOperation
- 
addFirstPhaseOperation
- 
addNewPhaseOperation
- 
configureConfigures its debugging mode and classloader classpath from a given compiler configuration. This cannot be done more than once due to limitations inURLClassLoader.- Overrides:
- configurein class- ProcessingUnit
 
- 
getASTReturns the CompileUnit that roots our AST.
- 
getClassesGet the GroovyClasses generated by compile().
- 
getFirstClassNodeConvenience routine to get the first ClassNode, for when you are sure there is only one.
- 
getClassNodeConvenience routine to get the named ClassNode.
- 
getASTTransformationsContext- Returns:
- the AST transformations current context
 
- 
getClassNodeResolver
- 
setClassNodeResolver
- 
getJavaCompilationUnitSetpublic java.util.Set<javax.tools.JavaFileObject> getJavaCompilationUnitSet()
- 
addJavaCompilationUnitspublic void addJavaCompilationUnits(java.util.Set<javax.tools.JavaFileObject> javaCompilationUnitSet)
- 
getTransformLoaderReturns the class loader for loading AST transformations.
- 
addSourcespublic void addSources(java.lang.String[] paths)Adds a set of file paths to the unit.
- 
addSourcespublic void addSources(java.io.File[] files)Adds a set of source files to the unit.
- 
addSourceAdds a source file to the unit.
- 
addSourceAdds a source file to the unit.
- 
addSourceAdds a InputStream source to the unit.
- 
addSource
- 
addSourceAdds a SourceUnit to the unit.
- 
iteratorReturns an iterator on the unit's SourceUnits.
- 
addClassNodeAdds 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
- 
getClassgenCallback
- 
setClassgenCallbackSets a ClassgenCallback. You can have only one, and setting it tonullremoves any existing setting.
- 
getProgressCallback
- 
setProgressCallbackSets a ProgressCallback. You can have only one, and setting it tonullremoves any existing setting.
- 
compileSynonym forcompile(Phases.ALL).- Throws:
- CompilationFailedException
 
- 
compileCompiles the compilation unit from sources.- Throws:
- CompilationFailedException
 
- 
dequeuedDequeues 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
 
- 
createClassVisitorprotected org.objectweb.asm.ClassVisitor createClassVisitor()
- 
markUpdates the phase marker on all sources.- Throws:
- CompilationFailedException
 
- 
addPhaseOperationDeprecated.
- 
addPhaseOperationDeprecated.
- 
addPhaseOperationDeprecated.
- 
addFirstPhaseOperation@Deprecated public void addFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)Deprecated.
- 
addNewPhaseOperationDeprecated.
- 
applyToSourceUnits@Deprecated public void applyToSourceUnits(CompilationUnit.SourceUnitOperation op) throws CompilationFailedExceptionDeprecated.- Throws:
- CompilationFailedException
 
- 
applyToPrimaryClassNodes@Deprecated public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation op) throws CompilationFailedExceptionDeprecated.- Throws:
- CompilationFailedException
 
 
-