Package: org.codehaus.groovy.ast

## [Java] Class CompileUnit

- org.codehaus.groovy.ast.CompileUnit

- All Implemented Interfaces and Traits:
    NodeMetaDataHandler

      public class CompileUnit
      extends Object
      implements NodeMetaDataHandler

  Represents a complete compilation step involving one or more ModuleNode instances. A single CompileUnit is shared across all modules and classes compiled in one invocation, serving as a central repository for class metadata, import resolution, and compilation state.

  The compile unit maintains mappings from fully qualified class names to ClassNode instances, tracks which classes require compilation, stores source file locations, and manages generated inner classes. It is attached to MethodNode and ClassNode instances to enable fully qualified name resolution, import lookup, and other compilation-time queries.

  See Also:
    ModuleNode
    ClassNode
    SourceUnit
    CompilerConfiguration

- - ### Constructor Summary

    Constructors
    Constructor and description
    `CompileUnit(GroovyClassLoader classLoader, CompilerConfiguration config)` Creates a compile unit with the given classloader and compiler configuration.
    `CompileUnit(GroovyClassLoader classLoader, CodeSource codeSource, CompilerConfiguration config)` Creates a compile unit with the given classloader, code source, and compiler configuration.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addClass(ClassNode node)` Adds a single class to this compilation unit, checking for duplicates and reporting errors.
    `` | `public void` | `addClassNodeToCompile(ClassNode node, SourceUnit location)` Marks a class for compilation and associates it with its source location.
    `` | `public void` | `addClasses(List<ClassNode> list)` Adds all classes in the given list to this compilation unit.
    `` | `public void` | `addGeneratedInnerClass(InnerClassNode icn)` Adds a generated inner class to the map of generated inner classes.
    `` | `public void` | `addModule(ModuleNode node)` Adds a module to this compilation unit along with all its classes.
    `` | `public ClassNode` | `getClass(String name)` Looks up a class by its fully qualified name, checking both compiled classes and classes pending compilation.
    `` | `public GroovyClassLoader` | `getClassLoader()` Returns the Groovy classloader used for loading classes during compilation.
    `` | `public List<ClassNode>` | `getClasses()` Returns all classes from all modules in this compilation unit.
    `` | `public Map<String, ClassNode>` | `getClassesToCompile()` Returns the map of classes pending compilation.
    `` | `public CodeSource` | `getCodeSource()` Returns the code source used for Java security permissions.
    `` | `public CompilerConfiguration` | `getConfig()` Returns the compiler configuration for this compilation.
    `` | `public InnerClassNode` | `getGeneratedInnerClass(String name)` Retrieves a generated inner class by name.
    `` | `public Map<String, InnerClassNode>` | `getGeneratedInnerClasses()` Returns an unmodifiable view of all generated inner classes.
    `` | `public Map<?, ?>` | `getMetaDataMap()` Returns the metadata map for storing compilation-phase metadata.
    `` | `public List<ModuleNode>` | `getModules()` Returns all modules being compiled in this unit.
    `` | `public SourceUnit` | `getScriptSourceLocation(String className)` Retrieves the source file location for a class by name.
    `` | `public boolean` | `hasClassNodeToCompile()` Checks if there are any classes pending compilation.
    `` | `public Iterator<String>` | `iterateClassNodeToCompile()` Returns an iterator over the names of classes pending compilation.
    `` | `public void` | `setMetaDataMap(Map<?, ?> metaDataMap)` Sets the metadata map for storing compilation-phase metadata.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public CompileUnit(GroovyClassLoader classLoader, CompilerConfiguration config)

      Creates a compile unit with the given classloader and compiler configuration. The code source is set to `null`.

      Parameters:
        `classLoader` - the GroovyClassLoader to use for loading classes
        `config` - the CompilerConfiguration defining compilation behavior

    - #### public CompileUnit(GroovyClassLoader classLoader, CodeSource codeSource, CompilerConfiguration config)

      Creates a compile unit with the given classloader, code source, and compiler configuration.

      Parameters:
        `classLoader` - the GroovyClassLoader to use for loading classes
        `codeSource` - the CodeSource for code permissions or `null`
        `config` - the CompilerConfiguration defining compilation behavior

  - ### Method Detail

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

      Adds a single class to this compilation unit, checking for duplicates and reporting errors. If a duplicate is detected, emits a SyntaxException describing the conflict and its possible resolution. Removes any pending compilation entry for the class if it exists.

      Parameters:
        `node` - the ClassNode to add

    - #### public void addClassNodeToCompile(ClassNode node, SourceUnit location)

      Marks a class for compilation and associates it with its source location. This method does not perform actual compilation—it is only a marker that the class should be compiled by the CompilationUnit at the end of a parse step. No marked class should remain by the end of compilation.

      Parameters:
        `node` - the ClassNode to compile
        `location` - the SourceUnit where this class is defined

    - #### public void addClasses(List<ClassNode> list)

      Adds all classes in the given list to this compilation unit.

      Parameters:
        `list` - the list of ClassNode instances to add

    - #### public void addGeneratedInnerClass(InnerClassNode icn)

      Adds a generated inner class to the map of generated inner classes. These are inner classes created during compilation, typically by transformations.

      Parameters:
        `icn` - the InnerClassNode to register as generated

    - #### public void addModule(ModuleNode node)

      Adds a module to this compilation unit along with all its classes. If the module is `null` (indicating a parsing error), it is silently ignored. Sets this compile unit as the module's owning unit.

      Parameters:
        `node` - the ModuleNode to add, or `null`

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

      Looks up a class by its fully qualified name, checking both compiled classes and classes pending compilation. Returns `null` if the name does not exist in the current compilation unit (does not check .class files on classpath).

      Parameters:
        `name` - the fully qualified class name
      Returns:
        the ClassNode or `null` if not found in this unit

    - #### public GroovyClassLoader getClassLoader()

      Returns the Groovy classloader used for loading classes during compilation.

      Returns:
        the GroovyClassLoader

    - #### public List<ClassNode> getClasses()

      Returns all classes from all modules in this compilation unit.

      Returns:
        a list of all ClassNode instances across all modules

    - #### public Map<String, ClassNode> getClassesToCompile()

      Returns the map of classes pending compilation. These are typically forward references or generated classes queued for later compilation phases.

      Returns:
        a map from fully qualified names to ClassNode instances pending compilation

    - #### public CodeSource getCodeSource()

      Returns the code source used for Java security permissions.

      Returns:
        the CodeSource or `null`

    - #### public CompilerConfiguration getConfig()

      Returns the compiler configuration for this compilation.

      Returns:
        the CompilerConfiguration

    - #### public InnerClassNode getGeneratedInnerClass(String name)

      Retrieves a generated inner class by name.

      Parameters:
        `name` - the fully qualified inner class name
      Returns:
        the InnerClassNode or `null` if not found

    - #### public Map<String, InnerClassNode> getGeneratedInnerClasses()

      Returns an unmodifiable view of all generated inner classes. These are typically inner classes generated by Groovy transformations or the compiler.

      Returns:
        an unmodifiable map from inner class names to InnerClassNode instances

    - #### public Map<?, ?> getMetaDataMap()

      Returns the metadata map for storing compilation-phase metadata. Implements NodeMetaDataHandler for consistency with AST nodes.

      Returns:
        the metadata map or `null`

    - #### public List<ModuleNode> getModules()

      Returns all modules being compiled in this unit.

      Returns:
        a list of ModuleNode instances

    - #### public SourceUnit getScriptSourceLocation(String className)

      Retrieves the source file location for a class by name. Useful for mapping compiled classes back to their source SourceUnit.

      Parameters:
        `className` - the fully qualified class name
      Returns:
        the SourceUnit where this class was defined, or `null` if not tracked

    - #### @Deprecated
      public boolean hasClassNodeToCompile()

      Checks if there are any classes pending compilation.

      deprecated:
        Use getClassesToCompile() instead
      Returns:
        `true` if `classesToCompile` is not empty

    - #### @Deprecated
      public Iterator<String> iterateClassNodeToCompile()

      Returns an iterator over the names of classes pending compilation.

      deprecated:
        Use getClassesToCompile() instead
      Returns:
        an iterator over fully qualified class names

    - #### public void setMetaDataMap(Map<?, ?> metaDataMap)

      Sets the metadata map for storing compilation-phase metadata.

      Parameters:
        `metaDataMap` - the metadata map or `null`