Package: org.codehaus.groovy.control

## [Java] Class CompilationUnit

- org.codehaus.groovy.control.CompilationUnit

- 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.

  Authors:
    Chris Poirier
    Jochen Theodorou
    Roshan Dawrani

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `static class` | `CompilationUnit.ClassgenCallback` | A callback interface you can use to "accompany" the classgen() code as it traverses the ClassNode tree.
    `static class` | `CompilationUnit.GroovyClassOperation` |
    `static class` | `CompilationUnit.PrimaryClassNodeOperation` | An callback interface for use in the applyToPrimaryClassNodes loop driver.
    `static class` | `CompilationUnit.ProgressCallback` | A callback interface you can use to get a callback after every unit of the compile process.
    `static class` | `CompilationUnit.SourceUnitOperation` | An callback interface for use in the applyToSourceUnits loop driver.

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `protected CompileUnit` | `ast` |
    `protected ASTTransformationsContext` | `astTransformationsContext` |
    `protected ClassNodeResolver` | `classNodeResolver` |
    `protected Map` | `classSourcesByPublicClassName` |
    `protected CompilationUnit.ClassgenCallback` | `classgenCallback` |
    `protected boolean` | `configured` |
    `protected boolean` | `debug` |
    `protected List<GroovyClass>` | `generatedClasses` |
    `protected List<String>` | `names` |
    `protected OptimizerVisitor` | `optimizer` |
    `protected CompilationUnit.ProgressCallback` | `progressCallback` |
    `protected LinkedList<SourceUnit>` | `queuedSources` |
    `protected ResolveVisitor` | `resolveVisitor` |
    `protected Map<String, SourceUnit>` | `sources` |
    `protected StaticImportVisitor` | `staticImportVisitor` |
    `protected Map` | `summariesByPublicClassName` |
    `protected Map` | `summariesBySourceName` |
    `protected Verifier` | `verifier` |

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class ProcessingUnit` | `classLoader, configuration, errorCollector, phase, phaseComplete`

  - ### Constructor Summary

    Constructors
    Constructor and 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, 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.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `void` | `addClassNode(ClassNode node)` Adds a ClassNode directly to the unit (ie. without source).
    `` | `void` | `addFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)`
    `` | `void` | `addNewPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)`
    `` | `void` | `addPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)`
    `` | `void` | `addPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)`
    `` | `void` | `addPhaseOperation(CompilationUnit.GroovyClassOperation op)`
    `` | `SourceUnit` | `addSource(File file)` Adds a source file to the unit.
    `` | `SourceUnit` | `addSource(URL url)` Adds a source file to the unit.
    `` | `SourceUnit` | `addSource(String name, InputStream stream)` Adds a InputStream source to the unit.
    `` | `SourceUnit` | `addSource(String name, String scriptText)`
    `` | `SourceUnit` | `addSource(SourceUnit source)` Adds a SourceUnit to the unit.
    `` | `void` | `addSources(String[] paths)` Adds a set of file paths to the unit.
    `` | `void` | `addSources(File[] files)` Adds a set of source files to the unit.
    `` | `void` | `applyToGeneratedGroovyClasses(CompilationUnit.GroovyClassOperation body)`
    `` | `void` | `applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation body)` A loop driver for applying operations to all primary ClassNodes in our AST.
    `` | `void` | `applyToSourceUnits(CompilationUnit.SourceUnitOperation body)` 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` | `createClassVisitor()`
    `` | `protected boolean` | `dequeued()` Dequeues any source units add through addSource and resets the compiler phase to initialization.
    `` | `CompileUnit` | `getAST()` Returns the CompileUnit that roots our AST.
    `` | `ASTTransformationsContext` | `getASTTransformationsContext()` @return the AST transformations current context
    `` | `ClassNode` | `getClassNode(String name)` Convenience routine to get the named ClassNode.
    `` | `ClassNodeResolver` | `getClassNodeResolver()`
    `` | `Map` | `getClassSourcesByPublicClassName()`
    `` | `List` | `getClasses()` Get the GroovyClasses generated by compile().
    `` | `CompilationUnit.ClassgenCallback` | `getClassgenCallback()`
    `` | `ClassNode` | `getFirstClassNode()` Convenience routine to get the first ClassNode, for when you are sure there is only one.
    `` | `CompilationUnit.ProgressCallback` | `getProgressCallback()`
    `` | `Map` | `getSummariesByPublicClassName()`
    `` | `Map` | `getSummariesBySourceName()` Get the source summaries
    `` | `GroovyClassLoader` | `getTransformLoader()` Returns the class loader for loading AST transformations.
    `` | `boolean` | `isPublicClass(String className)`
    `` | `Iterator<SourceUnit>` | `iterator()` Returns an iterator on the unit's SourceUnits.
    `` | `protected void` | `mark()` Updates the phase marker on all sources.
    `` | `void` | `setClassNodeResolver(ClassNodeResolver classNodeResolver)`
    `` | `void` | `setClassgenCallback(CompilationUnit.ClassgenCallback visitor)` Sets a ClassgenCallback.
    `` | `void` | `setProgressCallback(CompilationUnit.ProgressCallback callback)` Sets a ProgressCallback.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class ProcessingUnit` | `completePhase, configure, getClassLoader, getConfiguration, getErrorCollector, getPhase, getPhaseDescription, gotoPhase, nextPhase, setClassLoader, setConfiguration`
    `class Object` | `equals, toString, hashCode, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### protected CompileUnit ast

    - #### protected ASTTransformationsContext astTransformationsContext

    - #### protected ClassNodeResolver classNodeResolver

    - #### protected Map classSourcesByPublicClassName

    - #### protected CompilationUnit.ClassgenCallback classgenCallback

    - #### protected boolean configured

    - #### protected boolean debug

    - #### protected List<GroovyClass> generatedClasses

    - #### protected List<String> names

    - #### protected OptimizerVisitor optimizer

    - #### protected CompilationUnit.ProgressCallback progressCallback

    - #### protected LinkedList<SourceUnit> queuedSources

    - #### protected ResolveVisitor resolveVisitor

    - #### protected Map<String, SourceUnit> sources

    - #### protected StaticImportVisitor staticImportVisitor

    - #### protected Map summariesByPublicClassName

    - #### protected Map summariesBySourceName

    - #### protected Verifier verifier

  - ### Constructor Detail

    - #### public CompilationUnit()

      Initializes the CompilationUnit with defaults.

    - #### public CompilationUnit(GroovyClassLoader loader)

      Initializes the CompilationUnit with defaults except for class loader.

    - #### public CompilationUnit(CompilerConfiguration configuration)

      Initializes the CompilationUnit with no security considerations.

    - #### 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.

    - #### 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 transforms
        `loader` - - loader used to resolve classes against during compilation
        `security` - - security setting for the compilation
        `configuration` - - compilation configuration

  - ### Method Detail

    - #### public void addClassNode(ClassNode node)

      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

    - #### public void addFirstPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)

    - #### public void addNewPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)

    - #### public void addPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)

    - #### public void addPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)

    - #### public void addPhaseOperation(CompilationUnit.GroovyClassOperation op)

    - #### public SourceUnit addSource(File file)

      Adds a source file to the unit.

    - #### public SourceUnit addSource(URL url)

      Adds a source file to the unit.

    - #### public SourceUnit addSource(String name, InputStream stream)

      Adds a InputStream source to the unit.

    - #### public SourceUnit addSource(String name, String scriptText)

    - #### public SourceUnit addSource(SourceUnit source)

      Adds a SourceUnit to the unit.

    - #### public void addSources(String[] paths)

      Adds a set of file paths to the unit.

    - #### public void addSources(File[] files)

      Adds a set of source files to the unit.

    - #### public void applyToGeneratedGroovyClasses(CompilationUnit.GroovyClassOperation body)

    - #### public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation body)

      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.

    - #### public void applyToSourceUnits(CompilationUnit.SourceUnitOperation body)

      A loop driver for applying operations to all SourceUnits. Automatically skips units that have already been processed through the current phase.

    - #### public void compile()

      Synonym for compile(Phases.ALL).

    - #### public void compile(int throughPhase)

      Compiles the compilation unit from sources.

    - #### public void configure(CompilerConfiguration configuration)

      Configures its debugging mode and classloader classpath from a given compiler configuration. This cannot be done more than once due to limitations in URLClassLoader.

    - #### protected org.objectweb.asm.ClassVisitor createClassVisitor()

    - #### protected boolean 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.

      throws:
        CompilationFailedException
      Returns:
        true if there was a queued source

    - #### public CompileUnit getAST()

      Returns the CompileUnit that roots our AST.

    - #### public ASTTransformationsContext getASTTransformationsContext()

      Returns:
        the AST transformations current context

    - #### public ClassNode getClassNode(String name)

      Convenience routine to get the named ClassNode.

    - #### public ClassNodeResolver getClassNodeResolver()

    - #### public Map getClassSourcesByPublicClassName()

    - #### public List getClasses()

      Get the GroovyClasses generated by compile().

    - #### public CompilationUnit.ClassgenCallback getClassgenCallback()

    - #### public ClassNode getFirstClassNode()

      Convenience routine to get the first ClassNode, for when you are sure there is only one.

    - #### public CompilationUnit.ProgressCallback getProgressCallback()

    - #### public Map getSummariesByPublicClassName()

    - #### public Map getSummariesBySourceName()

      Get the source summaries

    - #### public GroovyClassLoader getTransformLoader()

      Returns the class loader for loading AST transformations.

      Returns:
        - the transform class loader

    - #### public boolean isPublicClass(String className)

    - #### public Iterator<SourceUnit> iterator()

      Returns an iterator on the unit's SourceUnits.

    - #### protected void mark()

      Updates the phase marker on all sources.

    - #### public void setClassNodeResolver(ClassNodeResolver classNodeResolver)

    - #### public void setClassgenCallback(CompilationUnit.ClassgenCallback visitor)

      Sets a ClassgenCallback. You can have only one, and setting it to null removes any existing setting.

    - #### public void setProgressCallback(CompilationUnit.ProgressCallback callback)

      Sets a ProgressCallback. You can have only one, and setting it to null removes any existing setting.