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 Summary

    Nested Classes
    Modifier and Type | Class and 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 applyToSourceUnits 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
    Modifier and Type | Field and Description
    --- | ---
    `protected CompileUnit` | `ast`
    `protected ASTTransformationsContext` | `astTransformationsContext`
    `protected CompilationUnit.ClassgenCallback` | `classgenCallback`
    `protected ClassNodeResolver` | `classNodeResolver`
    `protected java.util.Map` | `classSourcesByPublicClassName`
    `protected boolean` | `configured`
    `protected boolean` | `debug`
    `protected java.util.List<GroovyClass>` | `generatedClasses`
    `protected java.util.List<java.lang.String>` | `names`
    `protected OptimizerVisitor` | `optimizer`
    `protected CompilationUnit.ProgressCallback` | `progressCallback`
    `protected java.util.LinkedList<SourceUnit>` | `queuedSources`
    `protected ResolveVisitor` | `resolveVisitor`
    `protected java.util.Map<java.lang.String,SourceUnit>` | `sources`
    `protected StaticImportVisitor` | `staticImportVisitor`
    `protected java.util.Map` | `summariesByPublicClassName`
    `protected java.util.Map` | `summariesBySourceName`
    `protected Verifier` | `verifier`

    - ### Fields inherited from class org.codehaus.groovy.control.ProcessingUnit

      `classLoader, configuration, errorCollector, phase, phaseComplete`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CompilationUnit()` Initializes the CompilationUnit with defaults.
    `CompilationUnit(CompilerConfiguration configuration)` Initializes the CompilationUnit with no security considerations.
    `CompilationUnit(CompilerConfiguration configuration, java.security.CodeSource security, 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 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.
    `CompilationUnit(GroovyClassLoader loader)` Initializes the CompilationUnit with defaults except for class loader.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addClassNode(ClassNode node)` Adds a ClassNode directly to the unit (ie. without source).
    `void` | `addNewPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)`
    `void` | `addPhaseOperation(CompilationUnit.GroovyClassOperation op)`
    `void` | `addPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op, int phase)`
    `void` | `addPhaseOperation(CompilationUnit.SourceUnitOperation op, int phase)`
    `SourceUnit` | `addSource(java.io.File file)` Adds a source file to the unit.
    `SourceUnit` | `addSource(SourceUnit source)` Adds a SourceUnit to the unit.
    `SourceUnit` | `addSource(java.lang.String name, java.io.InputStream stream)` Adds a InputStream source to the unit.
    `SourceUnit` | `addSource(java.lang.String name, java.lang.String scriptText)`
    `SourceUnit` | `addSource(java.net.URL url)` Adds a source file to the unit.
    `void` | `addSources(java.io.File[] files)` Adds a set of source files to the unit.
    `void` | `addSources(java.lang.String[] paths)` Adds a set of file paths 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()`
    `java.util.List` | `getClasses()` Get the GroovyClasses generated by compile().
    `CompilationUnit.ClassgenCallback` | `getClassgenCallback()`
    `ClassNode` | `getClassNode(java.lang.String name)` Convenience routine to get the named ClassNode.
    `ClassNodeResolver` | `getClassNodeResolver()`
    `java.util.Map` | `getClassSourcesByPublicClassName()`
    `ClassNode` | `getFirstClassNode()` Convenience routine to get the first ClassNode, for when you are sure there is only one.
    `CompilationUnit.ProgressCallback` | `getProgressCallback()`
    `java.util.Map` | `getSummariesByPublicClassName()`
    `java.util.Map` | `getSummariesBySourceName()` Get the source summaries
    `GroovyClassLoader` | `getTransformLoader()` Returns the class loader for loading AST transformations.
    `boolean` | `isPublicClass(java.lang.String className)`
    `java.util.Iterator<SourceUnit>` | `iterator()` Returns an iterator on the unit's SourceUnits.
    `protected void` | `mark()` Updates the phase marker on all sources.
    `void` | `setClassgenCallback(CompilationUnit.ClassgenCallback visitor)` Sets a ClassgenCallback.
    `void` | `setClassNodeResolver(ClassNodeResolver classNodeResolver)`
    `void` | `setProgressCallback(CompilationUnit.ProgressCallback callback)` Sets a ProgressCallback.

    - ### Methods inherited from class org.codehaus.groovy.control.ProcessingUnit

      `completePhase, getClassLoader, getConfiguration, getErrorCollector, getPhase, getPhaseDescription, gotoPhase, nextPhase, setClassLoader, setConfiguration`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### astTransformationsContext

          protected ASTTransformationsContext astTransformationsContext

    - #### sources

          protected java.util.Map<java.lang.String,SourceUnit> sources

    - #### summariesBySourceName

          protected java.util.Map summariesBySourceName

    - #### summariesByPublicClassName

          protected java.util.Map summariesByPublicClassName

    - #### classSourcesByPublicClassName

          protected java.util.Map classSourcesByPublicClassName

    - #### names

          protected java.util.List<java.lang.String> names

    - #### queuedSources

          protected java.util.LinkedList<SourceUnit> queuedSources

    - #### ast

          protected CompileUnit ast

    - #### generatedClasses

          protected java.util.List<GroovyClass> generatedClasses

    - #### verifier

          protected Verifier verifier

    - #### debug

          protected boolean debug

    - #### configured

          protected boolean configured

    - #### classgenCallback

          protected CompilationUnit.ClassgenCallback classgenCallback

    - #### progressCallback

          protected CompilationUnit.ProgressCallback progressCallback

    - #### resolveVisitor

          protected ResolveVisitor resolveVisitor

    - #### staticImportVisitor

          protected StaticImportVisitor staticImportVisitor

    - #### optimizer

          protected OptimizerVisitor optimizer

    - #### classNodeResolver

          protected ClassNodeResolver classNodeResolver

  - ### Constructor Detail

    - #### CompilationUnit

          public CompilationUnit()

      Initializes the CompilationUnit with defaults.

    - #### CompilationUnit

          public CompilationUnit(GroovyClassLoader loader)

      Initializes the CompilationUnit with defaults except for class loader.

    - #### CompilationUnit

          public CompilationUnit(CompilerConfiguration configuration)

      Initializes the CompilationUnit with no security considerations.

    - #### CompilationUnit

          public CompilationUnit(CompilerConfiguration configuration,
                         java.security.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,
                         java.security.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

    - #### getTransformLoader

          public GroovyClassLoader getTransformLoader()

      Returns the class loader for loading AST transformations.
      Returns:
        - the transform class loader

    - #### addPhaseOperation

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

    - #### addPhaseOperation

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

    - #### addPhaseOperation

          public void addPhaseOperation(CompilationUnit.GroovyClassOperation op)

    - #### addNewPhaseOperation

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

    - #### configure

          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`.
      Overrides:
        `configure` in class `ProcessingUnit`

    - #### getAST

          public CompileUnit getAST()

      Returns the CompileUnit that roots our AST.

    - #### getSummariesBySourceName

          public java.util.Map getSummariesBySourceName()

      Get the source summaries

    - #### getSummariesByPublicClassName

          public java.util.Map getSummariesByPublicClassName()

    - #### getClassSourcesByPublicClassName

          public java.util.Map getClassSourcesByPublicClassName()

    - #### isPublicClass

          public boolean isPublicClass(java.lang.String className)

    - #### getClasses

          public java.util.List getClasses()

      Get the GroovyClasses generated by compile().

    - #### getFirstClassNode

          public ClassNode getFirstClassNode()

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

    - #### getClassNode

          public ClassNode getClassNode(java.lang.String name)

      Convenience routine to get the named ClassNode.

    - #### getASTTransformationsContext

          public ASTTransformationsContext getASTTransformationsContext()

      Returns:
        the AST transformations current context

    - #### addSources

          public void addSources(java.lang.String[] paths)

      Adds a set of file paths to the unit.

    - #### addSources

          public void addSources(java.io.File[] files)

      Adds a set of source files to the unit.

    - #### addSource

          public SourceUnit addSource(java.io.File file)

      Adds a source file to the unit.

    - #### addSource

          public SourceUnit addSource(java.net.URL url)

      Adds a source file to the unit.

    - #### addSource

          public SourceUnit addSource(java.lang.String name,
                             java.io.InputStream stream)

      Adds a InputStream source to the unit.

    - #### addSource

          public SourceUnit addSource(java.lang.String name,
                             java.lang.String scriptText)

    - #### addSource

          public SourceUnit addSource(SourceUnit source)

      Adds a SourceUnit to the unit.

    - #### iterator

          public java.util.Iterator<SourceUnit> iterator()

      Returns an iterator on the unit's SourceUnits.

    - #### addClassNode

          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

    - #### setClassgenCallback

          public void setClassgenCallback(CompilationUnit.ClassgenCallback visitor)

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

    - #### setProgressCallback

          public void setProgressCallback(CompilationUnit.ProgressCallback callback)

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

    - #### getClassgenCallback

          public CompilationUnit.ClassgenCallback getClassgenCallback()

    - #### getProgressCallback

          public CompilationUnit.ProgressCallback getProgressCallback()

    - #### compile

          public void compile()
                       throws CompilationFailedException

      Synonym for compile(Phases.ALL).
      Throws:
        `CompilationFailedException`

    - #### compile

          public void compile(int throughPhase)
                       throws CompilationFailedException

      Compiles the compilation unit from sources.
      Throws:
        `CompilationFailedException`

    - #### dequeued

          protected boolean dequeued()
                              throws CompilationFailedException

      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

          protected void mark()
                       throws CompilationFailedException

      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

          public ClassNodeResolver getClassNodeResolver()

    - #### setClassNodeResolver

          public void setClassNodeResolver(ClassNodeResolver classNodeResolver)