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.
| 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. |
| 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. |
Creates a compile unit with the given classloader and compiler configuration.
The code source is set to null.
classLoader - the GroovyClassLoader to use for loading classesconfig - the CompilerConfiguration defining compilation behaviorCreates a compile unit with the given classloader, code source, and compiler configuration.
classLoader - the GroovyClassLoader to use for loading classescodeSource - the CodeSource for code permissions or nullconfig - the CompilerConfiguration defining compilation behaviorAdds 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.
node - the ClassNode to addMarks 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.
node - the ClassNode to compilelocation - the SourceUnit where this class is definedAdds all classes in the given list to this compilation unit.
list - the list of ClassNode instances to addAdds a generated inner class to the map of generated inner classes. These are inner classes created during compilation, typically by transformations.
icn - the InnerClassNode to register as generated 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.
node - the ModuleNode to add, or null 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).
name - the fully qualified class namenull if not found in this unitReturns the Groovy classloader used for loading classes during compilation.
Returns all classes from all modules in this compilation unit.
Returns the map of classes pending compilation. These are typically forward references or generated classes queued for later compilation phases.
Returns the code source used for Java security permissions.
nullReturns the compiler configuration for this compilation.
Retrieves a generated inner class by name.
name - the fully qualified inner class namenull if not foundReturns an unmodifiable view of all generated inner classes. These are typically inner classes generated by Groovy transformations or the compiler.
Returns the metadata map for storing compilation-phase metadata. Implements NodeMetaDataHandler for consistency with AST nodes.
nullReturns all modules being compiled in this unit.
Retrieves the source file location for a class by name. Useful for mapping compiled classes back to their source SourceUnit.
className - the fully qualified class namenull if not trackedChecks if there are any classes pending compilation.
true if classesToCompile is not emptyReturns an iterator over the names of classes pending compilation.
Sets the metadata map for storing compilation-phase metadata.
metaDataMap - the metadata map or nullCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.