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:
  • Constructor Details

  • 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.
      Checks if there are any classes pending compilation.
      Returns:
      true if classesToCompile is not empty
    • iterateClassNodeToCompile

      @Deprecated public Iterator<String> iterateClassNodeToCompile()
      Deprecated.
      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