Package org.codehaus.groovy.ast

# Class CompileUnit

java.lang.Object
org.codehaus.groovy.ast.CompileUnit

All Implemented Interfaces:
  `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
  Description
  `CompileUnit(GroovyClassLoader classLoader, CodeSource codeSource, CompilerConfiguration config)`
  Creates a compile unit with the given classloader, code source, and compiler configuration.
  `CompileUnit(GroovyClassLoader classLoader, CompilerConfiguration config)`
  Creates a compile unit with the given classloader and compiler configuration.

- ## Method Summary

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

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

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

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## Constructor Details

  - ### CompileUnit

    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

  - ### CompileUnit

    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 Details

  - ### getConfig

    public CompilerConfiguration getConfig()
    Returns the compiler configuration for this compilation.
    Returns:
      the `CompilerConfiguration`

  - ### getClassLoader

    public GroovyClassLoader getClassLoader()
    Returns the Groovy classloader used for loading classes during compilation.
    Returns:
      the `GroovyClassLoader`

  - ### getCodeSource

    public CodeSource getCodeSource()
    Returns the code source used for Java security permissions.
    Returns:
      the `CodeSource` or `null`

  - ### getMetaDataMap

    public Map<?,?> getMetaDataMap()
    Returns the metadata map for storing compilation-phase metadata. Implements `NodeMetaDataHandler` for consistency with AST nodes.
    Specified by:
      `getMetaDataMap` in interface `NodeMetaDataHandler`
    Returns:
      the metadata map or `null`

  - ### setMetaDataMap

    public void setMetaDataMap(Map<?,?> metaDataMap)
    Sets the metadata map for storing compilation-phase metadata.
    Specified by:
      `setMetaDataMap` in interface `NodeMetaDataHandler`
    Parameters:
      `metaDataMap` - the metadata map or `null`

  - ### getModules

    public List<ModuleNode> getModules()
    Returns all modules being compiled in this unit.
    Returns:
      a list of `ModuleNode` instances

  - ### getClasses

    public List<ClassNode> getClasses()
    Returns all classes from all modules in this compilation unit.
    Returns:
      a list of all `ClassNode` instances across all modules

  - ### getClass

    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

  - ### getClassesToCompile

    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

  - ### getScriptSourceLocation

    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

  - ### getGeneratedInnerClasses

    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

  - ### getGeneratedInnerClass

    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

  - ### hasClassNodeToCompile

    @Deprecated public boolean hasClassNodeToCompile()
    Deprecated.
    Use `getClassesToCompile()` instead
    Checks if there are any classes pending compilation.
    Returns:
      `true` if `classesToCompile` is not empty

  - ### iterateClassNodeToCompile

    @Deprecated public Iterator<String> iterateClassNodeToCompile()
    Deprecated.
    Use `getClassesToCompile()` instead
    Returns an iterator over the names of classes pending compilation.
    Returns:
      an iterator over fully qualified class names

  - ### addModule

    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`

  - ### addClasses

    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

  - ### addClass

    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

  - ### addClassNodeToCompile

    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

  - ### addGeneratedInnerClass

    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